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?
I believe what youāre looking for is
msg.post(".", ādisableā)
and
msg.post(".", āenableā)
Thanks. It helped me.
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.
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?
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.
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)
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
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
I have copied the code to a T and now I get more errors and when I build the project, this happens:
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)
Could you verify that the ids of your nodes are really Button
and Island
@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
Please share the current state of your code as well.
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
Yes!!! It works!!! Thank you so much for your help @britzl, @Mattias_Hedberg and @Klear
It works just like a charm now. Sorry for being a noob with this.
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
?ā