Changing/Reading variables in LUA

Ok so the title might not exactly fit what I’m trying to say but in the event it doesn’t let me explain:

Right now I’m working with Derma combo boxes. I’m trying to make it so if for example, I click the option “DUCK”, it makes the variable Animal = “DUCK”. This is probably not the most efficient way to do something, but it’s the only way I can think of doing it right now. To get a little more specific: I want the option I choose in a ComboBox to change a variable.

I think I’ve got the bit with setting the variable to “DUCK” down, but I can’t know for sure because when I try to make an if then statement with it *, I get lua errors about how the then should be next to the =.

*Example code


if ANIMAL = ("DUCK") then
--Stuff
end

Is there a better way to do this? The reason I want to set the variable is because I want to temporarily save that tiny bit of data so when I run the menu close, It can apply certain things based on what the variable is. In this example, if I chose duck, it would change me to a duck after I closed the menu.

And no, I’m not making something in relation to ducks.

[lua]DComboBox.OnSelect = function( panel, index, value, data )
Animal = data
end[/lua]
When they select Duck, your variable will be set to Duck.

You’re going to have to use the “OnSelect” function that comes with the DComboBox. You can easily get the selected option with a parameter.

[lua]DComboBox.OnSelect = function(panel, index, value, data)
Animal = data
end[/lua]

Damnit SaintSin6.

I already had that. My issue is getting if then statements to work with that.

What I have so far in terms of writing/reading the variables. (Ignore the Msg(“Hi”). I was using that for testing.)


Primarym.OnSelect = function( panel, index, value, data )
	Gun = (data)
end

if GUN = ("AK47") then
Msg("Hi")
end

A single = is an assignment operator. When comparing values, use ==. Also, Lua is case sensitive. So GUN is not the same as Gun.

[lua]
Primarym.OnSelect = function( panel, index, value, data )
Gun = (data)
end

if Gun == (“AK47”) then
Msg(“Hi”)
end[/lua]

if Gun == “AK47” then

There is no reason to put () around “AK47” (probably doesn’t hurt though).
You do need to use == to check for equality though, = is the assignment operator.

Oh thank god I’ve finally gotten it where I want to it be. Thanks!

Bump/double post (Assuming this doesn’t merge).

Now I have the issue of the if then statements always assuming they’re correct. What I mean:


	if Gun == (AK47) then
		RunConsoleCommand("PAK47")
	elseif Gun != AK47 then
		Msg("Not an AK!")
	end

It immediately assumes that Gun is equal to AK47 even when it’s not.

I’ve also tried using elseif Gun == (What ever other weapon I would use). Only the first if statement does anything and it does the same as above.

[lua]
if wepClass == “weapon_ak47” then
–its an ak
else
–its not
end
[/lua]