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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » CMUD Beta Forum
Lina
Novice


Joined: 12 Oct 2006
Posts: 49

PostPosted: Wed Oct 10, 2007 10:20 pm   

[2.05] Question about %case and variables (data and normal)
 
Using the example given in the manual for %case,

#VAR Greeting %case(%random(1,4),"Hi","Hello","Hiya","Hail")

when doing #SHOW @Greeting, it works perfectly displaying Hi or Hello and so on. However, I want to use this same function within a data variable, but I cannot get it to print out the words. It gives me to whole %case.

This is what I have
#ADDKEY Phrases Greeting {%case(%random(1,4),"Hi","Hello","Hiya","Hail")}

is there any way I can get #SHOW @{Phrases.Greeting} to display Hi or Hello, etc, without assigning it to another variable?

Thanks.
Reply with quote
Tech
GURU


Joined: 18 Oct 2000
Posts: 2733
Location: Atlanta, USA

PostPosted: Wed Oct 10, 2007 11:57 pm   
 
I don't think so. What you are actually doing in the example is creating a variable function. That was old zMUD/CMUD way of doing functions.

It was designed (and I don't think it's possible) to put that into a database variable.

Sorry.
_________________
Asati di tempari!
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu Oct 11, 2007 1:33 am   
 
Try this:

#ADDKEY Phrases Greeting "%case(%random(1,4),~"Hi~",~"Hello~",~"Hiya~",~"Hail~")"

Which'll put the literal string into the variable, turning it into a #varfunc, which'll hopefully be evaluated properly when you do #show @phrases.greeting - if that doesn't work, try using %eval as well.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Lina
Novice


Joined: 12 Oct 2006
Posts: 49

PostPosted: Thu Oct 11, 2007 3:29 am   
 
Both dont work. It displays the whole %case() to the screen.
Reply with quote
Arde
Enchanter


Joined: 09 Sep 2007
Posts: 605

PostPosted: Thu Oct 11, 2007 4:09 am   
 
Make a function setting with parameter. Here what I did
Code:

<func name="GetNextPriExp" type="Integer" id="133">
  <value>%case(%eval($iCurrentLevel + 1), 0, 2000, 4000, 9000, 15000, 20000, 30000, 40000, 80000, 120000, 150000, 200000, 250000, 300000, 400000, 450000, 500000, 550000, 600000, 700000, 800000, 1000000, 1500000, 2000000, 2500000, 3500000, 4700000, 6000000, 7300000, 8600000, 9900000, 11000000, 13000000, 15000000, 18300000, 21500000, 25000000, 30000000, 35000000, 40000000, 0);</value>
  <arglist>$iCurrentLevel</arglist>
  <notes>Returns number of experience points required for next level in primary class or 0 if maximum level reached. Parameter - current level in primary class.</notes>
</func>


After that you may call it like #SHOW @GetNextSecExp(%eval(30)) Do not forget to use %eval(), read more in docs about it.

P.S. As I can remember, varfunc had not evaluate properly for me these days, that why I moved to functions...
_________________
My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu Oct 11, 2007 4:24 am   
 
That'd be fine if he didn't want to keep the function in a data record variable, I guess to avoid clutter. You could always just name your functions Phrases_Whatever.

You also shouldn't need to do %eval(30) since that just returns 30, and I'm not sure if it's intended for functions to work without using #return.

You do however need %eval to get this working. My previous example works as long as you do %eval(@Phrases.Greeting) - it also works without the escaped quotes. But if your objective was to make something usable from the command line, you might be better off using a series of real functions with underscores instead of database keys.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Lina
Novice


Joined: 12 Oct 2006
Posts: 49

PostPosted: Thu Oct 11, 2007 4:49 am   
 
hm..I see what you mean now. I've looked over my code again, I had misunderstood and tried the %eval inside the variable itself, which obviously didnt work. Now I've tried it onto the #show itself and I understand what you mean. I think I can make use of that. Thank you all for your replies.

PS Lina is really a female name Confused I promise.
Reply with quote
Arde
Enchanter


Joined: 09 Sep 2007
Posts: 605

PostPosted: Thu Oct 11, 2007 1:07 pm   
 
Fang Xianfu wrote:

You also shouldn't need to do %eval(30) since that just returns 30, and I'm not sure if it's intended for functions to work without using #return.

Well, actually I use an expression here, so %eval() here is not required only in my sample. What about how functions work - I've read somewhere that Zugg wanna implement real functions, cause now they are work more like an aliase; may be I'm wrong... Can't remember now, sorry.
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu Oct 11, 2007 1:19 pm   
 
Real functions are in since 2.00. The #function command now creates proper functions, and the #return command gives returns from those functions. The old function command is now #varfunc. There's a new "function" setting type, which is what you've posted there. Notice that it's <func> and not <var> like a varfunc would be.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Thu Oct 11, 2007 1:20 pm   
 
Zugg DID implement real functions.

#func AddOne {#return (%1+1)}
#show @addone(7)

Displays 8 on the screen.

That was just a simple example though. These new REAL functions can contain anything that you would normally place into an alias.

