| 05-09-2014, 09:31 PM | #1 |
Hey~ I'm trying to use interfaces and ran into a strange problem JASS:library a initializer init interface eventHandler endinterface struct Buff extends eventHandler static method create takes nothing returns thistype local eventHandler this = thistype.allocate() // if use thistype then this.getType() is compiled to the typeid of Buff // how to get correct typeid in this func ? call BJDebugMsg("Buff: " + I2S(this.getType())) // displays 2 (should be 3) return this endmethod endstruct struct BuffTypeA extends Buff endstruct private function init takes nothing returns nothing local Buff this = BuffTypeA.create() call BJDebugMsg("BuffTypeA: " + I2S(this.getType())) // displays 3 endfunction endlibrary If you look at the jass generated from jasshelper: JASS://Generated allocator of BuffTypeA function s__BuffTypeA__allocate takes nothing returns integer local integer this=sc__Buff_create() local integer kthis if(this==0) then return 0 endif set si__eventHandler_type[this]=3 set kthis=this return this endfunction The typeid of the interface is stored after the parent struct's create function is called, making it impossible to access in the parent's create function Does anyone know of a way around this without having to call a second function or using a 0 second timer in the create function ? Both have their own issues for the API ~~ I'm also having a similar issue with interface variables as well. Not being able to access them in the create method of parent structs (as the create method is always called for parent struct before variables of child struct are initialized) JASS:private interface eventHandler integer abiliconrawcode=0 endinterface struct Buff extends eventHandler static method create takes unit whichUnit returns thistype local thistype this = thistype.allocate() //abiliconrawcode is always 0 here // as it is not setup if called by // child struct until after create // method is finished call UnitAddAbility(whichUnit, abiliconrawcode) return this endmethod endstruct struct BuffTypeA extends Buff integer abiliconrawcode='A000' endstruct After Compiling://Generated allocator of BuffTypeA function s__BuffTypeA__allocate takes unit whichUnit returns integer local integer this=sc__Buff_create(whichUnit) // parent create called local integer kthis if(this==0) then return 0 endif set si__b___eventHandler_type[this]=3 set kthis=this set s__b___eventHandler_abiliconrawcode[this]='A000' // variable set return this endfunction I know it's possible to workaround by making a module require to be implemented to each child struct; but I'd rather find a way to do it without any changes to API |
| 05-10-2014, 12:02 PM | #2 |
I'm not sure why it is even important for the parent struct to know these things. You could pass the type id to the parent struct allocator as a parameter but that kind of defeats the purpose of using inheritance. That is my recommendation, though, don't use inheritance, it might seem like the logical thing to use but it's not really and existing buff systems don't use it. In fact, my recommendation would be to save yourself the trouble and use an existing buff system. |
