HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

timer function inside a struct

08-03-2009, 10:04 AM#1
TheWye
Hi, I have one question about structs and timers. Is it not possible to call method inside a struct with an expiring timer? The example below will clarify what I mean:

Collapse JASS:
struct test
    timer T = CreateTimer()
    
    private method runmethod takes nothing returns nothing
        //do something
    endmethod
    
    method testmethod takes nothing returns nothing
        call TimerStart(T, 5.0, false, function runmethod) //<--this will cause the worldeditor to say 
                                                           //   that function "runmethod" is not defined
    endmethod
endstruct

Or is there a special way to do this thing inside a struct?? I have tried call TimerStart(T, 5.0, false, method runmethod) and it also doesn't work.

EDIT: OK, I realized my mistake. You can't call a method from another method... I thought structs were similar to Java's class...

EDIT: Let me change the question then.. Is there any use for private methods? Are they even useable??
08-03-2009, 10:24 AM#2
Hans_Maulwurf
call TimerStart(.T, 5.0, true, function thistype.runmethod)
There is a module by vexorian, which does this for you: http://www.wc3c.net/showthread.php?t=105515
08-03-2009, 10:28 AM#3
TheWye
hmm.. does that mean private methods have to be static in order to be callable by other methods inside the struct?

EDIT: ok, I just learned that we can add this. in front of the method call in order to call a private method.. but still, do I need to use the module vexorian made in order to call a method with a timer?
08-03-2009, 11:00 AM#4
Pyrogasm
Quote:
Originally Posted by TheWye
do I need to use the module vexorian made in order to call a method with a timer?
By no means do you need that. Hans was simply linking to something that might have replicated what you were trying to do.

And yeah, private methods can have their uses, but you do need the struct prefix before them if you want to call them/use them as functions.
08-03-2009, 11:11 AM#5
TheWye
Quote:
Originally Posted by Pyrogasm
but you do need the struct prefix before them if you want to call them/use them as functions.

And the method needs to be static?
08-03-2009, 12:05 PM#6
Mr_Saturn
Quote:
Originally Posted by TheWye
And the method needs to be static?
If you want to use it as a code or boolexpr, then yes.
08-03-2009, 12:18 PM#7
TheWye
I see. Thanks guys
08-03-2009, 12:46 PM#8
moyack
BTW, you don't need to define the timer in your example, this code is better and saves a timer array creation:

Collapse JASS:
struct test
    // timer T = CreateTimer() <= Bye bye!!!
    
    private method runmethod takes nothing returns nothing
        //do something
        call DestroyTimer(GetExpiredTimer()) //here you manage properly the timer.
    endmethod
    
    method testmethod takes nothing returns nothing
        call TimerStart(CreateTimer(), 5.0, false, function thistype.runmethod)
    endmethod
endstruct

Collapse TimerUtils version:
struct test
    // timer T = CreateTimer() <= Bye bye!!!
    
    private method runmethod takes nothing returns nothing
        //do something
        call ReleaseTimer(GetExpiredTimer()) //here you manage properly the timer.
    endmethod
    
    method testmethod takes nothing returns nothing
        call TimerStart(NewTimer(), 5.0, false, function thistype.runmethod)
    endmethod
endstruct
08-03-2009, 02:05 PM#9
Anitarf
Vex will not be pleased to see such liberal use of thistype...

Also, when using timers in structs you would typically want to attach a struct instance to them.
Collapse JASS:
struct test
    private static method delayed takes nothing returns nothing
        local timer t=GetExpiredTimer()
        local test this=test(GetTimerData(t))
        call ReleaseTimer(t)
        // Do stuff.
    endmethod
    
    method delayedCall takes nothing returns nothing
        local timer t=NewTimer()
        call SetTimerData(t, integer(this))
        call TimerStart(NewTimer(), 5.0, false, function test.delayed)
    endmethod
endstruct
08-03-2009, 02:41 PM#10
Tyrande_ma3x
> Vex will not be pleased to see such liberal use of thistype...
It's quite useful though, in times where you might change the struct's name. Personally I use this and thistype all the time so I don't have to worry about anything if I try to change my code.
08-03-2009, 03:47 PM#11
Anitarf
Quote:
Originally Posted by Tyrande_ma3x
It's quite useful though, in times where you might change the struct's name.
Use search-replace?
08-03-2009, 04:07 PM#12
snowtiger
Quote:
Originally Posted by Anitarf
Use search-replace?
The alternative of not doing anything is more interesting to us, lazy types :)
08-03-2009, 04:22 PM#13
Hans_Maulwurf
Isn't replace exactly what thistype does?
I just checked the JassHelper Readme for thistype and it says:
Quote:
The intended usage for thistype, is when it is actually necessary, e.g: textmacros, modules. I do not endorse the idea of people using this so they can rename the struct later, but I guess they are allowed to.
But why? What's the reason?
08-03-2009, 05:00 PM#14
moyack
Quote:
Originally Posted by Anitarf
Vex will not be pleased to see such liberal use of thistype...
Is better abuse of something than not using it, and he did it to make reference to the proper struct scope, and I'm doing that.

Quote:
Also, when using timers in structs you would typically want to attach a struct instance to them.
Ok.
Quote:
Collapse JASS:
struct test
    private static method delayed takes nothing returns nothing
        local timer t=GetExpiredTimer()
        local test this=test(GetTimerData(t))
        call ReleaseTimer(t)
        // Do stuff.
    endmethod
    
    method delayedCall takes nothing returns nothing
        local timer t=NewTimer()
        call SetTimerData(t, integer(this))
        call TimerStart(t, 5.0, false, function test.delayed)
        set t = null
    endmethod
endstruct
Fixed.
08-03-2009, 07:00 PM#15
Tyrande_ma3x
> The alternative of not doing anything is more interesting to us, lazy types :)
Indeed, besides I like the syntax better: local thistype this = thistype.allocate(), etc.