• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Is there a way to use function as argument?

Status
Not open for further replies.

Deleted member 219079

D

Deleted member 219079

How to make something like this work?
JASS:
struct something
    function f
    method destroy takes nothing returns nothing
        call f()
        call this.deallocate()
    endmethod
endstruct

function asd takes function f returns nothing
    local some = something.create()
    set some.f = f
    call some.destroy()
endfunction
No my code doesn't look like that, made that to represent my problem.

So problem is, is there a way to call a set function on destroy of a struct?
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
JASS:
function Nyan takes code cfunc returns nothing
endfunction

Uhh.. I guess you could use a ForPlayer, ForGroup or the AI native (I forgot which one) call to execute the function lol.
 

Deleted member 219079

D

Deleted member 219079

Ceday's method seems to work : )
Wc3 doesn't allow arraied codes somehow, so chobi's method doesn't work in my system..
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
you can put functions into boolexprs(And, Or, Condition, Filter, Not) and while you have to couple it with null, which may slow down the performance by a call to null function, you can put those to array.

Dont forget to clear them with DestroyBoolExpr before clearing the array

remember that the function must by all means return boolean if you are storing them in boolexprs
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
The type for a function pointer is code but you cannot have code arrays in jass. So saving it as a struct instance member won't work. Nor can you input in hashtable.

You can convert it to a trigger like CreateTrigger(), TriggerAddCondition(Condition(f)), save the trigger and then run it. Or as boolexpr, or as string for ExecuteFunc as already said, though there is no function to directly convert it. To truly dynamically obtain code, you would have to make functions like

JASS:
function someFunc_setCode takes nothing returns nothing
    set GLOBAL_CODE_VAR = function someFunc
endfunction

for each regular function, then put those special functions into containers as suggested above, assign them, run them on demand and see the resulting value of GLOBAL_CODE_VAR.

Or you make mass if-branching:

JASS:
function getCode takes integer id returns code
    if (id == UNIQUE_ID_FOR_funcA) then
        return funcA
    endif
    if (id == UNIQUE_ID_FOR_funcB) then
        return funcB
    endif
    if (id == UNIQUE_ID_FOR_funcC) then
        return funcC
    endif
    ...

or whatever you want to represent it with. Of course such tasks should be realized by compilers. You do not really require pure code though.

@edo494: The functions do not need to return a value unless you mean that pjass nags. I do not recommend to destroy single-function boolexprs (returned by Condition or Filter) because they are recycled and you can index a function by

JASS:
function GetCodeId takes code c returns integer
    return GetHandleId(Condition(c))
endfunction

if you keep them.
 
Status
Not open for further replies.
Top