• 🏆 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!

Simple Unit Event Handler

Status
Not open for further replies.
Level 15
Joined
Nov 30, 2007
Messages
1,202
I've made this simple event handler for damage detection and deaths in a spell of mine but... There is a theoretical scenario where the units alive in .gr never become 0 and therefore refresh would never happen, so how do I fix my refresh method so that alive (?) units are re-included into addUnit, and what do I have to keep in mind?

JASS:
struct EventHandler 
        group        gr = CreateGroup()
        trigger      trg = null
        code         func
        unitevent    uEvent
        boolean      active = false
    
        static method addUnit takes unit u returns nothing
            if not active then
                call .refresh()
                set .active = true
            endif
            if not IsUnitInGroup(u, .gr) then
                call GroupAddUnit(.gr, u)
                call TriggerRegisterUnitEvent(.trg, u, .uEvent)
            endif
        endmethod
        
        static method refresh takes nothing returns nothing
            if .trg != null then
                call DestroyTrigger(.trg)
            endif
            set .trg = CreateTrigger()
            call TriggerAddCondition(.trg, Condition(.func))
            call GroupClear(.gr)
        endmethod
        
        static method create takes unitevent ue, code c returns EventHandler
            local thistype tmp = .allocate()
            set .uEvent = ue
            set .func = c
            return tmp
        endmethod
    endstruct

I'm not sure the above concept even works, oh unitevent and code cant be a array. What about saving them inside a hash or something and loading them that way? Right... I can't save those two in a hash, only the trigger... Forget about this. :p
 
Last edited:
I'm not sure if I'm following 100%, but for that scenario, you would just change your refresh method so that it: (1) destroys the trigger (2) iterates through the group (3) registers all alive units with their respective unit event/etc. (4) discards all the non-alive units from the group (GroupRemoveUnit).

Also, why are your addUnit and refresh methods static?
 
Level 15
Joined
Nov 30, 2007
Messages
1,202
I'm not sure if I'm following 100%, but for that scenario, you would just change your refresh method so that it: (1) destroys the trigger (2) iterates through the group (3) registers all alive units with their respective unit event/etc. (4) discards all the non-alive units from the group (GroupRemoveUnit).

Also, why are your addUnit and refresh methods static?

I realised that you can't have an array of unitevent or code so the whole point of the struct above fails. ;(

For the functions, whats the difference between a static and none static method isn't all funciton calls static?
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
static method is such a function that has no idea of the instances of given struct(can be given one explicitly in the body), and it has sense of the struct's scope. Non-static method is such function that has known information about the instance being passed in, and also has sense of the struct's scope.

I checked, and you can indeed have unitevent arrays, code is the only disallowed type
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
For the functions, whats the difference between a static and none static method isn't all funciton calls static?
JASS as a language only knows "static" function calls. However vJASS pre-compiler allows for non-static methods using some trickery. By keeping all struct instances inside an array (using parallel arrays for the members) you can pass an a reference in the form of the array index to the particular struct instance. This is similar to C objects but provided in a a way more like C++.

Abstract methods can also be supported like this with a hidden "object type" member (usually called vtable or Virtual Method Table). This can be used to determine what the concrete method implementation to call is. vJASS implements this using triggers or dynamic function calls with global variables passing the function arguments.

If you want multiple instances of variables then use a struct. If you want only a single instance (eg a singleton object) then a scope or library with globals is sufficient.
 
Status
Not open for further replies.
Top