|
albo Apprentice
Joined: 27 Mar 2008 Posts: 106
|
Posted: Thu Jun 11, 2020 6:04 pm
Can you call functions from within functions? |
Hi there,
I have a function that I was using the #SWITCH command in at first, because I have several things I'm trying to evaluate for a true or false status. I'd like to call a function if one of the cases in the switch statement evaluate to true, but after some testing and debugging the function nested in my function is not being called. I then changed the switch statement to be nested if statements, but that is not working either.
Code: |
#IF (@unrefinedIron = 5) {@smeltTheOre(Iron)} {#IF (@unrefinedTitanium = 5) {@smeltTheOre(Titanium)} {#IF (@unrefinedCopper = 5) {@smeltTheOre(Copper)} {#IF (@unrefinedGold = 5) {@smeltTheOre(Gold)} {#IF (@unrefinedSilver = 5) {@smeltTheOre(Silver)} {#IF (@unrefinedPlatinum = 5) {@smeltTheOre(Platinum)} {get unrefined kaka}}}}}} |
The above is my code for the first function being called, that has the nested function. The below is the code for the nested function.
Code: |
smelt $a unrefined
#IF ($a = Iron) {@unrefinedIron = 0}
#IF ($a = Copper) {@unrefinedCopper = 0}
#IF ($a = Gold) {@unrefinedGold = 0}
#IF ($a = Platinum) {@unrefinedPlatinum = 0}
#IF ($a = Silver) {@unrefinedSilver = 0}
#IF ($a = Titanium) {@unrefinedTitanium = 0} |
I'm not sure why my function smeltTheOre() is not being called. |
|
|
|
unrealtor Beginner
Joined: 23 May 2020 Posts: 19
|
Posted: Thu Jun 11, 2020 10:52 pm |
// formatting the code to check syntax and seems fine
#IF (@unrefinedIron = 5) {@smeltTheOre(Iron)} {
#IF (@unrefinedTitanium = 5) {@smeltTheOre(Titanium)} {
#IF (@unrefinedCopper = 5) {@smeltTheOre(Copper)} {
#IF (@unrefinedGold = 5) {@smeltTheOre(Gold)} {
#IF (@unrefinedSilver = 5) {@smeltTheOre(Silver)} {
#IF (@unrefinedPlatinum = 5) {@smeltTheOre(Platinum)} {
get unrefined kaka
}
}
}
}
}
}
Could it be that you are comparing a string to an integer? Try as follows:
#IF (%int(@unrefinedIron) = 5) {@smeltTheOre(Iron)} |
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4690 Location: Pensacola, FL, USA
|
Posted: Fri Jun 12, 2020 12:21 am |
Yes, you can, however, #FUNCTIONs only return a value, you are trying to use it as an #ALIAS.
That aside, given the way you structured this, you can make good use of %concat and skip a lot of code:
#IF (%int(%concat("@unrefined", $a)) = 5) {smelt $a unrefined;#VAR %concat("unrefined", $a) {0}} {get unrefined kaka} |
|
_________________ Discord: Shalimarwildcat |
|
|
|
albo Apprentice
Joined: 27 Mar 2008 Posts: 106
|
Posted: Sun Jun 14, 2020 4:47 pm |
Thank you both for taking a look at that! I worked it out by using more code and breaking out the functions, but I really like the solution provided shalimar. Now I've got to craft a bunch of items, so I'll probably take the advice of using %concat and apply it to crafting items from all the smelted ore. When my character in the MUD grabs a piece of ore from a container I can either specify the quality or the type, but not both. These means I have to track the quality and the type and when crafting I need to have a certain amount of a specific type and quality. I should be able to reduce the amount of code I use with the %concat function.
|
|
|
|
solarcatfive Newbie
Joined: 28 Jun 2021 Posts: 1
|
Posted: Tue Jun 29, 2021 10:11 am |
In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems.
|
|
|
|
hpoonis2010 Adept
Joined: 18 Jun 2019 Posts: 279
|
Posted: Thu Jul 01, 2021 7:29 am |
I was pondering whether it is possible to use a function as a structured block as in other languages. Ie., just have it perform its actions and then return. So the #RETURN would not actually return a value (no return parameter) just return to the calling code.
|
|
|
|
shalimar GURU
Joined: 04 Aug 2002 Posts: 4690 Location: Pensacola, FL, USA
|
Posted: Wed Jul 14, 2021 2:27 am |
The #CALL command or perhaps #NOOP would be better for those applications, but yes, it should be possible
They were purpose-built to run %functions and throw away the results. |
|
_________________ Discord: Shalimarwildcat |
|
|
|
|
|