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()