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
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Mon Jul 09, 2007 10:21 pm   

Functions in CMUD v2.x
 
I've got the basics of "real" functions added to CMUD v2.x.

I created a new setting type called "Function". So you add new functions in the settings editor just like adding aliases, triggers, etc. There is a button on the left to filter on functions, etc.

A function has an editor interface very similar to an alias. The only difference is that a function also has a "Type" box in the advanced setting that allows you to force a particular type of return value (autotype is the default).

The new #RETURN command is used to return the result of a function.

You call the function using the normal @function(args) syntax. If @function is a variable name, then the old style of user functions can still be used. If @function is a new function name, then the script assigned to the function is executed, and the result given by the #RETURN command is returned.

I've added a button to the Variables editor screen that allows you to convert an existing variable into a function. It basically takes the variable's value and adds #RETURN to the front of it, and then makes it into a Function object instead of a Variable object.

The #FUNCTION command on the command line now works like the #ALIAS command, but defines a function instead of an alias. You can no longer use the #FUNCTION command to create an old-style variable function.

CMUD does *not* try to automatically convert your old-style functions when it reads your package. You will need to convert your old variables into real functions using the new Convert button. I know this is unfortunate, but in the previous versions of CMUD there was no way to tell that a given variable was a user-defined function.
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Mon Jul 09, 2007 10:33 pm   
 
Oh, another good use for Functions is to create a routine that you only want to call within a script, and not be an actual Alias. For example:
Code:
#FUNC hello {
  #SHOW Hello world
  }

This works like an alias, except that putting "hello" in the command line won't call it. To call it you need to do:
Code:
#CALL @hello()

Yes, the () are required. That's how CMUD knows you are calling a function and not just accessing a variable. But this shows that functions do not need to return a value. You can use them just like functions in other programming languages and call them using #CALL if you want to throw away the result.

This is something that some people have been asking for a while now. Now you can create subroutines without ending up with a bunch of aliases that you might accidentally type on the command line.
Reply with quote
Thinjon100
Apprentice


Joined: 12 Jul 2004
Posts: 190
Location: Canada

PostPosted: Mon Jul 09, 2007 10:41 pm   
 
This is one of the additions I've really been looking forward to. You have no idea how many of my old-style variable-functions this will simplify and improve.

Since a function is mostly a specialized alias, will there be an alternate syntax w/ flags for the #ALIAS command to create functions? (more curiosity than a request)

What effect (if any) will there be if #RETURN is used in something other than a #FUNCTION?

In declaring a #FUNCTION, will the old-style #FUNC myfunc($arg1,$arg2) {} syntax still work? i.e. will I be able to name parameters from command-line creation?


--Drooling over CMud 2.x
_________________
If you're ever around Aardwolf, I'm that invisible guy you can never see. Wizi ftw! :)
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Mon Jul 09, 2007 10:51 pm   
 
Quote:
Since a function is mostly a specialized alias, will there be an alternate syntax w/ flags for the #ALIAS command to create functions?

Nope. That's what the #FUNCTION command does. No need to mess up the #ALIAS command. An Alias and a Function are similar, but they are still stored in different record types in the database and internal cache.
Quote:
What effect (if any) will there be if #RETURN is used in something other than a #FUNCTION?

The #RETURN command just pushes the result of the expression onto the internal stack. In an alias, anything left on the stack at the end of the alias is sent to the MUD. So within an alias, the #RETURN is similar to the #SEND command. But this should be considered an undefined result. Future versions of CMUD might ignore #RETURN outside of functions.
Quote:
In declaring a #FUNCTION, will the old-style #FUNC myfunc($arg1,$arg2) {} syntax still work? i.e. will I be able to name parameters from command-line creation?

Yep, that's exactly how it works. Same as the #ALIAS command.
Reply with quote
Thinjon100
Apprentice


Joined: 12 Jul 2004
Posts: 190
Location: Canada

PostPosted: Mon Jul 09, 2007 10:53 pm   
 
