HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

[vJASS] struct question

04-13-2011, 09:38 PM#1
Yrth
Hi,
I started learning how to use the world editor yesterday so I have what might be a very retarded question.

here's my problem in condensed form

Collapse JASS:
struct Thing

    private method something takes nothing returns nothing
        ...
    endmethod

    private method startWheel takes Thing newthing returns nothing
        local trigger triggerthing = CreateTrigger()
        call TriggerRegisterTimerEventPeriodic(triggerthing, .03)
        call TriggerAddAction(triggerthing, newthing.something())
    endmethod
endstruct

this doesn't work because newthing.something() is evaluated (and returns nothing)

so my question is, how do I pass a pointer to the method: something? (or to any method for that matter)

on a very related note, are there lambda expressions in vJASS?
04-14-2011, 01:03 AM#2
tooltiperror
The problem is you are trying to use a code variable (a function that takes no arguments as a variable). In vJASS, methods with no arguments become a method with an argument, an integer, which deals with how Object-oriented programming is handled: this.variable = variable[this], structs are arrays and integers.

In order to use a method as an argument, you must use a static method with no parameters, in example:

Collapse JASS:
struct Data
    private static method onActions takes nothing returns nothing
    endmethod
    private static method onInit takes nothing returns nothing
        local trigger t=CreateTrigger()
        call TriggerAddAction(t,thistype.onActions) // notice thistype.methodName syntax
    endmethod
endstruct

There are not technically lamba functions in vJASS, however they are in ZINC and called "anonymous functions".
04-14-2011, 02:15 AM#3
Fledermaus
You should also note that static methods will not be passed the struct so you cannot do any this.someOtherMethod within them without passing the struct to the static method yourself.
04-14-2011, 12:18 PM#4
tooltiperror
Yeah, what Fledermaus said.

A static method is not an instance method, so you can't call someVariable.StaticMethod() if StaticMethod is, obviously, static. Instead you call the name of the struct, for example:
Collapse JASS:
struct Message
    string text

    static method Display takes nothing returns nothing
        call BJDebugMsg("This was called by the type name!")
    endmethod
    method display takes nothing returns nothing
        call BJDebugMsg(this.text)
    endmethod
endstruct

// somewhere else
local Message hello=Message.create()
set hello.text="Hello, World!"
call hello.display() // Hello, World!

call Message.Display() // This was called by the type name!
You can also have static variables if you want, which are useful for keeping data throughout the entire struct as a type not as an instance.
Collapse JASS:
struct Data
    static integer ten = 10
endstruct
call BJDebugMsg("ten = "+I2S(ten))
There is more on the static keyword in the JASSHelper Bible, of which it will be useful to read for structs and all other parts of the language(s).
06-25-2011, 09:47 PM#5
Yrth
sorry to take so long to reply
a lotta stuff happened between then and now

but thanks for the help guys, ill try to work around it