Ask value of a different node

Godot Version 4.4

Question1

Hello, I’m making a frogger game and I need to move automatically the frog when is on a log so, I was thinking in ask to the node ‘‘trunk’’ for a @export variable to move the frog this way…
¿How can I ask to other node with other script and in the stage scene without no use a singleton?

I can use this too, but I don’t know how…
I want to use a global variable for every node Trunk and save it in the singleton but I don’t want to create a scene by code and the problem is the script of the node trunk, I need a few of trunks and everyone with a different ‘‘name’’ = variable

(I hope be so clear)

The Trunk node doesn’t need to be a singleton, it can just be a regular node. Make the Frog a node, too. Keep a reference to the current_trunk as a regular class field on the Frog. When the Frog jumps to the next Trunk, tell the Frog what Trunk it is. In the frog’s _process loop, move the frog to the current position of the trunk. You would do something like this:

extends Node2D

class_name Frog

var current_trunk: Trunk

func _process(delta):
    if current_trunk:
        frog.position = current_trunk.position

func jump_to_trunk(trunk: Trunk):
    current_trunk = trunk
1 Like

so, maybe I should use node instead area2d for the trunk…?

I’m still doubting about if is there more than one trunk the frog don’t be crazy if not is collision with only one, but I’ll trying thing to understand the code, thanks

You can use an Area2D, just make it a child of the Frog node. It’s typically better to use a component pattern for this kind of logic so nodes are handling the logic they are themselves responsible for in a self-contained way. When there’s a collision, you can tell your Frog node about it by having it listen to a signal, and then have the Frog node decide what to do.

1 Like

finally I use the values of the trunk I saved in a ‘‘Global.var’’ and in the frog I ask about they…

I don’t like so much but works

-------------0--------------------------

I’ll trying things like this for others occasions, I don’t understand so much because I need practice a lot, I really am a noob with Godot T>T
thanks