This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Node not found error [solved]

BatteryBattery Posts: 26Member
edited August 2016 in Programming
Hey,<br /><br />I've got my character set up in it's own scene (imported as an instance in the main scene) with a script attached to control the character nodes. However, when I call get_node("path/to/node") in the script I get a node not found error even though the node exists and the path is correct. I've littered my functions with null checks:<br />
<br /><br />func set_eyes(new_value):<br />	if get_node(&quot;Eyes&quot;) != null:<br />		eye_colour = new_value<br />		eye_offset = eye_types[eye_colour]<br />		get_node(&quot;Eyes&quot;).set_frame(eye_offset + direction_offset)
<br />And the script works fine and suppresses all but the first error message for each node I'm trying to fetch. The example function is called both in the game and as a setget. I figure I must be doing something wrong because there must be a way to call get_node() without getting an error and without requiring null checks. <br /><br /><br />Thanks in advance!

Tags :

Comments

  • RossRoss Posts: 199Member
    Are these get_nodes called during the ready function by any chance? The ready function generally fires twice, and the first time is before any of the nodes are initialized.
  • AkienAkien Posts: 70Member Godot Leader
    on 1465177192:
    <br />Are these get_nodes called during the ready function by any chance? The ready function generally fires twice, and the first time is before any of the nodes are initialized.<br />
    <br />Small correction, the [tt]_ready[/tt] function does not fire twice, only once when the node enters (or reenters) the scene tree (and after all newly entered nodes have called their [tt]_enter_tree[/tt] callback if any).<br /><br />But indeed, if this [tt]set_eyes[/tt] function is called during [tt]_ready[/tt], it can be that the "Eyes" child node hasn't been instantiated yet, and thus the code fails.
  • BatteryBattery Posts: 26Member
    Thanks for the info. <br /><br />Unfortunately that doesn't seem to fix it. It makes no difference if the the function is called in [tt]_ready[/tt] or not, I still get the error. It's not game breaking, as it only fails on the first loop, then works as it should. <br /><br />From what you've both said, I guess it's being called early, but the curious thing is that the function isn't called until the player presses a key and nowhere else is calling it. <br /><br />Could it be because [tt]set_eyes[/tt] is also a tool function used with setget? So it gets called when the game starts?<br /><br />Here's the full script:<br /><br />
    <br />tool<br />extends Node2D<br /><br />export(String, &quot;Down&quot;, &quot;Left&quot;, &quot;Right&quot;, &quot;Up&quot;) var facing = &quot;Down&quot; setget set_direction<br />export(String, &quot;White&quot;, &quot;Pale&quot;, &quot;Brown&quot;, &quot;Orange&quot;, &quot;Grey&quot;, &quot;Blue&quot;, &quot;Green&quot;, &quot;Red&quot;) var skin_colour = &quot;White&quot; setget set_skin_colour<br />export(String, &quot;Blue&quot;, &quot;Green&quot;, &quot;Brown&quot;, &quot;Narrow&quot;, &quot;Deamon&quot;, &quot;Blood&quot;, &quot;AnimalYellow&quot;, &quot;AnimalGreen&quot;) var eye_colour = &quot;Blue&quot; setget set_eyes<br />export(String, &quot;None&quot;, &quot;Long&quot;, &quot;Medium&quot;, &quot;Short&quot;, &quot;Large&quot;) var beard_style = &quot;None&quot; setget set_beard_style<br />export(String, &quot;Grey&quot;, &quot;Blonde&quot;, &quot;Black&quot;, &quot;Brown&quot;, &quot;Ginger&quot;, &quot;Red&quot;, &quot;Green&quot;, &quot;Blue&quot;) var hair_colour = &quot;Grey&quot; setget set_hair_colour<br />export var wrinkles = false setget set_wrinkles<br /><br />var skin_colour_offsets = {<br />	White = 0,<br />	Pale = 4,<br />	Brown = 8,<br />	Orange = 12,<br />	Grey = 16,<br />	Blue = 20,<br />	Green = 24,<br />	Red = 28,<br />	}<br /><br />var eye_types = {<br />	Blue = 64,<br />	Green = 68,<br />	Brown = 72,<br />	Narrow = 76,<br />	Deamon = 80,<br />	Blood = 84,<br />	AnimalYellow = 88,<br />	AnimalGreen = 92,<br />	}<br /><br />var facial_hair_offsets = {<br />	Long = 96,<br />	Medium = 128,<br />	Short = 160,<br />	Large = 192,<br />	}<br /><br />var hair_colour_offsets = {<br />	Grey = 0,<br />	Blonde = 4,<br />	Black = 8,<br />	Brown = 12,<br />	Ginger = 16,<br />	Red = 20,<br />	Green = 24,<br />	Blue = 28<br />	}<br /><br />var head_offset = 0<br />var wrinkles_offset = 32 <br />var eye_offset = 64<br />var direction_offset = 0<br />var beard_offset = 96<br />var hair_colour_offset = 0<br /><br />var current_animation = null<br />var new_animation = null<br /><br />func _ready():<br />	pass<br /><br />func set_beard_style(new_value):<br />	if get_node(&quot;Beard&quot;) != null:<br />		print(&quot;setting beard&quot;)<br />		beard_style = new_value<br />		if beard_style == &quot;None&quot;:<br />			get_node(&quot;Beard&quot;).hide()<br />			return<br />		beard_offset = facial_hair_offsets[beard_style]<br />		get_node(&quot;Beard&quot;).set_frame(beard_offset + hair_colour_offset + direction_offset)<br />		get_node(&quot;Beard&quot;).show()<br />		<br />func set_hair_colour(new_value):<br />	if get_node(&quot;Beard&quot;) != null:<br />		hair_colour = new_value<br />		hair_colour_offset = hair_colour_offsets[hair_colour]<br />		set_beard_style(beard_style)<br /><br />func set_skin_colour(new_value):<br />	if get_node(&quot;Head&quot;) != null:<br />		skin_colour = new_value<br />		head_offset = skin_colour_offsets[skin_colour]<br />		get_node(&quot;Head&quot;).set_frame(head_offset + direction_offset)<br />		get_node(&quot;Wrinkles&quot;).set_frame(wrinkles_offset + head_offset + direction_offset)<br /><br />func set_eyes(new_value):<br />	if get_node(&quot;Eyes&quot;) != null:<br />		eye_colour = new_value<br />		eye_offset = eye_types[eye_colour]<br />		get_node(&quot;Eyes&quot;).set_frame(eye_offset + direction_offset)<br /><br />func set_direction(new_value):<br />	if get_node(&quot;Animation&quot;) != null and get_node(&quot;Eyes&quot;) != null and get_node(&quot;Head&quot;) != null:<br />		facing = new_value<br />		if facing == &quot;Down&quot; and direction_offset != 0:<br />			direction_offset = 0<br />			get_node(&quot;BackFacing&quot;).hide()<br />			get_node(&quot;FrontFacing&quot;).show()<br />			get_node(&quot;SideFacing&quot;).hide()<br />			<br />		elif facing == &quot;Left&quot; and direction_offset != 1:<br />			direction_offset = 1<br />			get_node(&quot;SideFacing&quot;).set_scale(Vector2(1,1))<br />			get_node(&quot;BackFacing&quot;).hide()<br />			get_node(&quot;FrontFacing&quot;).hide()<br />			get_node(&quot;SideFacing&quot;).show()<br />			var sword = get_node(&quot;SideFacing/FrontArmUpper/FrontArmLower/Sword&quot;)<br />			get_node(&quot;SideFacing/FrontArmUpper/FrontArmLower&quot;).remove_child(sword)<br />			get_node(&quot;SideFacing/BackArmUpper/BackArmLower&quot;).add_child(sword)<br />		elif facing == &quot;Right&quot; and direction_offset != 2:<br />			direction_offset = 2<br />			get_node(&quot;SideFacing&quot;).set_scale(Vector2(-1,1))<br />			get_node(&quot;BackFacing&quot;).hide()<br />			get_node(&quot;FrontFacing&quot;).hide()<br />			get_node(&quot;SideFacing&quot;).show()<br />			var sword = get_node(&quot;SideFacing/BackArmUpper/BackArmLower/Sword&quot;)<br />			get_node(&quot;SideFacing/BackArmUpper/BackArmLower&quot;).remove_child(sword)<br />			get_node(&quot;SideFacing/FrontArmUpper/FrontArmLower&quot;).add_child(sword)<br />		elif facing == &quot;Up&quot; and direction_offset != 3:<br />			direction_offset = 3<br />			get_node(&quot;BackFacing&quot;).show()<br />			get_node(&quot;FrontFacing&quot;).hide()<br />			get_node(&quot;SideFacing&quot;).hide()<br /><br />		get_node(&quot;Head&quot;).set_frame(head_offset + direction_offset)<br />		get_node(&quot;Wrinkles&quot;).set_frame(wrinkles_offset + head_offset + direction_offset)<br />		get_node(&quot;Eyes&quot;).set_frame(eye_offset + direction_offset)<br />		if beard_style != &quot;None&quot;:<br />			get_node(&quot;Beard&quot;).set_frame(beard_offset + hair_colour_offset + direction_offset)<br /><br />func set_wrinkles(new_value):<br />	if get_node(&quot;Wrinkles&quot;) != null:<br />		wrinkles = new_value<br />		if wrinkles:<br />			get_node(&quot;Wrinkles&quot;).show()<br />		else:<br />			get_node(&quot;Wrinkles&quot;).hide()<br />
    
  • bitwesbitwes Posts: 74Member
    As the other posters have said, the timing is off on that first call.  For some reason the node isn't there but by the time any subsequent calls are made the node IS there.  I'm not sure about the timing issue, but to get rid of the error use [tt]has_node[/tt] instead of [tt]get_node[/tt] and then checking for [tt]null[/tt]
  • BatteryBattery Posts: 26Member
    Odd, all the other scripts work fine, just that one.<br /><br />Anyway, [tt]has_node()[/tt] works a charm, no more error messages! Thanks!  :D

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.