1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. The Lich King demands your service! We've reached the 19th edition of the Icon Contest. Come along and make some chilling servants for the one true king.
    Dismiss Notice
  4. The 4th SFX Contest has started. Be sure to participate and have a fun factor in it.
    Dismiss Notice
  5. The poll for the 21st Terraining Contest is LIVE. Be sure to check out the entries and vote for one.
    Dismiss Notice
  6. The results are out! Check them out.
    Dismiss Notice
  7. Don’t forget to sign up for the Hive Cup. There’s a 555 EUR prize pool. Sign up now!
    Dismiss Notice
  8. The Hive Workshop Cup contest results have been announced! See the maps that'll be featured in the Hive Workshop Cup tournament!
    Dismiss Notice
  9. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[JASS] Lag when running script

Discussion in 'Triggers & Scripts' started by Rugarus, Sep 30, 2019.

  1. Rugarus

    Rugarus

    Joined:
    Dec 2, 2016
    Messages:
    709
    Resources:
    0
    Resources:
    0
    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

    Code (vJASS):

    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);
    }

    }

     
     
  2. Pyrogasm

    Pyrogasm

    Joined:
    Feb 27, 2007
    Messages:
    2,901
    Resources:
    1
    Spells:
    1
    Resources:
    1
    • 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.
     
  3. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    25,546
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    Most likely adding the item to the unit is causing the problem. One can try pre-placing the item on the map and then moving it to the unit's inventory when the unit is created or removing the pre-placed copies at map initialization.