Awesome! You should be careful though, Zugg... you keep teasing us with all these new features (OMG... real functions, multiple threads, inline regex, Lua, SSH... *faint*) and you're going to have a ravenous pack of scripters banging on your door begging for 2.0 :P
_________________
If you're ever around Aardwolf, I'm that invisible guy you can never see. Wizi ftw! :)
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Mon Jul 09, 2007 10:56 pm   
 
Btw, I also decided to go ahead and add a command called #VARFUNC that works like the old #FUNC and creates an old-style variable function instead of the new style. This will allow people to convert their scripts more easily if they don't want to mess with the new functions. For example, if you have a line like this in your current script:
Code:
#FUNC myfunc {%1+%2}

then you can convert it to one of the following:
Code:
#FUNC myfunc {#RETURN %1+%2}
#VARFUNC myfunc {%1+%2}

I'll add this to the Changes in CMUD section of the help file once I get to the documentation of v2.x.
Reply with quote
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Mon Jul 09, 2007 11:34 pm   
 
Nice!!!

Is there anyway you could use % instead of @ for the functions? if it doesn't find a %cmud_builtinfunction it would look for a %cmud_customfunction. It would be easier to distinguish in my scripts, It would also look better in the package editor for distinguishing settings at a glance.
_________________
Zmud Support Library
Zmud Knowledge Base
Reply with quote
Thinjon100
Apprentice


Joined: 12 Jul 2004
Posts: 190
Location: Canada

PostPosted: Mon Jul 09, 2007 11:53 pm   
 
I agree with Nexela that the %func() syntax would be preferable... especially since we're used to seeing % used for functions.
Also, given that @ is my mud's color-syntax character, I tend to avoid using @ as much as possible, so this would be a definite benefit to lessen the instances where I forget whether parsing is on or off and end up trying to execute a function that doesn't exist.
_________________
If you're ever around Aardwolf, I'm that invisible guy you can never see. Wizi ftw! :)
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Tue Jul 10, 2007 12:31 am   
 
Sorry, but no. That would be a huge change in the parser. The % is only for internal functions. The whole point was to distinguish between built-in (%) and user (@) functions.

It shouldn't matter at all that @ is your mud's color-syntax character, since functions are meant to be called from within scripts and not from the command line. And from the command line, it's no different that accessing a variable with @var. And, of course, you can change @ to some other character in your Special Character preferences if you must.

Also, using @func() on the smart command line should just send it to the MUD if "func" isn't defined, although it looks like that feature of the smart command line is currently broken in v1.34.

Anyway, @func() has *always* been a user-defined function call in zMUD and that's just not going to change. All we are doing here is allowing you to write your function in a different way so that you have more control over the return result.
Reply with quote
Thinjon100
Apprentice


Joined: 12 Jul 2004
Posts: 190
Location: Canada

PostPosted: Tue Jul 10, 2007 2:30 am   
 
Understood... was merely a "would be nice" adjustment... I can completely understand if it requires a lot of changes that would ruin compatibility. :)

Still... I'm really looking forward to this next update. Any update on when we can expect this? (Hopeful Aura) :P
_________________
If you're ever around Aardwolf, I'm that invisible guy you can never see. Wizi ftw! :)
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Tue Jul 10, 2007 6:13 am   
 
