Change alpha of gui from script (SOLVED)

Hi.
I make menu in my game. I made points of menu in gui. When i click New Game gui must dissapear. I think it can be done by changing of
transparency or something else.
How can I do it?

1 Like

I believe what youā€™re looking for is

msg.post(".", ā€œdisableā€)

and

msg.post(".", ā€œenableā€)

3 Likes

Thanks. It helped me.

2 Likes

You usually donā€™t want to hide things by setting transparency to zero since it will still be rendered. Itā€™s better to disable the component like @Klear suggested.

3 Likes

Hi @britzl,

Could I please have an example of this as I am trying to do the same?
I keep getting the error of:

--ERROR:GAMEOBJECT: Instance '<unknown>' could not be found when dispatching message 'enable' sent from main:/Button#Button

Here is my code:

function init(self)
	msg.post(".", "acquire_input_focus")
	msg.post('Island', "disable")
end

function on_input(self, action_id, action)
	if action_id == hash("Mouse") and action.pressed then
		local Button = gui.get_node("Button")
		if gui.pick_node(Button, action.x, action.y) then
			msg.post('Island', "enable")
		end
	end
end

Thanks,

Badger.

Do you have a game object with id ā€œIslandā€ that is on the same ā€œlevelā€ of the scene graph as the game object that the script making the call is on?

1 Like

You need to specify the url of the component where you want to send the message, most likely

msg.post("#Island", "enable")

You can get the full url by clicking the object in the outline on the right side of the screen when you open the collection which contains it.

Details on how message passing works can be found here.

2 Likes

That still doesnā€™t make a difference. The two images are separate box nodes in one GUI. Do I need to make another GUI and link the same script to it?

Oh, so it is an individual node, not the entire gui you want to hide? The original question was to hide an entire gui component. To hide a single node:

local node_to_hide = gui.get_node("Island")
gui.set_enabled(node_to_hide, false)
4 Likes

This appears to almost be working. However, I get the error of

ERROR:SCRIPT: /main/Scripts/Button.gui_script:13: bad argument #1 to 'set_enabled' (user data expected, got nil)

Here are the changes I have made to my code:

function init(self)
	msg.post(".", "acquire_input_focus")
	local node_to_appear = gui.get_node("Island")
	gui.set_enabled(node_to_appear, false)
	local node_to_hide = gui.get_node("Button")
	gui.set_enabled(node_to_hide, true) 
end

function on_input(self, action_id, action)
	if action_id == hash("Mouse") and action.pressed then
		local Button = gui.get_node("Button")
		if gui.pick_node(Button, action.x, action.y) then
			gui.set_enabled(node_to_appear, true)
			gui.set_enabled(node_to_hide, false)
		end
	end
end

I would like the button to disappear when pressed and also have the island appear.

The nodes_to_appear and nodes_to_hide are not deffined in the scope of the on_input()

Instead put the references to the nodes in self

function init(self)
	msg.post(".", "acquire_input_focus")
	self.node_to_appear = gui.get_node("Island")
	gui.set_enabled(node_to_appear, false)
	self.node_to_hide = gui.get_node("Button")
	gui.set_enabled(node_to_hide, true) 
end

function on_input(self, action_id, action)
	if action_id == hash("Mouse") and action.pressed then
		if gui.pick_node(self.node_to_hide, action.x, action.y) then
			gui.set_enabled(self.node_to_appear, true)
			gui.set_enabled(self.node_to_hide , false)
		end
	end
end
3 Likes

How would I do this? Sorry Iā€™m new to defold

Mattias modified your code. See his answer.

self.node_to_appear = gui.get_node(ā€œIslandā€)

etc

1 Like

I have copied the code to a T and now I get more errors and when I build the project, this happens:

Island%20infront%20of%20button

The errors I now get:

ERROR:GAMESYS: Error when initializing GUI component: -2.
ERROR:SCRIPT: /main/Scripts/Button.gui_script:11: bad argument #1 to 'pick_node' (userdata expected, got nil)
1 Like

Could you verify that the ids of your nodes are really Button and Island

14

1 Like

Button%20ID%20proof

Island%20ID%20proof

@britzl is this it?

function init(self)
	msg.post(".", "acquire_input_focus")
	self.node_to_appear = gui.get_node("Island")
	gui.set_enabled(node_to_appear, false)
	self.node_to_hide = gui.get_node("Button")
	gui.set_enabled(node_to_hide, true) 
end

function on_input(self, action_id, action)
	if action_id == hash("Mouse") and action.pressed then
		if gui.pick_node(self.node_to_hide, action.x, action.y) then
			gui.set_enabled(self.node_to_appear, true)
			gui.set_enabled(self.node_to_hide , false)
		end
	end
end
1 Like

Please share the current state of your code as well.

1 Like

You forgot to add the self. to the node ids in the gui.set_enable().

Try this instead

function init(self)
	msg.post(".", "acquire_input_focus")
	self.node_to_appear = gui.get_node("Island")
	gui.set_enabled(self.node_to_appear, false)
	self.node_to_hide = gui.get_node("Button")
	gui.set_enabled(self.node_to_hide, true) 
end
1 Like

Yes!!! It works!!! Thank you so much for your help @britzl, @Mattias_Hedberg and @Klear :hugs:

It works just like a charm now. Sorry for being a noob with this. :sweat_smile:

2 Likes

The error message might look unfamiliar to new programmers, but it contains a lot of useful information.

This error message means that the first argument of the pick_node() function is nil. And, since it prints out the file and line number ( /main/Scripts/Button.gui_script:11), you can go there and find the variable that is nil.

The next step is then to ask: ā€œWhy is this variable nil?ā€

5 Likes