Turn based combat based on speed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Rensae

Hey there ! I’m trying to get a turn based combat to work, I know that I’m close to it, but I’m lacking some knowledge to finish it. Here is the code I’m using (following a tutorial I found on it):

onready var active_character

func intialize():
	var mobs = get_mobs()
	mobs.sort_custom(self, "sort_mobs")
	for mob in mobs:
		mob.raise()
	active_character = get_child(0)

static func sort_mobs(a, b):
    #take speed of a and b, compare them and return the fastest
	return a.speed > b.speed

func play_turn():
    #wait for the character turn to end
	yield(active_character.play_turn(), "completed")
    #modulo the child count to go to 0 after finishing the child count
	var new_index : int = (active_character.get_index() + 1) % get_child_count()
    make active_character the next child to play
	active_character = get_child(new_index)

func get_mobs():
    #get the childrens
	active_character = get_children

I have 6 characters and 6 ennemies, each have their own stats in their own scripts, and all of them are childrens of the TurnQueue nodes I made and who has this script

I’ll gladly take any help, and sorry if this is obvious for some of you

:bust_in_silhouette: Reply From: whiteshampoo

your get_mobs() function does not return anything.
It should return an array of mobs, if i understand correctly.
So you should write:

func get_mobs() -> Array:

Then the editor will tell you, if you forgot to return something. (An array in this case)

What does the “->” mean ? You’re asking for a value right ?

Edit : Writing this give me a “A non-void function must return a value in all possible paths.”
I’m kinda lost on how to return all of the children as an Array, maybe something like

active_character = [$Children1, $Children2, $Children3]

?

EDIT 2 : I’m dumb. Thank you very much, I understand how it works now !

For those who will pass by :
The “-> Array:” means that you intend the function to return a value. So, you need to add a return [insert your variable] at the end of it.

Rensae | 2020-06-24 20:52

oh, sorry. i should have posted more explanations…
maybe you will find this useful:

Static Typing

I can recomment to use this whenever possible. It will help the editor to give you better autocomplete predictions and tell you, if you do stupid things :slight_smile:

whiteshampoo | 2020-06-24 21:36