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
acaykath
Wanderer


Joined: 03 Aug 2005
Posts: 84

PostPosted: Thu Feb 08, 2007 9:40 pm   

How do I use local variables in a function?
 
I have the following code:
Code:
$cl = ""
$result = ""
#LOOP %len($colString) {
$cl = %case(%random(1,15), "{r", "{c", "{g", "{y", "{b", "{w", "{m",
  "{D", "{R", "{C", "{G", "{Y", "{B", "{W", "{M")
$cl = %concat($cl, %copy($colString, %i, 1))
$result = %concat($result, $cl)
}
$result

It takes the string colString and randomly colors each letter. It works in triggers, but i'd like it as a function so I can randomly recolor words for my own amusement.

Unfortuneately, CMud doesn't like the local variables except for those in the parametres. I also tried using aliases, but due to the fact that they had to start at the beginning of the line, it made the function useless, but it did work...

What am I doing wrong here? And what do I need to do to fix it?
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Fri Feb 09, 2007 12:21 am   
 
For one thing, using #LOCAL makes it quicker and less messy to define localvars.

Also, what sets the $colString variable? If I add it to the #local, the script compiles fine so I'm not sure how it's going wrong. I also created a test alias that looked like this:

Code:
#local $cl $result $colString
$colString=%-1
#LOOP %len($colString) {
$cl = %case(%random(1,15), "{r", "{c", "{g", "{y", "{b", "{w", "{m",
  "{D", "{R", "{C", "{G", "{Y", "{B", "{W", "{M")
$cl = %concat($cl, %copy($colString, %i, 1))
$result = %concat($result, $cl)
}
$result


and it worked just fine.
Reply with quote
acaykath
Wanderer


Joined: 03 Aug 2005
Posts: 84

PostPosted: Fri Feb 09, 2007 1:12 am   
 
I never had a problem compiling it as an alias, I wanted to compile it as a function so that it could be used somehwhere other than the beginning of a line...

ex:
Code:

#func colWrd($colString) {
  #LOCAL $cl, $result
  #LOOP %len($colString) {
    $cl = %case(%random(1,15), "{r", "{c", "{g", "{y", "{b", "{w",
      "{m", "{D", "{R", "{C", "{G", "{Y", "{B", "{W", "{M")
    $cl = %concat($cl, %copy($colString, %i, 1))
    $result = %concat($result, $cl)
    }
  $result
  }


Yet when I define it as a function, I get the error
Quote:

invalid local variable: cl at row 1 col 9
Reply with quote
Fang Xianfu
GURU


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

PostPosted: Fri Feb 09, 2007 2:42 am   
 
Oh, sorry, I misread your post, I didn't realise you were talking about functions. I'm not an expert on custom functions, but my interpretation is that functions won't actually run commands inside them, because if you wanted to do that you'd use an alias - custom functions are basically a shorthand for a long series of other functions that you'd use in conjunction with another command, but is too complicated or too time-consuming to type out every time.

In short, functions won't run commands like #loop without them being inside an %exec function - I tried creating an %exec to do this, but it wouldn't compile because the commands inside the %exec function can't see the $colString parameter properly.

Code:
%exec("#LOOP %len($colString) {$cl = %case(%random(1,15), "~{r", "~{c", "~{g", "~{y", "~{b", "~{w", "~{m","~{D", "~{R", "~{C", "~{G", "~{Y", "~{B", "~{W", "~{M");$cl = %concat($cl, %copy($colString, %i, 1));$result = %concat($result, $cl)};$result")


Had to quote the braces twice to get it to compile at all, and then when ran it gives invalid localvar $colString in the %len just after the #loop
_________________
Rorso's syntax colouriser.

- Happy bunny is happy! (1/25)
Reply with quote
acaykath
Wanderer


Joined: 03 Aug 2005
Posts: 84

PostPosted: Fri Feb 09, 2007 2:25 pm   
 
Hmm... Maybe zugg should think about adding away to do this, mayhaps a special char or function that will allow an alias to be called midline.
Reply with quote
Arminas
Wizard


Joined: 11 Jul 2002
Posts: 1265
Location: USA

PostPosted: Fri Feb 09, 2007 3:27 pm   
 
Here is how I would do this.

Create your alias that you have above, and at the end of the alias set a regular variable to the value of $result.

Then on the line before you would have called the function call the alias and place the regular variable in place of the function.

I played with this a lot a couple months ago and this was the best solution I could find at the time.

When Zugg changes the way #function works we will be able to more easily use commands within them.
_________________
Arminas, The Invisible horseman
Windows 7 Pro 32 bit
AMD 64 X2 2.51 Dual Core, 2 GB of Ram
Reply with quote
Larkin
Wizard


Joined: 25 Mar 2003
Posts: 1113
Location: USA

PostPosted: Fri Feb 09, 2007 7:25 pm   
 
Zugg is going to be implementing a true function command for CMUD, as he's said in his development blog. We'll one day be able to execute code (as an alias) and return a value (which aliases cannot do). This type of function should be far more readable and also far faster than the current "expanded string variable" version of a function. I'm really looking forward to it, as it'll help simplify logic in my scripts and speed things up a bit!
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