Reputation: 23
Godot 4.3
I want my main scene to continously generate many instances of one node. Since I'm instancing the node via code, I'm having trouble accessing it's children to send it a signal.
I'm generating customers in a store and need to access their animationplayer while they move around.
In the relevant code in the store script:
@onready var CustomerNode = load("nodepath")
@onready var Customer = CustomerNode.instantiate()
var starting position
var ending position
var customeranimation
signal AnimationStart(customeranimation
signal AnimationStop()
func CustomerSpawn():
add_child(Customer)
if randi() % 2 ==0:
WalkPastStore()
else:
GoInStore()
## ^^ a customer will spawn and then randomly decide rather to go in the store or past the store
func WalkPastStore():
var tween = get_tree().create_tween()
if randi() % 2 ==0: #right to left
starting position = some_vector2_coordinates
ending position = some_vector2_coordinates
customeranimation = str("WalkLeft")
else: #left to right
starting position = some_vector2_coordinates
ending position = some_vector2_coordinates
customeranimation = str("WalkRight")
Customer.position = startingposition AnimationStart.emit(customeranimation) tween.tween_property(Customer, "position", endingposition, 1)
## ^^ when the customer spawns, it will be randomly assigned a start point, an end point, and a string name for an animation that matches the direction it'll go in. Then the AnimationStart signal will emit and send the animation string name to the instanced node's child -- an AnimationPlayer. A tween will also drag the Customer to it's positions.
func GoInStore():
pass
#the same thing but AnimationStop is used once inside the store
And the relevent code in the animation player script looks like this:
func _on_animation_start(customeranimation):
play(customeranimation)
print("the animation is ", customeranimation)
func _on_animation_stop():
stop()
print("the animation stopped")
There are no errors, the signals do go through as the print() test works, but the instanced node still isn't animating.
__
In order to send the signals, I needed to have a Customer already instanced into the scene visually. When I made that Customer visible during runtime, I noticed that its animation played even though the one instanced through code doesn't have its animation running. It just stands like a statue while it slides into its position.
I think this might be because the Customer that is instanced through code counts as a completely separate node from the Customer I'm sending the signals to. I'm hoping to find a way to send a signal to a manually instanced node's children.
I tried this
@onready var anisprite = Customer.get_node("Customer/AnimationPlayer")
func testfunc():
print(anisprite)
Nothing changed and my output printed "<Objectnull>" so I'm not quite sure how to send a signal this way.
Upvotes: 0
Views: 39