Alright, this might seem simple, but I'm confused.
Basically what I want to do, is make a function which dynamically takes in 2 variables of my choosing, modifies them, and saves the variables.
My current code, simplified:
[lua]function ENT:DoStuff()
self.Variable1 = 10
self.Variable2 = 20
self.Entity:ModVars( self.Variable1, self.Variable2 )
end
function ENT:ModVars( var1, var2 )
var1 = var1 - 2
var2 = var2 + 2
end[/lua]
What I want in the second function is for it to take in self.Variable1 and self.Variable2, or any other variable of my choosing, and modify the variables themselves, rather than creating the two local variables (var1 and var2) and modifying them locally, I want it to actually change the entities variables.
The problem I have is that I want to be able to insert basically any variable I want into ModVars(), and have it set the variable. Not just a local one.
That's not possible in Lua. You'll either want to use a table or return the values.
Noooooooooooo.
:saddowns:
Don't really see why you would need that, it's really not a clean approach.
[lua]function ModVars(env1, var1, env2, var2)
env1[var1] = env1[var1] - 2
env2[var2] = env2[var2] + 2
end
function ENT:DoStuff()
self.Variable1 = 10
self.Variable2 = 20
ModVars(self, "Variable1", self, "Variable2")
end
GlobalVariable1 = 10
GlobalVariable2 = 20
ModVars(getfenv(), "GlobalVariable1", getfenv(), "GlobalVariable2")[/lua]
That's probably the best you can do. Maybe you can implement kind of pointers using epic debug and metatable hacks, but I don't think it would be worth it, you just better find a cleaner method for doing what you need. Accessing variables like that is usually a really bad idea, use tables or something.
Also for god's sake, "self.Entity" is the same as "self". Writing "self.Entity" just makes everything more confusing because you will never know when you should write "self", and when you should write "self.Entity".
It's not that I want to make a messy code or a workaround, it's just that I'm going to be a lot of modifying variables in pairs under different situations and I don't want to have to basically double the size of my script with code which is essentially the same.
Also, kilburn, I taught myself lua from gmod, so if i have bad coding practices I'm sorry :saddowns:
Already mentioned before but this will probably be the cleanest/simplest approach:
[lua]
local var1 = 2
local var2 = 3
function ModVars( var1, var2 )
var1 = var1 - 2
var2 = var2 + 2
return var1, var2
end
var1, var2 = ModVars( var1, var2 )
[/lua]
For some reason, when I do this:
[lua]function SWEP:Learn(stat1,stat2)
print(stat1)
if stat1 <= 99 and stat2 >= 1 then
stat1 = stat1+1
stat2 = stat2-1
end
return stat1, stat2
end[/lua]
It tells me I am trying to compare a string with a number on line 3.
But the variables I'm giving it are numbers...
Nowhere are they strings, and they work perfectly fine in other parts of the script.
And when I specify != rather than <= it accepts that.
[QUOTE=Empty_Shadow;19912500]And when I specify != rather than <= it accepts that.[/QUOTE]
It's because lua automatically converts strings and numbers when they're used for arithmetic operations, however the behaviour for comparison is undefined. Should it be comparing them as Strings or as Numbers. As numbers 12 > 6, but with strings "6" > "12". So instead of picking one seemingly at random it gives you an error instead.
Equivalence checking is a lot more simple as 12 == 6 gives the same result as "12" == "6", so logically ~= will work as well.
Sorry, you need to Log In to post a reply to this thread.