#Shaun Spalding - Turn-based Combat - How to determine turn order?

52 messages · Page 1 of 1 (latest)

surreal otter
#

Hi all, I'm a new programmer doing my best to work on my first basic concept for an RPG. I've got mostly everything sorted, I'm just struggling to figure out how to work out turn order? I've followed Shaun Spalding's video tutorial on turn-based combat, so currently it reads unit_turn_order = array_shuffle(units), but i want it to either be determined by the characters' speed stat (which i can add) or, preferably, enemies take an action after players do. Does anyone have any tips or pointers? I've heard using ds_lists can be a good idea, but I honestly have no idea where to start with those.

Thanks so much!

kind moatBOT
#

With this function you can sort an array in ascending order or descending order or using a custom function to define the sort order. The function requires you to provide the array to sort, and then either of the following:

Arguments
variable: The variable that holds the array.
**sorttype_or_function **: The sort type (true for ascending or false for descending) or a function reference to use for sorting.

drifting siren
#

Instead of shuffling, you sort it

#

See the example

#

Try it out if you still cant ill help more in like 30 mins

surreal otter
#

i tried to sort it like that, i'm not sure if i'm doing something wrong sorry, i'm pretty new ;;

drifting siren
#

right ok

#

so can i see what your array looks like

#

so the example i wanted you to see is this one

#
var _a = [10, 9, 8, 7, 6, 5];

array_sort(_a, function(elm1, elm2)
{
    return elm1 - elm2;
});
#

where it uses a function to determine the sorting order

#

elm1 - elm2
if elm1 is less than elm2, then elm1 - elm2 is negative, which means elm1 goes before elm2

that means taht elm1 - elm2 sorts by ascending order

#

if you want descending order, you sort by elm2 - elm1

#

ok

that's for an array full of numbers

surreal otter
#

right right

drifting siren
#

what are your arrays full of

#

(also, that error is unrelated to the sorting. the error is in the draw event)

surreal otter
#

this is what i've got here
i'm drawing from my global party and enemy arrays that i've got listed elsewhere

#

ah okayokay

drifting siren
#

ok so

#

units is full of instance id's

#

and i assume each instance
both obj_battle_unit_pc and obj_battle_unit_enemy
have a speed stat?

surreal otter
#

yes yes

drifting siren
#

what is it called

surreal otter
#

spd

drifting siren
#

cool

#

right
now here's the magic

#
array_sort(units, function(elm1, elm2)
{
    return elm1.spd - elm2.spd;
});
#

that'll sort the array units, by ascending spd

#

in this case elm1 and elm2 are id's

so it's perfectly good to do elm1.spd, just as you would do it for any other instance id

#

ok
that being said

array_sort doesn't return anything

#

so unit_turn_order is undefined

surreal otter
#

right

drifting siren
#

so

#

you tell me what you want

#

do you want to sort units and have that change

#

or sort and make a copy

#

and units will remain unsorted

surreal otter
#

i think i want to sort units and have that change, yeah

drifting siren
#

cool
then just do array_sort(units...)

#

then draw units in your draw event

#

no need for unit_turn_order

#

probs the best takeaway here is to read the docs page for any new functions in it's entirety

surreal otter
#

ooh okayokay that makes a lot of sense

#

thanks

drifting siren
#

and i say that

because there are array functions that modify the original, and there are others that make a copy

#

so...you gotta read the docs

#

example:

surreal otter
#

gotcha