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


Joined: 07 Oct 2001
Posts: 71

PostPosted: Fri Oct 13, 2006 7:48 pm   

[1.10] Local Variable question(s)
 
I understand the general concept of the local variables and that they greatly increase the speed of scripts using them over normal variables, just have a couple speed questions:


Is is always better to use local variables, or only when a variable in a script is referenced multiple times? This example seems pretty intuitive which would be faster, but anyway which is faster...

alias: kk
value: kill @enemy

or

alias: kk
value: $target=@enemy;kill $target

It would seem obvious that the first example is faster, simply because it's only 1 command, vs 2... wanted to verify though.


But what about this example...

alias: kk
value: kill %1

or

alias: kk
value: kill $target
parameter: target

With the new parameter option of alias to assign things to local variables, is that still acting as two commands, which way is faster?


Granted those are two very basic examples, and the speed difference is probably close to immesurable, just trying to get a better grasp on the actual affects of local variables and the parameter option.
_________________


Last edited by Rugbystyle on Sat Oct 14, 2006 6:05 pm; edited 1 time in total
Reply with quote
Zugg
MASTER


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

PostPosted: Fri Oct 13, 2006 8:00 pm   
 
In general, local variables will only speed things up if it is referenced more than once.

In your first example, the first version is technically faster, although the difference will be extremely small. Once the kk alias is compiled, the overhead from creating a local variable is very small.

In the second example, the two versions are the same speed. Using $target is just giving the compiler a name for the %1 variable. The compiled code for the first one uses the PARAMREF bytecode command, and the second one uses the LOCAL bytecode command, but these two commands do exactly the same thing (the only difference is that the LOCAL command has an extra integer argument to allow local variables from parent stack frames to be accessed).

Because the speed difference is negligable, I'd recommend using the first version of the first example, and the second version of the second example. Using named arguments will make your scripts more readable and easily supported than using the %1 variables, although for the simple case like you gave it probably doesn't matter.
Reply with quote
Rugbystyle
Wanderer


Joined: 07 Oct 2001
Posts: 71

PostPosted: Sat Oct 14, 2006 6:19 am   [1.10] Local Variable question(s)
 
Is there a way to use Params using a single local variable to mimic the %-1 option of zmud aliases?

So if in zmud I have an alias that is
Code:

#ALIAS ac {action=%-1}


I could send it "ac open door" or just "ac north". The problem being that I don't know how many words will be sent to the alias.
_________________
Reply with quote
Zugg
MASTER


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

PostPosted: Tue Oct 17, 2006 12:15 am   
 
The %params function will return all of the parameters. %params is the same as %params(-1) which is the same as %-1
Reply with quote
Seb
Wizard


Joined: 14 Aug 2004
Posts: 1269

PostPosted: Tue Oct 17, 2006 11:57 pm   
 
I think Rugbystyle may have been referring to named parameters to aliases...
Reply with quote
Rugbystyle
Wanderer


Joined: 07 Oct 2001
Posts: 71

PostPosted: Wed Oct 18, 2006 1:24 am   
 
Yeah, sorry - I should've been more specific. I didn't mean the literal use of "params". I wanted a way to use one parameter local variable that would take all of the input given. Like

Code:

#ALIAS ac (localAction) {action=$localAction}


that would work but only if I sent ac just one word. I was looking for a way to use the named parameters when there was an unknown number of words being sent to the alias.... like how %-1 functions compared to %1 in aliases
_________________
Reply with quote
Zugg
MASTER


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

PostPosted: Wed Oct 18, 2006 1:53 am   
 
Just list all of the possible arguments:

#ALIAS ac ($arg1,$arg2,$arg3) {action=$arg1}

First, remember that you MUST put the $ before each argument name in the argument list.

Anyway, CMUD doesn't force you to call the alias with all of the arguments. The named arguments that were not passed values should just be null. Give it a try and see if it works.
Reply with quote
Zugg
MASTER


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

PostPosted: Wed Oct 18, 2006 1:54 am   
 
To clarify, if you want to use a single named argument to represent multiple arguments passed to it, then you can't. You'll need to stick with the %-1 syntax to access multiple arguments.
Reply with quote
Rugbystyle
Wanderer


Joined: 07 Oct 2001
Posts: 71

PostPosted: Wed Oct 18, 2006 2:40 am   
 
Gotcha, that's what I was wanting to find out. Thanks
_________________
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