Setting variables

Runes of Magic/Radiant Arcana (http://www.runesofmagic.com)
Post Reply
Message
Author
ZZZZZ
Posts: 513
Joined: Sun Oct 21, 2012 1:42 am

Setting variables

#1 Post 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.
User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: Setting variables

#2 Post 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

Code: Select all

CurrentPosition = __WPL.FileName
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual
ZZZZZ
Posts: 513
Joined: Sun Oct 21, 2012 1:42 am

Re: Setting variables

#3 Post 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.
User avatar
rock5
Posts: 12173
Joined: Tue Jan 05, 2010 3:30 am
Location: Australia

Re: Setting variables

#4 Post 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.

Code: Select all

CurrentPosition = __WPL.FileName
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?
  • Please consider making a small donation to me to support my continued contributions to the bot and this forum. Thank you. Donate
  • I check all posts before reading PMs. So if you want a fast reply, don't PM me but post a topic instead. PM me for private or personal topics only.
  • How to: copy and paste in micromacro
    ________________________
    Quote:
    • “They say hard work never hurt anybody, but I figure, why take the chance.”
          • Ronald Reagan
User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: Setting variables

#5 Post by lisa »

its a string thing

Code: Select all

CurrentGatePosition = Middle
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.

Code: Select all

CurrentGatePosition = "Middle"
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual
ZZZZZ
Posts: 513
Joined: Sun Oct 21, 2012 1:42 am

Re: Setting variables

#6 Post 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?
noobbotter
Posts: 527
Joined: Fri Aug 31, 2012 1:15 pm

Re: Setting variables

#7 Post 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"
User avatar
lisa
Posts: 8332
Joined: Tue Nov 09, 2010 11:46 pm
Location: Australia

Re: Setting variables

#8 Post 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.
Remember no matter you do in life to always have a little fun while you are at it ;)

wiki here http://www.solarstrike.net/wiki/index.php?title=Manual
Post Reply