Variables within other things?

by Simimi

Back to Mechanic's Corner.

Simimi2006-11-04 19:18:41
Well I have finally got my system underway and things are being built. It is easier to build the system just for Mimi, but I can not do that anymore, since sitting on Simimi and trying to figur eout coding issues would be wasting her karma blessing, so I have an alt now, and I decided I might as well start from the beginning making the system so I can use it with any character.

Here is the current problem, I was told using the "st t &t" thing inside of Lusternia is not a good idea, as it will be ffected by things like stupidity and whatnot. So I am working on a targeter, this is what it looks like.

CODE
t *
ColourNote ("lime", " ", "Targetting %1!")
SetVariable ("target" , "%1")

This is sent to the script (Lua) and sets the target
CODE
attackset *
SetVariable ("attack", "%1")

This is sent to the script (Lua) and sets the attack (such as cast blast/abjure cosmicfire/nature curse)
CODE
1
Send ("("attack")at("target")")

This is sent to the script (Lua) and is SUPPOSED to send the attack variable + the word at + the target variable, but it recently occured to me I have no idea how to call a variable inside a string of something else. I have tried several other things in the alias 1 box but nothing seems to work...

Any ideas?
Love-mimi
Unknown2006-11-04 19:51:39
QUOTE(Simimi @ Nov 4 2006, 08:18 PM) 350764

CODE
1
Send ("("attack")at("target")")



I might be completely off because I don't know Mushclient or Lua, but... wouldn't that work?
CODE

Send( attack.." at "..target )

According to Lua documentation, concatenation operator is '..'
Simimi2006-11-04 19:54:02
Yea see I thought that would work, it looks ok to me...hrm, no idea....
Simimi2006-11-04 20:30:21
Using what you posted Kashim, I get..

CODE

:1: attempt to concatenate global `target' (a nil value)
stack traceback:
    :1: in main chunk


EVENTHOUGH the variable target = fink

Further ideas?
Charune2006-11-04 20:43:46
I havent tested it since I use Python and not Lua, but it should be this

CODE
Send(attack .. " at " .. target)



The part in quotes is the literal portion and sent as is.. the part outside is the string to evaluate and expand, and .. is the concatenate operater in Lua.
Simimi2006-11-04 20:48:46
This is what I get after using what You posted, Charune...

CODE

:1: attempt to concatenate global `target' (a nil value)
stack traceback:
    :1: in main chunk


when the variable target does in fact = fink. =(
Charune2006-11-04 20:57:14
Give me a moment to look at it in Lua and I will get back to you. smile.gif

Ok this is what is wrong..

When you are using the shortname of a variable, you need to define it in the alias/script. The reason it can't find a value for target is because it can't find a definition for what target means.

So to tell the command what to look for you need to define for it what to look for like this.


CODE
target = world.GetVariable("target")
attack = world.GetVariable("attack")
Send(attack .. " at " .. target)



result in testing:

command:
testattack
command sent:
test attack at test
response recieved:
Brilliant! Dumb it down for me?

If you wanted to do it the long way it would be like this:

CODE
Send(world.GetVariable("attack") .. " at " .. world.GetVariable("target"))


I advise learning the short method of defining your variables at the start, when you get into long scripts it will save you tremendous amounts of time and fixing a mistake is much easier.
Simimi2006-11-04 21:01:12
ahh, ok! Got it working! Thank you so much!!

So, for any future thing I do where I want whatever it is to get information from a Variable it will have to be defined previously, such as using world.GetVariable ?

Love,mimi
Charune2006-11-04 21:08:28
for short things, you can use the long form method, but I do advise learning the short form.. it looks longer because of more lines of code, but in the end it is a tremendous time saver.

imagine writing 1000 lines in long form, and then something happens that you need to change 200 getvariable lines... with the long form method you have to find and fix all 200.. with the short form method you just change the definition and the 200 lines are all set to the definition.
Simimi2006-11-04 21:19:06
I do not understand what you mean by "short form" and "long form"...

Could you explain, and/or perhaps give an example?

The thing is, I am learning Lua as I go, and I want to learn the "good/best/an effecient" way to get things done.

Love,mimi

EDIT: ok I see what appears to be your Edit, I think I understand this short vs. long thing. Thanks again everyone!
Charune2006-11-04 21:25:27
short form bolded part

target = world.GetVariable("target")
attack = world.GetVariable("attack")
Send(attack .. " at " .. target)

long form bolded part

Send(world.GetVariable("attack") .. " at " .. world.GetVariable("target"))

In short form you define everything at the front of the program and then use the definitions (shortform names) to call the variables needed.

In long form, you don't define the variables at all, and just type out the long format of them each time you use the variable in the program.
Acrune2006-11-04 23:59:58
:1: attempt to concatenate global `target' (a nil value)

Everytime you edit the lua script, all your variables poof. Annoying. To get around that I do things like:

if target==NIL then target=none

You'll still have to reset the target once you're done editing, but it won't crash you there anymore.
Simimi2006-11-05 02:26:43
Well now I am having the same problem with an automated recharge script, this is how it goes, in theory...

CODE
alias = recharge
mercy = world.GetVariable("enchant_mercy")
kingdom = world.GetVariable("enchant_kingdom")
perfection = world.GetVariable("enchant_perfection")
focus = world.GetVariable("enchant_focus")
levitation = world.GetVariable("enchant_levitation")
waterbreathe = world.GetVariable("enchant_waterbreathe")
cube = world.GetVariable("cube")

Send ("recharge" mercy .. "from" cube ..)


Well, I have debgged it down through several errors and now I am stuck with this one...

CODE
:9: `)' expected near `mercy'


