Calling Functions

Rainther

I guess I should write something of value here...
Reaction score
61

ShadowInTheD

Active Member
Reaction score
12
Ok so.. I want do something like this "Set a[1] = GetLastCreatedFunction() or something like that, GetFuntion() or something.. and if you can't do that, can you do something like S2F, string to function? Or can you make a generic trigger that calls a function based on like, for example, an item being picked up, and the function it calls depends on the item it picks up (without using a ton of if/then/else)?
 

Rainther

I guess I should write something of value here...
Reaction score
61
You could have a function take the item id and do actions based on that.

JASS:
funtion change takes integer itemid returns nothing
     if GetItemTypeId(itemid) == 'Item' then
          //do actions
     endif
endfunction

call change(GetEnumItem())
 

ShadowInTheD

Active Member
Reaction score
12
You could have a function take the item id and do actions based on that.

That's exactly what I didn't want to do, create a TON of if/thens.

Instead of doing it in 300 lines, I would rather do it in like 3 ><
 

Rainther

I guess I should write something of value here...
Reaction score
61
well, what do you want to do? A hero picks up an item and then what?
 

ShadowInTheD

Active Member
Reaction score
12
My specific situation is that, I'm going to create dummy items, and functions for each item to give the items' bonuses. (I'm doing this because I'm going to have an extended inventory). The problem is, I want a generic function to call ALL the functions specific to the items, rather than being specific with every single item. (I'm planning to have A LOT of items, 15 "equip" items, and a 6 item backpack)
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
You're probably thinking of something like a function interface.

If functions are objects then we may as well have interfaces for them.

The syntax for function interfaces is:
JASS:
function interface name takes (arguments) returns (return value)

It is actually similar to a function declaration.

Variables/values of a function interface type may be called using execute() and evaluate() as defined above:

To assign to variables of a function interface type you first need to get a function's pointer. The syntax to get them is understandable if you assume that every declared function interface will get as static members the functions found in the map script that follow its argument/return value rules.

JASS:
function interface Arealfunction takes real x returns real

function double takes real x returns real
    return x*2.0
endfunction

function triple takes real x returns real
    return x*2.0
endfunction

function Test1 takes real x, Arealfunction F returns real
    return F.evaluate(F.evaluate(x)*F.evaluate(x))
endfunction

function Test2 takes nothing returns nothing
 local Arealfunction fun = Arealfunction.double //syntax to get pointer to function

   call BJDebugMsg( R2S(  Test1(1.2, fun) ))

   call BJDebugMsg( R2S(  Test1(1.2, Arealfunction.triple ) )) //also possible...
endfunction

There are many ways you can associate an item type integer with a function, but the most obvious would probably be an array of structs holding A. The Item Type integer id, and B. The function associated with it. A function interface would probably be a good way to do this.

Or, you can try a combination of struct extending and method interfaces. This was the answer to my question, which is similar to your question.
 

ShadowInTheD

Active Member
Reaction score
12
Could you explain this to me? Will it allow me to set like the variable "a[1] = someFunction"? or something similar?
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Could you explain this to me? Will it allow me to set like the variable "a[1] = someFunction"? or something similar?

Yep.

Let's say you want to associate "function ItemDoStuff" with Item-type-integer 'Whoa'.

JASS:
function interface associativeFunc takes integer itemType returns nothing

function ItemDoStuff takes integer itemType returns nothing
    call BJDebugMsg(&quot;Hi!&quot;)
endfunction

struct Wrapper
    integer itemType
    associativeFunc whichFunc
endstruct

function TryItOut takes nothing returns nothing
    local Wrapper w = Wrapper.create()
    set itemType = &#039;Whoa&#039;
    set whichFunc = associativeFunc.ItemDoStuff
    //Now we want to run some function with some item.
    call w.whichFunc.execute(w.itemType)
endfunction


Basically, you create the function as an object. Then you simply associate the two together by putting them in an instance of a struct, and you can refer to the two at any time.

The only problem with function interfaces is that the objects must have the same arguments and return values.
 
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