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
nikitis
Beginner


Joined: 01 Mar 2007
Posts: 18

PostPosted: Sun Mar 18, 2007 8:42 pm   

How can I make this reuseable
 
$
Code:
$abv=%null
#if (%2) {pctarget=%1;@abv=%2} {@abv=%1}

#switch (@abv==a) {@limb=abdomen}
   (@abv==ra) {@limb="right arm"}
   (@abv==la) {@limb="left arm"}
   (@abv==b)  {@limb=back}
   (@abv==c) {@limb=chest}
   (@abv==re) {@limb="right eye"}
   (@abv==le) {@limb="left eye"}
   (@abv==rf) {@limb="right foot"}
   (@abv==lf) {@limb="left foot}
   (@abv==h) {@limb=head}
   (@abv==n) {@limb=neck}
   (@abv==rl) {@limb="right leg"}
   (@abv==ll) {@limb="left leg"}
   (@abv==rw) {@limb="right wing}
   (@abv==lw) {@limb="left wing"}
   (@abv==t) {@limb=tail}
   (@abv==rt) {@limb="right tusk"}
   (@abv==lt) {@limb="left tusk"}



this chooses a limb is there a way I can advoid having to paste this into everything that needs to be able to choose a limb

if we we had a editor for functions like we have for alias and such it wouldn't be a problem but trying to use #FU?
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Sun Mar 18, 2007 8:53 pm   
 
Two small additions - you define the $abv local variable but then don't use it for anything. I assume you mean to change all the @abv references to $abv. Also, take a look at the online version of the #switch helpfile - it includes a second syntax for #switch that can reduce clutter when you're checking possible values of the same variable like you are in this example.

EDIT: Just spotted another thing in there, too. You use @abv= and @limb= to set the value of the two variables. You'll want to use only the variable names, abv= and limb= unless the variable is a local one.

Back on topic: custom functions in CMUD must be made up only of functions. If you wanted a function to return the full limb name when given an abbreviation, you could use %switch (no shorter syntax here I'm afraid :():

#fu limb($abv) {%switch($abv=a,abdomen,$abv=ra,right arm,...etc)}
Reply with quote
nikitis
Beginner


Joined: 01 Mar 2007
Posts: 18

PostPosted: Mon Mar 19, 2007 1:59 am   
 
hehe funny @abv is suppuse to Be $adv what I did, I originaly created the switch for script for special comand and special target
so both variables were local $abv and $limb , then when I decided to modify to set a @limb for general targets just did a search and replace on $ to @ also the #if left over from other script

so custom funtions only funtions so would %var work assigning a value to a variable?
Reply with quote
nikitis
Beginner


Joined: 01 Mar 2007
Posts: 18

PostPosted: Mon Mar 19, 2007 5:03 am   
 
#
Code:
fu setlimb($abv) {%switch($abv=ra,@limb=right arm,$abv=la,@limb=left arm,$abv=b,%eval(@limb=back),$abv=c,%eval(@limb=chest),$abv=re,%eval(@limb=right eye),$abv=le%eval(,@limb=left eye),$abv=rf,%eval(@limb=right foot),$abv=lf,%eval(@limb=left foot),$abv=h,%eval(@limb=head),$abv=n,%eval(@limb=neck),$abv=rl,%eval(@limb=right leg),$abv=lf,%eval(@limb=left foot),$abv=rl,%eval(@limb=right leg),$abv=ll,%eval(@limb=left leg),$abv=rw,%eval(@limb=right wing),$abv=lw,%eval(@limb=left wing),$abv=rt,%eval(@limb=right tusk),$abv=lt,%eval(@limb=left tusk))}


Does not compile : unmatched parenthesis at row 0 col 64

never mind is should be (%eval(@limb=ra))
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Mon Mar 19, 2007 5:30 am   
 
I guess you missed it the first time - you should be using varname=value rather than @varname=value. It's caused some scripts not to compile for some people. If you're trying to run a command from your function you should really be using %exec rather than %eval.

However, that might be moot. The way I had it originally, the function would return the name of the limb based on the abbreviation. So if you did:

#say @limb(a) @limb(rt)

then "abdomen right tusk" would be printed on the screen. Generally functions are used instead of commands because they can be used for many different things. If you wanted to recreate your alias above using the function I described, for example, you could just use this:

#var limb @setlimb(%1)

but the code is useful for more things than just setting the variable. I assumed that's what you wanted. If you just want the code to set the variable, you don't need to use functions at all, you can just have whatever script you're using run the alias that has the code you described in the first post. The code you posted in the post above this one won't return anything - you'd have to use it like this

#call @setlimb(%1)

which will be identical to just running the alias from the first post.
Reply with quote
nikitis
Beginner


Joined: 01 Mar 2007
Posts: 18

PostPosted: Thu Mar 22, 2007 4:36 am   
 
Code:
#FU setlimb($abv) {%switch $abv=cl,limb=%null,$abv=h,limb=head,$abv=rt,limb="right tusk",$abv=lt,limb="left tusk",$abv=n,limb=neck,$abv=ra,limb="right arm",$abv=la,limb="left arm",$abv=rl,limb="right leg",$abv=ll,limb="left leg",$abv=rf,limb="right foot",$abv=lf,limb="left foot",$abv=rw,limb="right wing",$abv=lw,limb="left wing")}



this seems simple take $abv and asign a value to @limb but it doesn't I recall it being mentioned functions only expand so I tried
%eval but that didn't do it
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Thu Mar 22, 2007 6:52 am   
 
Yes, you need to use %exec to execute a command from inside a function. Since your original question was how to make the code more versatile, though, my suggestion is to do away with the limb= part and just have it return the value of the limb (see my example at the end of the first reply). Then if you want to set a variable based on the result you just do:

#var limb @setlimb(%1)

or whatever. If you have the function executing code and not returning any values its only use will be with #call like I already mentioned. For that you might as well use the alias you suggested in the very first post and not bother with functions at all, especially since I'd expect function call-switch function-exec function to be much slower than alias-switch command-command.
Reply with quote
Zugg
MASTER


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

PostPosted: Thu Mar 22, 2007 6:36 pm   
 
Your function has a typo in it. You need to put a ( right after the %switch call. Like this:

#FU setlimb($abv) {%switch($abv=cl, ...

It shouldn't even compile without that. Also, Fang is correct about the %exec call. I think you might be confusing functions and commands. A Function (using #FUNC and other functions like %switch) return a value. They don't perform any actions or commands. So you cannot use the assignment statement within a function like you are doing. So follow the advice that Fang gave above.
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