![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | lightspot21 |
Hello,
I’m making a scene which consists of 2 nodes, one the root (an Area2D
named Ship
) and the other a child to it (an AnimatedSprite
named ShipTypes
).
The Ship
node has a script that gives a random value to the ship_type
variable, while ShipType
’s script holds a reference to the parent Ship
node. When attempting to read ship_type
from the parent to check in the match
statement, the child reads 0 instead of the parent’s random value. Why could this be happening?
Thanks in advance.
Code (parent):
extends Area2D
# Available ship types
enum ship_types {CAPITAL = 0, HOSPITAL = 1, DESTROYER = 2}
# Chosen ship type
var ship_type: int
func _ready():
# get new random seed
randomize()
# pick a type at random
var ship_type: int = randi() % ship_types.size()
Code (child):
extends AnimatedSprite
var base_ship
# Called when the node enters the scene tree for the first time.
func _ready():
base_ship = $"../"
match base_ship.ship_type:
base_ship.ship_types.CAPITAL:
animation = "capital_ships"
# pick a random capital ship as a form
frame = randi() % 4
base_ship.ship_types.HOSPITAL:
animation = "hospital"
base_ship.ship_types.DESTROYER:
animation = "destroyer"