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.

Better ways to handle events

Discussion in 'Triggers & Scripts' started by chopinski, Sep 23, 2019.

  1. chopinski

    chopinski

    Joined:
    May 16, 2012
    Messages:
    147
    Resources:
    0
    Resources:
    0
    Hello Hive,

    i've been wondering if there is an optimal way to create/handle events that have potencial to fire thousands of times in a game, like EVENT_PLAYER_UNIT_DAMAGED or EVENT_PLAYER_UNIT_DEATH. Right now i'm using RegisterPlayerUnitEvent library to avoid creating one trigger for every spell/effect that i want to code. Is it the best way possible? I have about 80 triggers like the one below in my map at the moment, and the game is performing just fine, but i'm afraid that as my development continue and that number goes up, the performance impact will degrade the player experience.

    Code (vJASS):

    scope LightningSpear initializer Init

    private function Conditions takes nothing returns nothing
        local unit source = GetEventDamageSource()
        local unit target = BlzGetEventDamageTarget()

        if (UnitHasItemOfType(source, 'I03V') > 0 and BlzGetEventDamageType() == DAMAGE_TYPE_NORMAL and GetRandomInt(1,100) <= 25 and IsUnitEnemy(target, GetOwningPlayer(source)) and not IsUnitType(target, UNIT_TYPE_STRUCTURE)) then
            call CreateChainLightning(source, target, 250.0, 0, 4, 500.0, 0.2, 0.1, "BLNL", "Shock2HD.mdx", "origin")
        endif

        set source = null
        set target = null
    endfunction
    //===========================================================================
    private function Init takes nothing returns nothing
        call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DAMAGED, function Conditions)
    endfunction

    endscope
     


    Thanks.
     
  2. Pyrogasm

    Pyrogasm

    Joined:
    Feb 27, 2007
    Messages:
    2,901
    Resources:
    1
    Spells:
    1
    Resources:
    1
    No, this is as optimal as I an aware. Don’t worry about it unless it presents a problem. I suppose the only possible ‘improvement’ is to it would be to write them as textmacroes and then implement them all inside one larger function for each similar event. So put all your item checks into one trigger.
     
  3. _Guhun_

    _Guhun_

    Joined:
    Jun 12, 2010
    Messages:
    351
    Resources:
    7
    Spells:
    6
    Tutorials:
    1
    Resources:
    7
    It appears you are talking about an item's effect. You could optimize this by using some sort of system in which you store a boolexpr for every item type. Then, when a unit is damaged, you iterate over its slots to check for the items that it has. Then, for each item, if it has an associated boolexpr, you execute that boolexpr. This way, you are only responding the the event once if the unit has no items with associated boolexprs.

    I wouldn't worry about it unless it becomes a problem, though. It would be simple to refactor it later, if optimization is needed.
     
  4. chopinski

    chopinski

    Joined:
    May 16, 2012
    Messages:
    147
    Resources:
    0
    Resources:
    0
    thanks guys, i guess i can try your ideas if stuff becomes a problem in near future.
     
  5. Cespie

    Cespie

    Joined:
    May 21, 2019
    Messages:
    344
    Resources:
    0
    Resources:
    0
    One thing you could do, is have all of the "unit is damaged" events collected in one master trigger.
    This trigger then checks whatever conditions you have in your other triggers, and stops the trigger if the correct one is found. You could then sort the trigger, so that the most common occurences are checked first. That way, you optimize the processing time for each instance, as you can at least filter out all of the wrong triggers past the correct one. This isn't a perfect solution, but in certain scenarios, it could work very well.
    Another thing worth looking into, is your overall game design. It's a very good idea to design your game around instanced content, which means that you can limit the processes to only the ones you know for sure will be relevant at a given point in time. If you have a specific event in your map that's causing a large performance load, you could instance that game event, so that the associated trigger events are only active, whenever that game event is ongoing.