Derma: Buttons have the same variable name and that's okay?

Still learning the basics but this doesn’t seem to make sense. Someone helped me make a menu with 2 buttons on it. I copied one of these buttons right below it so I would have 3. I changed the position and in the game it displayed 3 buttons. Success!..but this doesn’t make sense. I know I am remaking the variable but shouldn’t that lead to problems. If i had 20 buttons that all had the variable name; I just don’t quite get how that wouldn’t cause issues.

If you do this:




local mybut = vgui.Create("DButton")
mybut:Etc()

local mybut = vgui.Create("DButton")
mybut:Etc()

local mybut = vgui.Create("DButton")
mybut:Etc()

local mybut = vgui.Create("DButton")
mybut:Etc()



You simply will not be able to modify those previous buttons with the variable name “mybut”.

mybut:SetText(“wdad”) Will only modify the last created button.

Basically even if you have



local a = ...
local a = ...


it’s actually two different variables. The second simply shadows the first because it has same name but they’re still independent of each other.

No, it’s not two different variables, its one variable that gets overwritten.

EDIT: Actually for local variables that might be true.

When in doubt, test.



local a = 5
local a = 8

print(debug.getlocal(1, 1))
print(debug.getlocal(1, 2))


Output:



a	5
a	8


But there’s really no reason to do this.