Still a couple of weeks. I still need to implement the Lua stuff (so far I've just been playing with test programs to see if it does what I want). Then there is the XML import/export, then the trigger tester. Then about a week of bug fixing. So I think it's looking like the end of the month. Depends a lot on the time spent on Lua. The XML should only take about 2 days, and the trigger tester should only take a day or two, depending upon how fancy I decide to get in this version.
Reply with quote
acaykath
Wanderer


Joined: 03 Aug 2005
Posts: 84

PostPosted: Tue Jul 10, 2007 9:15 pm   
 
I don't suppose a time estimate is availabe... I've been waiting for real funcions since the beta began.
Reply with quote
bortaS
Magician


Joined: 10 Oct 2000
Posts: 320
Location: Springville, UT

PostPosted: Tue Jul 10, 2007 10:05 pm   
 
Zugg has gotten burned on time estimates on previous releases, so he doesn't make them anymore. It will get done once he's done, and that's probably as close an estimate you'll get from him. Wink
_________________
bortaS
~~ Crusty Klingon Programmer ~~
Reply with quote
Caled
Sorcerer


Joined: 21 Oct 2000
Posts: 821
Location: Australia

PostPosted: Tue Jul 10, 2007 10:20 pm   
 
All this talk of the smart command line.. I seem to have missed something along the way. Is it the reason I can't do:
"tell friendofmine try this as your alias: "#AL alias {commands}" it should work."
Whenever I do, CMUD tells me it found an error in parsing the command line, and nothing gets sent :(

I'm looking forward to this, though being in the building stage of a completely new settings file for a new class (albeit it old character) it can't come fast enough :D.
_________________
Athlon 64 3200+
Win XP Pro x64
Reply with quote
Fang Xianfu
GURU


Joined: 26 Jan 2004
Posts: 5155
Location: United Kingdom

PostPosted: Tue Jul 10, 2007 10:38 pm   
 
Bit off-topic there Caled - but did you try pressing Ctrl+R to turn off command-line parsing when pasting things like that?

Zugg did make a rough time estimate for this version in this thread, actually, despite his predilections against doing so. End of the monthish - but 2.0 will almost certainly be a beta release given the number of massive changes, and the date could be pushed back by in-house testing too.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Tue Jul 10, 2007 11:37 pm   
 
You might want to start a new topic on the command line Caled. But I just did:

tell friendofmine try this as your alias: "#AL alias {commands}" it should work.

on the command line and it worked fine for me here. But the quotes around the command are important, since that tells CMUD not to parse the text inside. If you leave out the quotes, then you get the error.

Yes, end-of-month is the closest estimate I can give right now, and that might slip if stuff takes longer to implement. I don't do hard deadlines anymore...just leads to rushed and buggy code. Fang is correct that this will definitely be a BETA version, and there have been so many low-level changes that I'm expecting the first version to be pretty buggy. You'll definitely want to make a backup of your current installation and not use it in dangerous areas of your MUD until it's been more tested.
Reply with quote
Caled
Sorcerer


Joined: 21 Oct 2000
Posts: 821
Location: Australia

PostPosted: Wed Jul 11, 2007 7:39 am   
 
Heh thanks for the reply. And apparently I wasn't using the quotes and I should have. Thanks for the reply!
_________________
Athlon 64 3200+
Win XP Pro x64
Reply with quote
Taz
GURU


Joined: 28 Sep 2000
Posts: 1395
Location: United Kingdom

PostPosted: Fri Nov 09, 2007 2:31 am   Re: Functions in CMUD v2.x
 
Zugg wrote:
I've added a button to the Variables editor screen that allows you to convert an existing variable into a function. It basically takes the variable's value and adds #RETURN to the front of it, and then makes it into a Function object instead of a Variable object.

Has this button been removed as I don't seem to able to find it?
_________________
Taz :)
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Fri Nov 09, 2007 4:23 am   
 
It was removed before Zugg let us play with v2.x

I bugged its absence when v2.x first came out.
Zugg replied stating that it was basically a click to crash Cmud button. Shocked And as such he had removed it. I don't recall if he mentioned anything about adding it back.
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Zugg
MASTER


Joined: 25 Sep 2000
Posts: 23377
Location: Colorado, USA

PostPosted: Fri Nov 09, 2007 6:35 am   
 
Nope, it's not there and probably won't be. It was a pain to get it all working, and then I decided it just wasn't needed and would make it more confusing for newbies. I thought I'd need the conversion button back when I didn't think I could support old functions. But since v2.x still supports old (#VARFUNC) functions, and converting them to a new function is a pretty simply copy/paste of the script contents, I decided it wasn't worth the effort.
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