Better ways to handle events

Status
Not open for further replies.
Level 22
Joined
May 16, 2012
Messages
652
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.

JASS:
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.
 
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.
 
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.
 
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.
 
Status
Not open for further replies.
Back
Top