+2 votes

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

in Engine by (86 points)

1 Answer

+1 vote
Best answer

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)

by (1,536 points)
selected by

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.