• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[vJASS] Is this code useless?

Status
Not open for further replies.
Level 6
Joined
Oct 23, 2011
Messages
182
I've been thinking

"If I have hundreds of triggers checking itemid of the used item for EVENT_PLAYER_UNIT_USE_ITEM, it would call GetManipulatedItem() hundreds of times everytime an item is used" am I right?
I decided to write this short code so I can reduce the amount I call GetManipulatedItem() or GetTriggerUnit()

JASS:
library ItemUse uses Event, RegisterPlayerUnitEvent

    globals
        private Event USE
        private unit u
        private item i
    endglobals
    
    private function onUse takes nothing returns nothing
        set u=GetTriggerUnit()
        set i=GetManipulatedItem()
        call FireEvent(USE)
    endfunction
    
    function RegisterItemUseEvent takes boolexpr c returns nothing
        call RegisterEvent(c,USE)
    endfunction
    
    function GetUsingUnit takes nothing returns unit
        return u
    endfunction
    
    function GetUsedItem takes nothing returns item
        return i
    endfunction
        
    private module M
        private static method onInit takes nothing returns nothing
            set USE=CreateEvent()
            call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_USE_ITEM, function onUse)
        endmethod
    endmodule
    private struct S extends array  
        implement M
    endstruct
endlibrary

The question is.. is this useful at all? (or does it even make sense)?
I think I'm doing something completely stupid and this code is pointless

If this does serve some use, would it be better for me to just create a single trigger for each itemid, save it on a hashtable with the itemid as the childkey and execute whichever trigger that is saved to the childkey of itemid each time an item is used?
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
It's not only that you call the event response multiple times but you have mass triggers with their respective events, right? So these would all be fired and create new threads. Would surely be slower at a specific amount. The reason why I use such custom events has also other reasons though. You can define the order in which codes are fired and it does not run stuff unnecessarily that you would select out by conditions, so these conditions can be omitted. You can restrict which actions fire your events at all, so for example can easily account for a dummy that should be left out.

edit: Reread, of course, I meant that you should only fire events that get attached to the item/item types, not that you should run any code with an item use event.
 
Status
Not open for further replies.
Top