Can I make a struct member point to a function?

Zalinian

New Member
Reaction score
0
As the title says, I want to have a member of a struct point to function.

EX:
JASS:

function myFunc takes nothing returns nothing // <-- I also need it to take/return arguments if possible.
 // do stuff
endfunction

struct myStruct
    // members
    // more members
    Func // struct variable that points to a function
endstruct

Then somehow set myStruct.Func = myFunc, so I can call myStruct.Func() over myFunc().

Basically I need to attach certain functions to my heros (as unit-types). I already have the system for attaching structs of data to unit-types, and I can add triggers into the struct attached to the unit-types, but I can't find a way to attach a simple function.
 

Zalinian

New Member
Reaction score
0
I need to pass arguments to it.
EX:
JASS:
function myFunc takes integer i returns nothing
 // do stuff
endfunction

set myStruct.Func = myFunc
call myStruct.Func('some integer')
 

Romek

Super Moderator
Reaction score
964
> My data struct already extends an array, can I have multiple extensions?
That's got nothing to do with function interfaces..

Click me.
 

quraji

zap
Reaction score
144
My data struct already extends an array, can I have multiple extensions?
I'm still a bit confused at how I use a struct member to point to a function.

1. Structs can only extend one thing at a time.

2. You're not understanding what to do if you're trying to make your struct extend anything :p

Here we go. Say this is the function you want to have:
JASS:
function MyFunc takes string s returns nothing
     // do stuff
endfunction


The interface for this function would be like this:
JASS:
function interface MyFuncInterface takes string s returns nothing


Now, you can treat MyFuncInterface somewhat like a custom type:
JASS:
function MyFunc takes string s returns nothing
    // do stuff
endfunction

function interface MyFuncInterface takes string s returns nothing

// use it in a function:
function AnotherFunc takes nothing returns nothing
    local MyFuncInterface m = MyFunc
    call MyFunc.evaluate("a string")
endfunction

// or a struct:
struct Mystruct
    private MyFuncInterface M
    
    method SetFunc takes MyFuncInterface M returns nothing
        set this.M = M
    endmethod
    
    method EvalFunc takes nothing returns nothing
        call this.M.evaluate("a string")
    endmethod
endstruct


Hope that clears things up.
 

Zalinian

New Member
Reaction score
0
I read the 'interface' part, didn't notice the 'function interface' part till after.

Edit:Ok I changed some coding and such and I got it all to work, thanks for the help everyone.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
For the record, interfaces and function interfaces equal triggers ;p
Yes ofc, but the use is neater.
It's like use Table instead of directly an hashtable for an unlimited array.
The jass2 code is about the same but the vJass code is neater.
 

Romek

Super Moderator
Reaction score
964
> Just don't use fancy stuff in short periodic callbacks, lol.
Unless your computer is older than your mom, you won't notice even then.

If there's a simple way of doing something more efficiently, then go ahead and do it. There's no need to avoid doing things because they're 'slow' or anything though. :p
 
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