# Gui.try\_get\_node that doesn't throw an error when node is not found

**URL:** <https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266>\
**Category:** Questions\
**Created:** [September 26, 2018, 12:44pm UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266 "2018-09-26T12:44:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmalijonis](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jmalijonis/32/18870_2.png) [@jmalijonis](https://forum.defold.com/u/jmalijonis)\
**Post date:** [September 26, 2018, 12:44pm UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/1 "2018-09-26T12:44:32Z")

</div>

Would it be possible to add a variant on gui.get\_node so that when it fails to find a node it returns nil instead of throwing an error?

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [September 26, 2018, 12:47pm UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/2 "2018-09-26T12:47:19Z")

</div>

Can you describe the use case where you need such a function?

---

<div class="post-metadata">

**Author:** ![jmalijonis](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jmalijonis/32/18870_2.png) [@jmalijonis](https://forum.defold.com/u/jmalijonis)\
**Post date:** [September 26, 2018, 1:03pm UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/3 "2018-09-26T13:03:09Z")

</div>

A collection of animated buttons that each differ slightly by the components they are composed of, but each component has a specific animation. A bunch of these button pack guis (think pop-quiz) all ran by a single gui\_script. You access the count of these buttons then you iterate through all of them and try to animate every component appropriately, if the component isn’t there, you should be able elegantly to move on. This is solvable by having a table that describes every button in every button pack, but then editing actual gui has to be coupled with editing the table that describes those buttons.

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [September 26, 2018, 1:30pm UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/4 "2018-09-26T13:30:16Z")

</div>

Could you try wrapping the call in a lua pcall, something like;

```
local exists = pcall(gui.get_node, "some_node_id")
if exists then
	pprint("Node exists")
	--do stuff
else
	pprint("Node does not exist, carry on..")
end

```

---

<div class="post-metadata">

**Author:** ![Klear](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/klear/32/11577_2.png) [@Klear](https://forum.defold.com/u/Klear)\
**Post date:** [September 26, 2018, 1:33pm UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/5 "2018-09-26T13:33:32Z")

</div>

> [@Johan\_Beck-Noren](#):
>
> pcall

I need to remember this. Might come in handy.

---

<div class="post-metadata">

**Author:** ![Johan\_Beck-Noren](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan_beck-noren/32/2622_2.png) [@Johan\_Beck-Noren](https://forum.defold.com/u/Johan_Beck-Noren)\
**Post date:** [September 26, 2018, 1:38pm UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/6 "2018-09-26T13:38:13Z")

</div>

From the Lua manual; [Lua 5.3 Reference Manual](https://www.lua.org/manual/5.3/manual.html#pdf-pcall)

Reading the manual you should actually be able to do something like

```
local exists, node = pcall(gui.get_node, "some_node_id")

```

If the node exists, `node` will hold a ref to the node, otherwise it will contain the error string.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [September 26, 2018, 5:38pm UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/7 "2018-09-26T17:38:11Z")

</div>

pcall would solve the problem but I still don’t really understand the need to check for the node? It’s not like the collection of animated buttons is created completely at random and outside of the developers control. A data driven design would allow you to know for which buttons that there are animations and how to animate them.

---

<div class="post-metadata">

**Author:** ![jmalijonis](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/jmalijonis/32/18870_2.png) [@jmalijonis](https://forum.defold.com/u/jmalijonis)\
**Post date:** [September 27, 2018, 6:55am UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/8 "2018-09-27T06:55:53Z")

</div>

Each collection of buttons is created by hand in editor (not by code). From this comes the risk of desync I mentioned, where appropriate tables become meta-data that needs to be updated manually. This would be ok if gui items could have go properties. At least the data could be in a single place then, but otherwise it’s sspecially risky if you imagine a development pipeline with multiple developers where one developer group has only keyboards and another has only mouses (sort of comedic, but a good generalization of developer roles/skills sometimes). So there is no NEED in that this issue can be worked around. Anyway, it’s already solved so the feature exists.

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [September 27, 2018, 7:15am UTC](https://forum.defold.com/t/gui-try-get-node-that-doesnt-throw-an-error-when-node-is-not-found/30266/9 "2018-09-27T07:15:55Z")

</div>

An option is to run a python (or what have you) script and gather the relevant button setups and save it as a Lua table. Check out [DefTree: A python module for editing Defold files](https://forum.defold.com/t/deftree-a-python-module-for-editing-defold-files/15210)
