Scripting resource buildings/inventory, creating an end turn button

Godot Version

4.2.1

Question

So I’m chasing help with a couple things. Relatively new to GDScript and Godot, refugee from Unity, so still learning how it works over here.

Making a 2D turn-based empire sim. Less AOE and more just a few different scenes that lay out the various stats of your empire. There is a couple different but not unconnected things I want to achieve.

  1. An end turn button, which send a signal when pressed to various player-owned production buildings so that they will produce their goods.

  2. An empire inventory system which will track the various current resources/goods that the empire has in store, and that will be updated at the start of each turn with new values based on consumption/production.

I’ve spent quite a few hours struggling to get the latter to work, and the former I just don’t understand enough of GDScript to create myself. Any help would be appreciated.

The easiest way is to have an Autoload (or more)

  1. With a signal like turn_ended() where you’d connect your buildings and the end button would emit when pressed.
  2. With an inventory that each turn will be updated.

Example:

MyAutoload.gd

extends Node

signal turn_ended()

var inventory:Dictionary = {}

func _ready() -> void:
	pass

func update_inventory(resource:String, value:int) -> void:
	# this is just a quick example, don't copy it as is
	inventory[resource] = value

The end button script:

extends Button

func _ready() -> void:
	pressed.connect(func(): MyAutoload.turn_ended.emit())

The buildings script:

extends Node2D

func _ready() -> void:
	MyAutoload.turn_ended.connect(_on_turn_ended)
	
func _on_turn_ended() -> void:
	# do stuff when turn ended
	pass

What @mrcdk said - I have Events singleton that just declares all game-level events. No other code there besides signal declarations. Any node can then emit any of those signals and any other node can subscribe to an event. For example, when enemy dies, I emit enemy_killed(enemy) signal and my UI listens to it to update killed count and my Level listens to it to drop a chest or experience gem in enemy’s place. Obviously, gotta be careful to not overuse this or you’ll end up with a spaghetti event system.

I’d add that if the order doesn’t matter, then yeah just have everything listen to turn_ended() and do its processing then.

If the order matters, have one object (a node, everything is a node, but can be part of a scene tree or an Autoloaded singleton, ur call ) listen to turn_ended() and then emit function-specific signals such as produce_goods.emit() and update_inventory.emit() in the desired order.

Then your production facilities listen to produce_goods signal and inventory system listens to update_inventory signal, etc.

Good luck have fun and welcome to Godot!

1 Like

Awesome cheers for that! I’ll take some time today to work out what suits from this and see where I get. Thanks again!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.