• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Lag when running script

Status
Not open for further replies.
Level 12
Joined
Dec 2, 2016
Messages
733
I've got 3 scripts in my game that all run when a unit is created for the first time in game. They cause a small lag spike in the game, any idea what causes this and how I can fix that? Here is the latest that I've made. I recall that I may need to use the Preload function? But if that's so, why do you need to preload a unit when a trigger like this runs?


// Zinc

JASS:
library dropOuts requires libMisc, libSetup, unitClassification, bloodTower {

public boolean unitHas_humanVault[12];
public unit slayer[12];

function OnTimye60() {
  integer i;
  timer t=GetExpiredTimer();
 
  for (0 <= i < 13) {
if (unitHas_humanVault[i-1] == true) {
   RemoveItem( GetItemOfTypeFromUnitBJ(slayer[GetConvertedPlayerId(Player(i))-1],'I011') );
   }
  }
}


function onStarty() {
timer t=CreateTimer();
TimerStart(t, 15, true, function OnTimye60);
//TimerStart(CreateTimer(), 15, true, function OnTimye60);
        t=null;
}



function findSlayer() {
unit u = GetEnumUnit();
// Finding Slayer and Slayeress
if ( GetUnitTypeId(GetEnumUnit()) == 'HS00'  || GetUnitTypeId(GetEnumUnit()) == 'HS01') {

        CreateItemLoc( 'I011', GetUnitLoc(GetEnumUnit()) );
        UnitAddItemSwapped( GetLastCreatedItem(), GetEnumUnit() );
   slayer[GetConvertedPlayerId(GetOwningPlayer(u))-1] = GetEnumUnit();

}
  
}


function onCreation() {
unit u = GetTriggerUnit();

if (GetUnitTypeId(u) == 'h002') {

DisplayTextToForce(GetPlayersAll(), "Creation 1");

if (unitHas_humanVault[GetConvertedPlayerId(GetOwningPlayer(u))-1] == false ) {
unitHas_humanVault[GetConvertedPlayerId(GetOwningPlayer(u))-1] = true;
    ForGroupBJ( GetUnitsInRectAll(GetEntireMapRect()), function findSlayer );
  
}


}
}


private function onInit() {
  trigger trg = CreateTrigger();
  trigger creation = CreateTrigger();
  TriggerRegisterAnyUnitEventBJ( creation, EVENT_PLAYER_UNIT_CONSTRUCT_FINISH );
  TimerStart(CreateTimer(), 60, true, function OnTimye60);
  TriggerAddAction(creation, function onCreation);
}

}
 
Level 45
Joined
Feb 27, 2007
Messages
5,578
  • The OnTimye60 function uses a for 0 <= i < 13 but then references index [i-1] which would be -1, an index that doesn't exist. This will likely cause a threadcrash or some other error.
  • GetConvertedPlayerId just adds 1 to the player's id. Doing GCPI() - 1 is literally pointless, just use GetPlayerId() instead.
  • You're leaking a group in onCreation
  • No reason to use ForGroupBJ over ForGroup itself or UnitAddItemSwapped instead of UnitAddItem (how many times have I commented on your code to say to stop using unnecessary wrapper BJs?)
  • You're leaking a location in findSlayer
  • Instead of relying on GetLastCreatedItem (which is only set by BJ functions) you can assign the output (return value) of the item creation line to a variable and then reference that variable.
  • GetEntireMapRect creates and returns a rect every time it's used (leaking, effectively); GetPlayableMapRect (or using the bj_mapInitialPlayableArea global directly) does not. Use the latter, not the former.
As far as preloading goes, you generally need to preload models and abilities. So if item I011 uses a unique model or has some unique abilities you could need to preload it. That's my guess for the lag spike.
 
Status
Not open for further replies.
Top