Any idea on how to fix this one? I will keep trying!
Love,mimi
Unknown2006-11-05 03:09:24
You need a .. before mercy and a .. before cube, too, to indicate concatenation of the string and variable there.

Also, (it's been a while since I used MUSHclient myself) I think you can use an even shorter syntax by expanding the variables in the script without the use of function calls. Check the "Expand Variables" option for your trigger/alias and then try something like this:

CODE
Send("recharge " .. @enchant_mercy .. " from " .. @cube)
Simimi2006-11-05 03:36:19
Ok Zarquan... your way works perfectly, but I do not know why... I love the @variablename for variables is AWESOME!

you have

CODE
Send("recharge " .. @enchant_mercy .. " from " .. @cube)


why the .. after mercy?

Love,mimi


Why does
CODE
Send(attack .. " " .. target)
work, but something like
CODE
Send(@attack .. " " .. @target
not work?

and, if you have @variablename do you still have to define it previously in the script, such as @cube with cube = world.SetVariable ("cube") ?

More Love, mimi
Tervic2006-11-05 07:18:26
Charune, stop breaking Mimi's head....

And mimi, it looks like the .. after the @mercy_enchant is needed to add the rest of the words into the sentence.

In java the equivalent (and imo more sensible) concatenator is +, so you can think of it as "adding" more words into the sentence.

Example:
send ("say HI " .. " Mimi" .. "!") would make me do:

say HI Mimi!

Example 2: assume previously defined variable, person, set as "Mimi"

send ("say Hi ".. @person .. "!") would make me do the same as above. However, if I were to redifine the variable to be Bob, it'd make me say "Hi Bob!"
Simimi2006-11-05 07:40:42
New problem...

CODE
if (@currenthealth < (.85 * @maxhealth)) and (@potionbalance = "true")) then

CODE
:5: `)' expected near `='



Well, these \\/ these did not work...
CODE
if @currenthealth < (.85 * @maxhealth) and
if @potionbalance = "true" then
Send "drink health"

CODE
if @currenthealth < (.85 * @maxhealth) then
if @potionbalance = "true" then
Send "drink health"
end if
end if


any ideas folks?
Unknown2006-11-05 15:04:15
In most real scripting languages, you need to use a double equal sign as your comparison operator because a single equal sign is used for assigning values. In zMUD, either single or double will work for comparison, and that confuses people who then try to move to a real scripting language.

CODE
if @potionbalance == "true" then
  Send "drink health"
end if


Also, you do need to define the @variable before using it in your script, but not in the same way that you were using the local variables before. If you have a variable in your world file settings, it's considered defined and can be used with the @variable syntax, as long as you remember to check that "Expand Variables" option.
Simimi2006-11-05 17:02:42
Well since all of my variables are in the lusternia2.mcl file, and are all stored in the variables box on Mush...that makes them all world variables, right?
Acrune2006-11-05 18:14:11
if (@currenthealth < (.85 * @maxhealth)) and (@potionbalance = "true")) then

you have an extra ) at the end