How to properly create and actions queue?

Hey, I’m making a turn-based RPG where, during the player’s turn you select every action your party will take, then confirm them all at the end. All the actions solve once the turn finishes.

Now, I’ve looked up several tutorials online, but lots are project-specific and following 15 scripts with arbitrarily named arguments inter-referencing each other is… though. How do I go about doing it in a more general way?

I thought about using arrays, popping all the actions into it during the turn then solve them with a “for” function, but I’m having a hard time figuring out what to put in the array. For the sake of example, let’s say a basic attack is the most basic code:

func basic_attack():
       enemy.health -= player.damage

How do I put this into an array to be later solved as a function?

Since Godot 4 supports callables, I would simply put the functions themselves into the list and bind the required arguments to flesh out the exact behavior. Here’s a simple example:

var action_queue = []


func _ready() -> void:
	# add actions to queue
	action_queue.push_back(
		attack.bind("Lilly", 5)
	)
	action_queue.push_back(
		attack.bind("Peter", 3)
	)

	# execute actions in the order they were added
	for action in action_queue:
		action.call()

	# empty the queue
	action_queue.clear()


func attack(name: String, damage: int) -> void:
	print(name, " hit and caused ", damage, " points of damage")
1 Like

I ended up messing around with some code and testing different approaches and arrived at something very similar to this. Yours is even a bit cleaner, thanks for the reply!

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