Execute a code variable for callback?

fizix

New Member
Reaction score
0
I want to make my own callback function that takes a code variable as a parameter and executes it. Is there any way in JASS to do this? I'm getting desperate and this seems to be the only solution.

JASS:

function whatever takes code callback returns nothing
   call callback() // ? ? ?
endfunction
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
In vJass, there are function interfaces:

JASS:
function interface AMethod takes integer this returns nothing

function foo takes AMethod bar, integer AnArgument returns nothing
    call bar.execute(AnArgument)
endfunction


There is also the idea of using a trigger:

JASS:
function foo takes code bar returns nothing
    local trigger t = CreateTrigger()
    call TriggerAddAction(t,bar)
    call TriggerExecute(bar)
    //My trigger is leaked, but destroying dynamic triggers is bad, what do I do?!
endfunction
 

DioD

New Member
Reaction score
57
struct using trigger evaluation.
dont know about anyone posted direct execute before me.

Here is my sample of direct execution usage, you may cut unload detection and use direct exec only.
http://www.thehelper.net/forums/showthread.php?t=158295


JASS:
    set FFCXZ__ForceExec = CreateForce()
    call ForceAddPlayer(FFCXZ__ForceExec,Player(0))

+
JASS:
        call ForceEnumPlayers(FFCXZ__ForceExec,ExecData<i>)</i>


It will execute any given code.
Also you may use ForForce with code param, it will do same, but code cannot be placed into array.
 

Bribe

vJass errors are legion
Reaction score
67
It will execute any given code.
Also you may use ForForce with code param, it will do same, but code cannot be placed into array.

Wow :O

Finally, a way to execute code without needing expensive triggers! Very nice work!
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Nice, but with functions with arguments and returns something else than a boolean i would still use function interfaces instead, just because it makes the code neater (the vJass code not the jass one ofc)

Also i suppose ForceEnum doesn't open a new thread (reset limit op) like TriggerEvaluate/Execute ?
That's not bad, and most of time even better, it's a just a thing to keep in mind before using it.
 

DioD

New Member
Reaction score
57
it open new thread, also you may add one more player to force and practice "parralell calculations"
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Interesting.
But since wc3 triggers don't support multi threading i'm wondering what happens really.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Double post !

ForForce also opens a new thread, but i assume you already know that.
I'm pretty suprised of that, i always believed it didn't, i was wrong.

