Jump to content

(Help) Gui / guiSetVisible


Recommended Posts

Hi, I have about 50 or more gui-images to make visible / invisible but im getting all errors - @'guiSetVisible' expected element at argument 1, got string "button_1_img"
I need to use a loop to concat and change button numbers 1-50. How to actually make this work?

-- button elements
button_1_img = guiCreateStaticImage ( 500, 500, 32, 32, "img/buttons/button_select.png", false, main_window )
button_2_img = guiCreateStaticImage ( 550, 500, 32, 32, "img/buttons/button_select.png", false, main_window )
button_3_img = guiCreateStaticImage ( 600, 500, 32, 32, "img/buttons/button_select.png", false, main_window )
button_4_img = guiCreateStaticImage ( 650, 500, 32, 32, "img/buttons/button_select.png", false, main_window )
button_5_img = guiCreateStaticImage ( 700, 500, 32, 32, "img/buttons/button_select.png", false, main_window )
-- + 45

for index=1,50 do

	guiSetVisible ( "button_"..index.."_img", true )
end

 

Link to comment
  • Moderators
1 hour ago, Simon54 said:

How to actually make this work?

Normally you use a table.

But if you aren't able to work with those yet. Here is a very dirty way of doing it:

for index=1,50 do
	guiSetVisible ( _G["button_"..index.."_img"], true )
end

The _G accesses global variables. (variables without the keyword local in front of them)

⚠️ It is not recommended to use this method more than once. It is purely to demonstrate how to do it your way. But not the way Lua intended it to be used ⚠️

 

For working with tables, please take a look at:

 

  • Like 1
  • Thanks 1
Link to comment
  • Moderators
3 hours ago, Tekken said:

Hi, may I ask why is this a bad implementation ?

 

I can think of a few, no actual numbers to back this up to be honest.

  • The memory access speed. While _G is also a table, the access method is done with a string instead of an integer.
    • There are a lot of items in the _G table, adding a lot of extra items:
      • Will slow down the loopup with the _G table.
      • And might slowdown the lookup time for globals in general. (no proof/numbers here to be honest)
         
    • And on top of that I am not sure if _G (runtime) lookup speed can actually be optimised by the CPU. It is not a regular table after all.
       
  • Making collision/override possible, which is very easy to do in this case. Creating bugs in your code without knowing it.

 

Link to comment

Thanks IIYAMA, the dirty way does what I need it to do and it really saved a ton of lines in the code. I am however using it more than once in the same script, I think as long as it doesn't cause any major performance or security implications it should be okay until I figure out how to go about doing it the table way. Thanks again.

  • Like 1
Link to comment
1 hour ago, Simon54 said:

Thanks IIYAMA, the dirty way does what I need it to do and it really saved a ton of lines in the code. I am however using it more than once in the same script, I think as long as it doesn't cause any major performance or security implications it should be okay until I figure out how to go about doing it the table way. Thanks again.

The "table way" is actually pretty straight forward, here is a simple example:

 

local guiStuff = {}; --create the table.

guiStuff[ #guiStuff + 1 ] = guiCreateStaticImage("IMAGE1", x, y); --#guiStuff > gets the total number of items in the table and we add one so basically we do [0 + 1] then [1 + 1], [2 + 1] and so on..
guiStuff[ #guiStuff + 1 ] = guiCreateStaticImage("IMAGE2", x, y);
guiStuff[ #guiStuff + 1 ] = guiCreateStaticImage("IMAGE3", x, y);
guiStuff[ #guiStuff + 1 ] = guiCreateStaticImage("IMAGE4", x, y);
--so on as many times you want....


for index = 1, #guiStuff do
  guiSetVisible(guiStuff[index], true);
end

--you can also acces any of the above like this
guiSetVisible(guiStuff[3], false);
--if you are sure of the index

--imagine the table like this:
local guiStuff = {
	[1] = guiCreateStaticImage("IMAGE1", x, y),
	[2] = guiCreateStaticImage("IMAGE2", x, y),
	[3] = guiCreateStaticImage("IMAGE3", x, y),
	[4] = guiCreateStaticImage("IMAGE4", x, y),
};

 

 

  • Thanks 1
Link to comment
1 hour ago, Tekken said:

The "table way" is actually pretty straight forward, here is a simple example:

 

local guiStuff = {}; --create the table.

guiStuff[ #guiStuff + 1 ] = guiCreateStaticImage("IMAGE1", x, y); --#guiStuff > gets the total number of items in the table and we add one so basically we do [0 + 1] then [1 + 1], [2 + 1] and so on..
guiStuff[ #guiStuff + 1 ] = guiCreateStaticImage("IMAGE2", x, y);
guiStuff[ #guiStuff + 1 ] = guiCreateStaticImage("IMAGE3", x, y);
guiStuff[ #guiStuff + 1 ] = guiCreateStaticImage("IMAGE4", x, y);
--so on as many times you want....


for index = 1, #guiStuff do
  guiSetVisible(guiStuff[index], true);
end

--you can also acces any of the above like this
guiSetVisible(guiStuff[3], false);
--if you are sure of the index

--imagine the table like this:
local guiStuff = {
	[1] = guiCreateStaticImage("IMAGE1", x, y),
	[2] = guiCreateStaticImage("IMAGE2", x, y),
	[3] = guiCreateStaticImage("IMAGE3", x, y),
	[4] = guiCreateStaticImage("IMAGE4", x, y),
};

 

 

Okay perfect, this is also a great example, easy to understand. Thanks Tekken I really appreciate the help!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...