Trying to program a button to trigger a method in a different scene

Godot Version

4.5.1.stable

Question

I have two scenes, named “World_map” and “cargo_loading”. I am trying to program a button in “cargo_loading” to initiate a method in the “World_map” scene. Below is the code I have so far (not all of the code, because I currently have a lot of code, but these are the relevant sections):
In the “World_map” scene:

func _ready():
	Globals.cargo_sent.connect(create_airship_icon)
	calendar.text = str("Day " + str(timer_counter) + ", " + str(year))
	camera_2d.global_position = map_to_local(Vector2(78, 165))
	populate_cities()
	update_location_purchase_label()
	update_balance()

func create_airship_icon():
	print ("create_airship_icon triggered") <---Purpose of this is for debugging
	var airship = Button.new()
	
	#Spawns new airship button in World Map
	airship.icon = load("res://sprites/Airship_icon.png")
	airship.flat = true
	var origin_vector = Vector2(map_to_local(Globals.cities[Globals.selected_city][0]))
	airship.position = origin_vector
	add_child(airship)
	
	#Replace the random selecting of cities with a menu of available cities the player can choose from
	var destination_vector = Vector2(map_to_local(Globals.cities[Globals.selected_destination_city][0]))
	var final_distance = origin_vector.distance_to(destination_vector)
	
	#Records the day of airship departure
	initial_day = timer_counter
	
	#Moves the spawned airship button from the origin to the destination cities
	var tween = airship.create_tween()
	tween.tween_property(airship, "position", destination_vector, final_distance * speed_multiplier)
	await tween.finished
	
	#Records the day of airship arrival
	final_day = timer_counter
	
	#Tests if the travel time is calculated.
	print ("Travel time between " + str(Globals.selected_city) + " and " + str(Globals.selected_destination_city) + " was " + str(final_day - initial_day) + " days.")
	
	#Updates balance with income
	Globals.balance += int(Globals.cargo_amount * 0.1)
	cumulative_income += Globals.cargo_amount * 0.1
	income()
	
	#Updates balance with fuel expenses
	var fuel_cost = Globals.monthly_expenses["Fuel"] * final_distance
	cumulative_fuel_cost += fuel_cost
	Globals.balance -= fuel_cost
	fuel_amt.text = str("$ " + str(int(cumulative_fuel_cost)))
	
	airship.queue_free()
	update_balance()

Below is the code I have for the “cargo_loading” scene:

func set_destination(selected_city: String):
	Globals.selected_destination_city = selected_city
	get_tree().change_scene_to_file("res://scenes/World_Map.tscn")
	Globals.cargo_sent.emit()

I have the code below in “Globals” script (which is Autoloaded):

extends Node

signal cargo_sent()

I suggest the button store value at somewhere that accessible to World_map, and when the World_map loading, read the value in _ready function and trigger the function from there according the variable.

loading:
   press button:
       (set global-value/x 123)

world_map:
    _ready:
      match (get global-value/x):
         case ... : 
            (reset global-value/x)
            invoke the function
         else: break
      .....

I dont know how to do such thing in Godot, yet. But the logic much like storing temporary value in something globaly accesible and read it then invoke the function according it.

1 Like

Scripts can call functions in other scripts if they have a node path. If your hierarchy looks something like this:

world_map
   cargo_loading
   other_stuff

Then in the script for world_map you can call $cargo_loading.do_thing() assuming there’s a do_thing() function defined there.

In cargo_loading you can get_parent().do_thing() to call functions in world_map’s script. So you could do something like:

# world_map

[...stuff...]

func cargo_sent(cargo: String, dest: String, duration: float) -> void:
    print("cargo " + cargo + " -> " + dest + ", ETA: " + str(duration) ".")
# cargo_loading

func cargo_button_pressed() -> void:
    get_parent().cargo_sent("Top Hats", "Oslo", 17.0)

You don’t say what the problem is?
It should work the way you have it set up. (apart from the fact that you queuefree() the airship (effectively deleting it) in the same function you create it in, giving it a very short lifespan)