Sure, if you want to use a boolexpr variable array (because we aren't allowed to use a code array) then you must use ForceEnumPlayers, that's the reason of your choice.

I suggest a little thing, instead of using Player(0) you could use GetLocalPlayer(), it should be safe and map independant, at least in your script example.
Ofc the coder shouldn't use GetFilterPlayer at all in such code, at least if he doesn't know perfectly what he is doing.

Indeed for some reason if the player red slot is setted to "no player", then you can't use this player with ForForce/ForceEnumPlayers.
I mean the Player(0) won't be added to the force.
 

DioD

New Member
Reaction score
57
this is wrong (or you are doing something wrong) but this code allow to add any player to force, also player 15 always online.

peronally i do not recomend local player anywhere, it may async replays, ever if game run smooth.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
this is wrong (or you are doing something wrong) but this code allow to add any player to force, also player 15 always online.

peronally i do not recomend local player anywhere, it may async replays, ever if game run smooth.

No i've tested, leave a slot to "none player" in player property (SetPlayerController) and try to add a player to the force, you will see the player won't be added to the force, so the code won't be executed since the force will be empty.
Now do the same with a player controller or an computer, i haven't tested with other mapcontrols, but the player slot status doesn't seem to matter.

For the async replays, i don't see how it could desync, is it a personal experience or just a guess ?

EDIT :

Ok i'm wrong but it's a lot tricker, let's test that :

JASS:
library Something initializer onInit

globals
    force F
endglobals

private function callback takes nothing returns nothing
    call BJDebugMsg(&quot;for force , enum player : &quot; + GetPlayerName(GetEnumPlayer()))
endfunction

private function filter takes nothing returns boolean
    call BJDebugMsg(&quot;filter , filter player : &quot; + GetPlayerName(GetFilterPlayer()))
    return false
endfunction

private function onInit takes nothing returns nothing
    call TriggerSleepAction(0)
    set F = CreateForce()
    //player(3) and 4 are empty slots players controllers
    call ForceAddPlayer(F,Player(3))
    call ForceAddPlayer(F,Player(4))

    call ForForce(F,function callback)
    call ForceEnumPlayers(F,Filter(function filter))
endfunction

endlibrary


Also, test it with an empty but created force.

In fact ForceEnumPlayers will fire with all playing players (at least computers and actually playing players and also maybe leavers, i haven't tested with the others mapcontrollers), no matter if they are in the force or not.
So you can't use ForceEnumPlayers like that, it's to much map dependant.
 

DioD

New Member
Reaction score
57
well, never tested enum in multiplayer, my bad. (since i always ran test alone, this function always ran fine for player(0))

yes it enum every active player, not players already in force, but, its still possible to use.

JASS:
library Something initializer onInit

globals
    force F
    boolean B
endglobals

private function filter takes nothing returns boolean
    if B then
        return false
    endif
    set B = true
    call BJDebugMsg(&quot;filter , filter player : &quot; + GetPlayerName(GetFilterPlayer()))
    return false
endfunction

private function onInit takes nothing returns nothing
    set F = CreateForce()
    call TriggerSleepAction(0)
    set B = false
    call ForceEnumPlayers(F,Condition(function filter))
endfunction

endlibrary


Since there is always at least one player, it will work fine in any case.

localplayer() in replay will be player 0 always, if player 0 does not exist ingame game, it may cause problems.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Ofc i thought about that, but you have to agree that's silly to open X threads instead of one, in this way the vJass solution is better, even if it needs to create a trigger in map initialisation.
It was a clever idea though.
It is not your fault, how ForceEnumPlayers works is totally senseless, we couldn't imagine that.
EDIT : Hmm, in a second though that makes sense, that's kinda the same with GroupEnum after all.

About local player i'm the one which posted a thing about the replays (at least i haven't seen someone else), and i can say that's not always Player(0), it's the first human player which played in the game.
And it seems it only matters for things which doesn't interact with the gameplay, like texttags, text messages and so one.
I haven't heavily tested it but i haven't made a successful desync with a replay and GetLocalPlayer.
For example a trigger with the event "an unit is selected by player X", if i select only for player 2 with GetLocalPlayer, and inside the trigger condition/action kill all units of player 2.
In replay the same happens, even if GetLocalPlayer returns Player 1.
I can't explain why but that's it.

I know it because in first time i thought i could use GetLocalPlayer to know if it's a replay or not, without any success.
 

DioD

New Member
Reaction score
57
this anyway possible to place one unit inside corner and use this unit with GroupEnumRect.

also its enemy-ally enums, in most cases player 15 have only shops, soo we can make everyone enemy for him, expect one ally.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
this anyway possible to place one unit inside corner and use this unit with GroupEnumRect.

also its enemy-ally enums, in most cases player 15 have only shops, soo we can make everyone enemy for him, expect one ally.

Well, creating an unit just for that is way to much overkill.
It's time to recognize that vJass way (function interface/.evaluate/.execute) is actually the best :D

For personal purpose you can still use that, especially if you already use a dummy unit, but we can't seriously recommend it for the others, can we ?
 

DioD

New Member
Reaction score
57
there is no best solution.

for single run we can use enum
for multiple static calls we can use trigger

with vJass we dont have control over code, its just like GUI

i cant cache evaluate calls for futher use, cos vJass decide what and how to do, not me.
95% of users enjoy this by using GUI, vJass, cJass, zinc or some system they dont ever understand.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top