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

[JASS] (vJass) Inheritance problem

Status
Not open for further replies.
JASS:
library ToTestStuff initializer init

    private interface eventHandler
        method onCreate takes nothing returns nothing defaults nothing
    endinterface
    
    struct parent extends eventHandler
        public static method create takes nothing returns nothing
            local thistype this = thistype.allocate()
            
            call .onCreate()
            
            return this
        endmethod
    endstruct
    
    struct child extends parent
        method onCreate takes nothing returns nothing
            call BJDebugMsg("It worked!")
        endmethod
    endstruct
    
    private function init takes nothing returns nothing
        call child.create()
    endfunction

endlibrary

Can anyone tell me why this does not work and how to make it work?
 
I assume it is because .onCreate() isn't static.

You can try something like this:
JASS:
library ToTestStuff initializer init

    private interface eventHandler
        method onCreate takes nothing returns nothing defaults nothing
    endinterface
    
    struct parent extends eventHandler
        method whee takes nothing returns nothing
            call .onCreate()
        endmethod
        public static method create takes nothing returns parent
            local thistype this = thistype.allocate()
            return this
        endmethod
    endstruct
    
    struct child extends parent
        method onCreate takes nothing returns nothing
            call BJDebugMsg("It worked!")
        endmethod
    endstruct
    
    private function init takes nothing returns nothing
        call child.create().whee()
    endfunction
endlibrary

To achieve essentially what you wanted. Or you can just do this:
JASS:
call child.create().onCreate()

But that probably isn't what you're looking for.
 
Level 18
Joined
Jan 21, 2006
Messages
2,552
He defines this as a variable, which is what is looked for when the . operator is used, so it would be referencing the correct field.

I figured it out (after a long period of isolating the problem). It seems you cannot use the interface methods on the .create static method. If you have a timer that expires in 0 seconds and try the same thing, it will display the message.

Here is the code:
JASS:
interface parent
    method onCreate takes nothing returns nothing   defaults nothing
    method onLapse takes nothing returns nothing    defaults nothing
endinterface

struct child extends parent
    static method lapse takes nothing returns nothing
        call child(1).onLapse()
    endmethod
    static method create takes nothing returns thistype
        local thistype d = thistype.allocate()
        call TimerStart(CreateTimer(), 0, false, function thistype.lapse)
        call d.onCreate()
        return d
    endmethod
endstruct

struct superchild extends child
    method onCreate takes nothing returns nothing
        call BJDebugMsg("What?")
    endmethod
    method onLapse takes nothing returns nothing
        call BJDebugMsg("Lapse")
    endmethod
    
    static method create takes nothing returns thistype
        return .allocate()
    endmethod
endstruct

scope test initializer init
    public function init takes nothing returns nothing
        local superchild d = superchild.create()
        // The message "What?" should have been displayed.
    endfunction
endscope

Actually, Purge, you got the solution right. As long as you're not calling an interface method from within the create method it will work properly. Separating the calls into the two components as call type.create().onCreate() will display "What?" in my example.
 
Status
Not open for further replies.
Top