Page 1 of 1
Setting variables
Posted: Thu Feb 06, 2014 5:53 am
by ZZZZZ
How would I go around 'selecting' a variable without a crap load of elseif's? I tried doing
Code: Select all
if __WPL.FileName == "Right" then
CurrentPosition = Right
elseif __WPL.FileName == "Left" then
CurrentPosition = Left
elseif __WPL.FileName == "Middle" then
CurrentPosition = Middle
end
if ("..CurrentPosition.."section) == false then
etc etc
end
In this case I have already set all the 'Rightsection' 'Leftsection' etc = to false in a seperate function, and from there it will change value depending on the circumstances. I could do it all with elseif blah blah but that would be in my case (6 x 3) elseif's....not including the actual stuff I already have, which has too many as is >.< Any info would be great.
Re: Setting variables
Posted: Thu Feb 06, 2014 6:19 am
by lisa
looks to me the name of the wp is the same as you are checking for currentposition?
If so it is quite simple
Re: Setting variables
Posted: Thu Feb 06, 2014 6:31 am
by ZZZZZ
That was just an example, realised they were the same after I posted but didn't want to edit it.
Code: Select all
if __WPL.FileName == "zright_gate" then
CurrentGatePosition = Right
elseif __WPL.FileName == "zleft_gate" then
CurrentGatePosition = Left
elseif __WPL.FileName == "zmiddle_gate" then
CurrentGatePosition = Middle
end
is the code I have atm, but im trying to put 1 of those values (right, middle, left) into another variable depending on what waypoint is loaded. So Rightsection if right_gate is loaded etc.
And also why doesn't sendMacro("/p Our "..CurrentGatePosition.." section is under attack!") work? returns
Code: Select all
10:28pm - ...romacro/scripts/rom/userfunctions/userfunction_buffs.lua:64: attemp
t to concatenate global 'CurrentGatePosition' (a nil value)
even though I had declaired its value already.
Re: Setting variables
Posted: Thu Feb 06, 2014 6:57 am
by rock5
Lua doesn't have an equivalent to the "case" command that some other languages have. That said, there are some funny tricks you can do with the "and" and "or" commands. In this case you could do
Code: Select all
CurrentPosition = __WPL.FileName == "Right" and Right or
__WPL.FileName == "Left" and Left or
__WPL.FileName == "Middle" and Middle
But I would look at maybe naming things so that you can do something like what Lisa said.
Or maybe extracting part of the file name and using that directly.
It might be too much to explain why this works. You may have to read up elsewhere if you want to know. But basically lua 'and' and 'or' returns the last value when they get a true value, ie. not nil or false. So for that whole statement to be true then one line needs to be true (because they are separated by 'or' statements). For one line to be true both sides of the 'and' needs to be true, ie. not false or nil. So the first line where both parts are true will be the last line that it checks and the last value it checks will be the value on the right. So that is what it returns and assigns to CurrentPosition.
ZZZZZ wrote:And also why doesn't sendMacro("/p Our "..CurrentGatePosition.." section is under attack!") work?
It tells you exactly why it doesn't work. It's up to you to figure out why it's nil. A few things to check are; Did you spell it the same? Is it in the same scope ie. did you declare it local in another section? When you assigned a value to it, are you sure that it worked and didn't actually assigned nil to it?
Re: Setting variables
Posted: Thu Feb 06, 2014 7:13 am
by lisa
its a string thing
Middle without the "" is not a string and so if it hasn't been defined already, such as
Middle = "Middle"
then the value of Middle will be nil.
you probably want.
Re: Setting variables
Posted: Thu Feb 06, 2014 7:53 am
by ZZZZZ
Right, im half asleep, didn't even think to check if it was in " " ...
As for the CurrentPosition, the elseif's I use work, its just is there any way to 'merge' the value it returns with another variable? Eg i have variables RightGate20 and LeftGate40 (A lot more, but these are just a few) and I want to put the 'CurrentPosition' value in front of simply Gate20 to save using a lootttttt of elseif's, is that possible?
Re: Setting variables
Posted: Thu Feb 06, 2014 8:18 am
by noobbotter
Doing that should be as simple as, after you have CurrentGatePosition set to "Right" or "Left" or whatever it is, then do
Code: Select all
CurrentGatePosition = CurrentGatePosition.."Gate20"
Doing that, if CurrentGatePosition was equal to "Left" then after running that line it will make CurrentGatePosition equal "LeftGate20".
Or to save yourself a step, if you use CurrentPosition = __WPL.FileName as Lisa mentioned, then you could just do
Code: Select all
CurrentPosition = __WPL.FileName.."Gate20"
Re: Setting variables
Posted: Thu Feb 06, 2014 8:26 am
by lisa
the simplest way to use stuff like this is to use a table.
You need to have your variables already defined
Middle = "stufff" Left = "other stuff" Right = "weird stuff"
then have a table after those variables have been declared
vartables = {zright_gate = Right,zmiddle_gate = Middle,zleft_gate = Left}
then you can do
vartables[__WPL.FileName]
Example of working code looks like this
Code: Select all
Command>
Middle = "stufff"
Left = "other stuff"
Right = "weird stuff"
vartables= {zright_gate = Right,zmiddle_gate = Middle,zleft_gate = Left}
print(vartables["zmiddle_gate"])
stufff
You need to be very careful with the exact names and the usage of " "
--=== Added ===--
Adding this cus noobbotter posted before me.
What he said it true but the value will still be just a string and not the variable you want it to be.
__WPL.FileName.."Gate20"
that will be a string with the combination of the 2 things, ie
"LeftGate20"
and the string won't be the value of the variable that you want it to be.