I am fairly new to Godot and have been trying to make a centipede clone, but I keep running into an issue. To get it to work (with my implementation) I need to create a sibling node that is the same as the one that made it, but with a different value in a length variable. I have seemingly tried everything but signals so far, and haven’t found a way to parse the information to the node when I instantiate it. My current issue is that when I try to call a custom “func instantiate” it refuses to recognize it, despite the function itself working and being able to set properties such as position just fine. Current attempt is below, and it all occurs in this one script as I am instantiating the same node that creates the instance.
Also, I am aware of the small inefficiencies, I just want to get this working.
extends Area2D
@export var length : int = 12
@onready var segment_scene = preload("res://project_files/centipede/scenes/centipede_segment.tscn")
func _ready():
var segment = segment_scene.instantiate()
get_parent().add_child(segment)
segment.instantiate(length, position.x, position.y)
#when I run this line, I get the error:
#Invalid call. Nonexistent function 'instantiate' in base 'Area2D'.
func _process(delta):
pass
func instantiate(old_length, position_x, position_y):
length = old_length - 1
position.x = position_x + 32
position.y = position_y
No worries, but I was also wondering about “segment.length”. I did try doing exactly that and not using the custom function, but I then get the error that “length not found in base ‘Area2D’”. I get why it says that it isn’t in the base Area2D, but I don’t see why it looks at the Area2D and ignores the export variable.
Do you have any other errors in the bottom dock when you run your game?
Do you have custom _init() function by any chance?
From the looks of it what happens is that during instantiation Godot encounters a problem with the script attached to the scene and just skips it. So the resulting scene is a bare base Area2D which indeed doesn’t have instantiate() function or length property.
I am now very confused, as it could definitely have something to do with it not being able to find the scene in the file tree, but the path is correct so I’m not sure why it says it can’t find the resource. Specific error message is “E 0:00:00:0655 _parse_ext_resource: res://project_files/centipede/scenes/centipede_segment.tscn:234 - Parse Error: [ext_resource] referenced non-existent resource at: res://project_files/centipede/scripts/centipede_segment.gd”
I think I see the problem perhaps, it thinks the script is in a different folder than it is.
I started stripping things down, and I think that maybe Godot doesn’t like that I am technically (for less than a frame) trying to instantiate a scene inside itself. If that is the problem (as the only errors seem to be from that), how could I avoid that? Create the instance as a child of parent?
I am aware of the many issues this is causing, so I guess it’s time to just restart fresh knowing that recursion, creating oneself and of course the infinite loop (that I was going to fix but is still note worthy) are not typically very good things. You could probably make a game about those concepts, though.