The differences from aliases are that you can use #Return to return your results or not.
And that you must use the @Name(Paramaters) syntax.

Take a look at the help file functions.

Edit: Sliced to ribbons by Fang Confused
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Arde
Enchanter


Joined: 09 Sep 2007
Posts: 605

PostPosted: Thu Oct 11, 2007 1:37 pm   
 
Thanks! Now I remember that I red about new functions in online manual when start my work with that function, but than stuck with local help and forget what I'd read earlier. It is strange that #FUNCTION still works without using #RETURN though, pretty much as #VARFUNC does. Or may be it should be considered as "power of CMUD"? Very Happy
_________________
My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads
Reply with quote
Seb
Wizard


Joined: 14 Aug 2004
Posts: 1269

PostPosted: Thu Oct 11, 2007 3:01 pm   
 
Arde, does your example work on CMUD 2.0x? Cos it looks like an old function (now varfunc)...
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu Oct 11, 2007 3:04 pm   
 
It does indeed. I'm guessing that there's an implicit return if the function is only one line long and contains no commands.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Arde
Enchanter


Joined: 09 Sep 2007
Posts: 605

PostPosted: Thu Oct 11, 2007 3:35 pm   
 
Seb
I never had CMUD 1.xx at all. So all examples are from 2.03 or later.
_________________
My personal bug|wish list:
-Wrong Priority when copy-paste setting
-1 prompt trigger for Mapper, Session and General Options, not 3 different!
-#SECTION can terminate threads
-Buttons can't start threads
Reply with quote
Zugg
MASTER


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

PostPosted: Thu Oct 11, 2007 5:05 pm   
 
Functions basically return whatever is left on the internal stack. So if you just leave a value by itself without #RETURN, then it's still on the stack, although that is a bit undefined because it might also get sent to the MUD like in an Alias in a future version. So don't depend upon the implicit #RETURN...always put #RETURN in yourself.
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu Oct 11, 2007 5:15 pm   
 
Yeah, I saw some strange behaviour with this:

#func test {%case(%1,one,two,three,four,five)
%case(%1,five,four,three,two,one)}
#say @test(2)

Displays "ERROR: argument still on stack: four" and "four" like #say, and sends "two" to the MUD.
_________________
Rorso's syntax colouriser.

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


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

PostPosted: Thu Oct 11, 2007 7:10 pm   
 
Yep. That's because a function can only return a single value, and since you have two %case statements, it ends up with two things on the stack instead of just one.

In any case, it's good to be more formal with scripts and use #RETURN to return the result of the function and use #SEND within a function when you want to send something to the MUD. Then there is no ambiguity.
Reply with quote
Seb
Wizard


Joined: 14 Aug 2004
Posts: 1269

PostPosted: Thu Oct 11, 2007 8:02 pm   
 
Functions can (now) send stuff to the MUD? I wouldn't have guessed that! Though I suppose I might have done if I had considered that you can use any # command in a function. Is that true? - Can we use any # command in a function now? So it's basically an alias that you can't call from the command line, have to supply arguments to, and returns a value?
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu Oct 11, 2007 8:24 pm   
 
Functions can now run commands, yes... that's the whole point of the change Confused

You don't have to supply arguments if your function doesn't require any, and you can still call them from the command line with #call. But it stops you from calling them accidentally, yes.
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
Seb
Wizard


Joined: 14 Aug 2004
Posts: 1269

PostPosted: Thu Oct 11, 2007 9:58 pm   
 
Fang Xianfu wrote:
Functions can now run commands, yes... that's the whole point of the change Confused

Sure, I should have bolded the word "any" in my previous comment. I had assumed that things like #SEND would not be allowed since I wouldn't have expected functions to be able (or allowed) to interact with the MUD, screen or GUI. I think I must have assumed that functions would still only be allowed to return a value and not interact in other ways. But that they could use # commands for the purposes of calculations (such as #DBNEXT). In other words I had assumed that they had a scope that was limited to themselves (which is how functions tend to work in most OO programming languages - or at least how you are encouraged to use them).

I do recognise, however, that they are more powerful if they can do any # command.
Reply with quote
Zugg
MASTER


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

PostPosted: Fri Oct 12, 2007 1:55 am   
 
Fang is correct...*any* command can be used. They *do* have their own scope in that Functions can have their own local variables, for example. But there wasn't any reason to restrict what commands could be called.

After all, this is really true in any other language too. In PHP, for example, you can still call any PHP function from within your own function. If there was a "send()" function in PHP, then you could still call it from your own function. You can still call "echo()" in your PHP function to send something to the screen/web page, etc. So I'm not sure what you mean by other languages limited what functions can do...I've never run into anything like that.
Reply with quote
Seb
Wizard


Joined: 14 Aug 2004
Posts: 1269

PostPosted: Fri Oct 12, 2007 2:09 am   
 
Yeah, I think the old zMUD "functions" were still in my thoughts and causing me to be narrow-minded, and led me to slightly confuse an object method with a function (where an object method would not usually affect other unrelated objects).
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » CMUD Beta Forum 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