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.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      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