• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[vJASS] Difference between these two methods.

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi, all, what are the differences between the two methods below:
JASS:
static method create takes nothing returns <Name_of_struct>
    local <Name_of_Struct> this = <Name_of_Struct>.allocate()
    // do some init stuff
    return this
endmethod

JASS:
static method run takes nothing returns boolean
    local <Name_of_Struct> this = <Name_of_Struct>.allocate()
    // do some init stuff
    return false
endmethod

Basically I use the second one, any wrong with it? Thanks a lot.
 
Last edited:
Level 14
Joined
Dec 12, 2012
Messages
1,007
The difference between the two methods is the return type.

Since "run" is also a method of <Name_of_Struct> it doesn't make sense to make it static and create there a local instance of your struct. Instead you should make it none-static and do all the init stuff exactly once, namely in the constructor. Otherwise you have redundancies in your code. Also you should use allocate only within your create method, everywhere else use create (only exception would be multiple constructors because vJass doesn't have method overloading).

So do something like this:

JASS:
struct MyStruct
    method run takes nothing returns boolean
        // Do something using the variables from your initialization in create
        return false
    endmethod

    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        // do some init stuff
        return this
    endmethod
endstruct

Then you can use it like this:

JASS:
function foo takes nothing returns nothing
    local MyStruct s = MyStruct.create() // Create an instance of your struct
    call s.run() // Run the instance specific method
endfunction
 
Level 11
Joined
Oct 11, 2012
Messages
711
The difference between the two methods is the return type.

Since "run" is also a method of <Name_of_Struct> it doesn't make sense to make it static and create there a local instance of your struct. Instead you should make it none-static and do all the init stuff exactly once, namely in the constructor. Otherwise you have redundancies in your code. Also you should use allocate only within your create method, everywhere else use create (only exception would be multiple constructors because vJass doesn't have method overloading).

So do something like this:

JASS:
struct MyStruct
    method run takes nothing returns boolean
        // Do something using the variables from your initialization in create
        return false
    endmethod

    static method create takes nothing returns thistype
        local thistype this = thistype.allocate()
        // do some init stuff
        return this
    endmethod
endstruct

Then you can use it like this:

JASS:
function foo takes nothing returns nothing
    local MyStruct s = MyStruct.create() // Create an instance of your struct
    call s.run() // Run the instance specific method
endfunction

Thanks for the reply. Could you take a look at my first post again, I have edited it because the preview post might have misled you. Actually, I want to know which one I should use to do the initial stuff (not having both of them in one struct). :)

Edit: I make the method "run" static because Magtheridon96 did so in his tutorial. http://www.hiveworkshop.com/forums/...80/efficient-clean-vjass-spell-models-216166/ So I am confused now.
 
Level 14
Joined
Dec 12, 2012
Messages
1,007
Thanks for the reply. Could you take a look at my first post again, I have edited it because the preview post might have misled you. Actually, I want to know which one I should use to do the initial stuff (not having both of them in one struct). :)

Edit: I make the method "run" static because Magtheridon96 did so in his tutorial. http://www.hiveworkshop.com/forums/...80/efficient-clean-vjass-spell-models-216166/ So I am confused now.

Wait, but thats a whole different thing. The "run" method is static there because it is used as a trigger condition and because no instance of the struct is ever needed. You can only add functions as trigger conditions that take no parameters (= nothing). A non-static method always takes at least one (implicit) parameter, namely the this "pointer", which is not visible for the coder. But the main reason here is that you don't need an instance of the struct for an instant spell.

If you take a look at the tutorial you will see that neither the create nor the allocate methods are ever used at all. So you should basically just do it as stated in the tutorial: use the onInit method for your initialization.
 
Level 11
Joined
Oct 11, 2012
Messages
711
Wait, but thats a whole different thing. The "run" method is static there because it is used as a trigger condition and because no instance of the struct is ever needed. You can only add functions as trigger conditions that take no parameters (= nothing). A non-static method always takes at least one (implicit) parameter, namely the this "pointer", which is not visible for the coder. But the main reason here is that you don't need an instance of the struct for an instant spell.

If you take a look at the tutorial you will see that neither the create nor the allocate methods are ever used at all. So you should basically just do it as stated in the tutorial: use the onInit method for your initialization.

I kinda get the idea. Thanks, LFH. +Rep
 
Status
Not open for further replies.
Top