Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD General Discussion
albo
Apprentice


Joined: 27 Mar 2008
Posts: 106

PostPosted: 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.
Reply with quote
unrealtor
Beginner


Joined: 23 May 2020
Posts: 16

PostPosted: 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)}
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: 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
Reply with quote
albo
Apprentice


Joined: 27 Mar 2008
Posts: 106

PostPosted: 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.
Reply with quote
solarcatfive
Newbie


Joined: 28 Jun 2021
Posts: 1

PostPosted: 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.
Reply with quote
hpoonis2010
Adept


Joined: 18 Jun 2019
Posts: 279

PostPosted: 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.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4662
Location: Pensacola, FL, USA

PostPosted: 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
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net