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

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
Vorax
Apprentice


Joined: 29 Jun 2001
Posts: 198
Location: USA

PostPosted: Wed May 19, 2004 11:53 pm   

Help w/ user-defined functions?
 
I'm trying to understand how these are written. I have looked over the help file but that one example didn't help me much. Can someone explain how to write the functions and how to determine what they return?

Idealy, I have a set of code that's used in several of my triggers and I'd like to put that into a function and just call the function instead of copying that code all over the place.

Most of the code inside this trigger is what I want the function to perform, but how do I determine what it returns?

Code:

#TRIGGER {(%w) gossips ~(in (%x)~), '(*)'.$} {
  #VARIABLE name {%1} {_nodef} {translate/words}
  #VARIABLE LangToTranslate {%2} {_nodef} {translate/words}
  #VARIABLE ToTranslate {%3} {_nodef} {translate/words}
  #VARIABLE temp {} {_nodef} {translate/words}
  temp = %subchar( @ToTranslate, "?!@#$%^&*()|{}[]/<>,.';:-_=+", "")
  temp = %subchar( @temp, "0382916457", "1234567890")
  temp = %replace( @temp, " ", "|")
  #FORALL @temp {#IF (%ismember( %i, @nontranslated)) {temp = %replace( @temp, %i, %item( @translated, %ismember( %%i, @nontranslated)))}}
  temp = %replace( @temp, "|", " ")
  #SUBSTITUTE {@name gossips ~[translated~], '@temp'.}
}


@translated and @nontranslated are string lists of corisponding translated words. This is to translate things spoken in a language my character doesn't understand into English. Everything else is defined in the trigger.
Reply with quote
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Thu May 20, 2004 1:35 am   
 
You would prolly benifit better from an alias to call the commands. Just put most of the stuff inside an alias and have the trigger do something like this which I believe would work might need to dble quote the %1....%99 stuff though but not sure and cant test

#tr {gossip trig} {
Alias_with_commands
#SUB {your sub line}}

I would offer better examples but it hurts to type but User der functions work better with %functions and alias with Commands
Reply with quote
Vorax
Apprentice


Joined: 29 Jun 2001
Posts: 198
Location: USA

PostPosted: Thu May 20, 2004 2:05 am   
 
That's a pretty good idea, though it doesn't help me understand how #FUNCTION works.
Reply with quote
nexela
Wizard


Joined: 15 Jan 2002
Posts: 1644
Location: USA

PostPosted: Thu May 20, 2004 3:08 am   
 
#FUNC name {stuff} is the same as #VAR name {"stuff"} the #FUNC command doesn't expand its input on the command line.

Ok might be a bit off cause I can't remember the URL right off hand but still :P
%functions and #FUNC do stuff with the stuff in ()'s passed to them

read these helpfiles
#HELP func
and the one for advanced variables
and if I find the URL that explains it better Ill post that tooooooo :P
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Thu May 20, 2004 5:12 am   
 
How do you determine what a USER-defined function returns? The same
way you determine what will happen with any other USER-defined
setting. You wrote it, so you know what it does. If you didn't know
what it did, you wouldn't be able to write it.

It will return the value of the formula/expression after evaluation.

#FUNCTION NameOfFunction {formula/expression to be evaluated by function}
Reply with quote
Vorax
Apprentice


Joined: 29 Jun 2001
Posts: 198
Location: USA

PostPosted: Thu May 20, 2004 8:37 am   
 
Maybe this would be a better question to ask: Is it possible to do in a function what I've done in my trigger above? If so, how? And where are functions stored?
I'm just confused.
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Thu May 20, 2004 6:55 pm   
 
Functions are stored as variables.

Yes, you could probably duplicate that script in a function. However,
the expression to do so would be extremely complicated, since you
can't use commands and would have to invent your own method of
looping to replace #FORALL. It's not worth the bother (in my opinion)
when this is easily achieved with an alias.

EDIT: Inserted hard-returns so I can at least read my own reply
without needing to scroll the screen sideways.
Reply with quote
Rorso
Wizard


Joined: 14 Oct 2000
Posts: 1368

PostPosted: Sun May 23, 2004 6:37 pm   
 
quote:
Originally posted by LightBulb

Functions are stored as variables.

Yes, you could probably duplicate that script in a function. However,
the expression to do so would be extremely complicated, since you
can't use commands and would have to invent your own method of
looping to replace #FORALL. It's not worth the bother (in my opinion)
when this is easily achieved with an alias.


That isn't 100% true because if you "cheat" you can use #forall in "functions". I discovered this today and have been playing around some with ways to use it.

The solution to the issue is '%exec' Smile. You can write a "function" to add numbers in a list and return the result like this:

#func listAdd {%exec( "#forall %1 {res=%eval( @res+%i)};@res")}

Example:
#echo @listAdd(1|2|3|4) -> 10
Reply with quote
LightBulb
MASTER


Joined: 28 Nov 2000
Posts: 4817
Location: USA

PostPosted: Sun May 23, 2004 11:02 pm   
 
For a good demonstration of the difference between #FUNCTION and
#VARIABLE, see Request: DEC2HEX.
This difference is much easier to illustrate than to explain. The
topic also includes some well-written user-defined functions from
Rorso.

This problem is better suited to an alias because you're translating
entire lines, not just pieces of them. I've passed the language to
the alias even though it isn't used. It may be useful if you ever
expand to cover more than one language. This is basically just a copy
of your script from the original post, so if that worked then this
should also. I did change the one use of %%i to %i.

#AL GossipXlator {
#VAR LangToTranslate {%1} {_nodef} {translate/words}
#VAR ToTranslate {-%2} {_nodef} {translate/words}
#VAR temp {} {_nodef} {translate/words}
temp = %subchar( @ToTranslate, "?!@#$%^&*()|{}[]/<>,.';:-_=+", "")
temp = %subchar( @temp, "0382916457", "1234567890")
temp = %replace( @temp, " ", "|")
#FORALL @temp {#IF (%ismember( %i, @nontranslated)) {temp = %replace( @temp, %i, %item( @translated, %ismember( %i, @nontranslated)))}}
temp = %replace( @temp, "|", " ")
}
#TR {(%w) gossip ~(in (%x)~), '(*)'.$} {
#PRIORITY {GossipXlator %2 %3}
#SUBSTITUTE {%1 gossips ~[translated~], '@temp'.}
}

You would probably find a record-variable, with the LangToTranslate
words as keys and the English words as vals, easier to maintain than
matched lists. For instance, if @LangToTranslate is "Fowl" then you
could have a single variable named @Fowl with the Fowl words as the
keys and the English words as the vals.

Example
#ADDK Fowl gobbler turkey
#ADDK Fowl clucker chicken
#VAR temp {Lets|talk|gobbler|you|clucker}
#VAR LangToTranslate Fowl
#FORALL @temp {#IF (%iskey( @{@LangToTranslate}, %i)) {temp = %replace( @temp, %i, %db( @{@LangToTranslate}, %i))}}
#SAY {%replace( @temp, "|", " ")}
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD 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