#Signals between scenes

1 messages · Page 1 of 1 (latest)

blissful barn
#

I'm creating a simple 3D space shooter (think StarFox). So far I have three scenes, "Space" is a big empty pile of thing for things to be in and two child scenes, "Ship" which is the players ship they control, and "SimpleUI" which is a debug UI that shows the ship status in a 2D HUD.

In order to show things the Ship object has keyboard controls, and whenever the speed changes I emit a signal with throttle_changed.emit(throttle) where throttle is the ship's current speed after doing all the physics calculations. So how I tell the label in the UI to listen for a change in the Ship object? I tried

func _ready():
    var ship = get_node("Ship")
    ship.throttle_changed.connect(_on_throttle_change)

But I guess that would only work if Ship was part of the scene, do I need to pass info up to the main "Space" scene then pass it down somehow?

subtle ingot
#

When/where/how do you spawn your ship? That's how you should get a reference to it

#

or is the ship node inside your scene?

blissful barn
#

At the moment it's all hard-coded as I'm just starting out, Ship and SimpleUI are separate tscn files

#

Currently all the controls and movement code is in Ship, which I realise I'll need to move up to the main scene when I want ships for the player to shoot at

subtle ingot
#

No, having stuff self-contained is actually the way to go, IMO.

#

Discord images taking forever to load again.

blissful barn
#

So how do I make the label (in simpleUI) listen to the signal being emitted by Ship?

#

Oh, it's a tree! So I just need some ../ action. Wheeeee

#

var ship = get_node("../../Ship")

subtle ingot
#

You can do that, but that's potentially problematic

blissful barn
#

Oh it'll need redoing once I'm deeper and instancing and removing items, but I've a working signal so I'm starting to understand.

subtle ingot
#

Oh, forgot to add the second part 🙂