passing functions as arguments

Exfyre

hmm...
Reaction score
60
how would I pass a function as an argument so that it can be called remotely?
 

Azlier

Old World Ghost
Reaction score
461
Well, you take a code value and use a trigger to fire it. Alternatively, if you want to allow passing arguments, you use function interfaces.
 

Exfyre

hmm...
Reaction score
60
okay, but how would i store the code if I need to add it to a struct, since you can't have code arrays
 

Azlier

Old World Ghost
Reaction score
461
Well, you have a trigger as a member in the struct instead. You add the code as an action to the trigger and release the action when the struct is destroyed.
 

ZugZugZealot

New Member
Reaction score
33
There's also this.

JASS:
globals
    string array functions
endglobals

function HelloWorld takes nothing returns nothing
    call BJDebugMsg( "Hello World" )
endfunction

function DomoSekai takes nothing returns nothing
    call BJDebugMsg( "Domo Sekai ~Kimi Unko!" )
endfunction

function BrokeBackFootmen takes nothing returns nothing
    call CreateUnit( Player(0), 'hfoo', 0, 0, 0 )
    call CreateUnit( Player(0), 'hfoo', 100, 0, 0 )
endfunction

function Actions takes nothing returns nothing
    local integer i = 0
    loop
        exitwhen i > 2
        call ExecuteFunc( functions<i> )
        set i = i + 1
    endloop
endfunction

function Init takes nothing returns nothing
    set functions[0] = &quot;HelloWorld&quot;
    set functions[1] = &quot;DomoSekai&quot;
    set functions[2] = &quot;BrokeBackFootmen&quot;
endfunction
</i>


Though it'd be easier to pass data abusing triggers.
[ljass]local integer parentKey = GetHandleId(GetTriggeringTrigger())[/ljass]
 

Jesus4Lyf

Good Idea™
Reaction score
397
No, there isn't that for all kinds of good reasons generally :p, but there's this:
JASS:
function interface BasicFunction takes nothing returns nothing

globals
    BasicFunction array functions
endglobals

function HelloWorld takes nothing returns nothing
    call BJDebugMsg( &quot;Hello World&quot; )
endfunction

function DomoSekai takes nothing returns nothing
    call BJDebugMsg( &quot;Domo Sekai ~Kimi Unko!&quot; )
endfunction

function BrokeBackFootmen takes nothing returns nothing
    call CreateUnit( Player(0), &#039;hfoo&#039;, 0, 0, 0 )
    call CreateUnit( Player(0), &#039;hfoo&#039;, 100, 0, 0 )
endfunction

function Actions takes nothing returns nothing
    local integer i = 0
    loop
        exitwhen i &gt; 2
        call functions<i>.execute()
        set i = i + 1
    endloop
endfunction

function Init takes nothing returns nothing
    set functions[0] = HelloWorld
    set functions[1] = DomoSekai
    set functions[2] = BrokeBackFootmen
endfunction</i>

And with that you can use functions with parameters. :thup:
It is not as efficient as Azlier's suggestion. :)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top