x


Turn Based RPG Combat, Sorting Turns

Each character has a script and within the script a "speed" variable.

I have an empty GameObject with a script that will handle all the combat mechanics. This script accesses the speed value of every character. It also has these values in an array.

How do I script to have the Assign the turns based on speed? For example, 100speedChar goes before 70speedChar who goes before 50speedChar etc.

Thanks.

more ▼

asked Nov 19 '11 at 12:08 PM

Neten gravatar image

Neten
61 2 2 4

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Flip the speeds, so that lower is better, with, say, 100/spd. That gives your speed 100 a 1 and speed 50 a 2. Call that the "ActionDelay."

Start everyone off at their ActionDelay. Each turn, lowest number acts, and add their actionDelay:

speed100   speed50  speed70
    1         2       1.43   #1 acts, goes to 2

speed100   speed50  speed70
    2         2       1.43   #3 acts, goes to 1.43+1.43

speed100   speed50  speed70
    2         2       2.86   #2 acts (special rule slower wins ties)

speed100   speed50  speed70
    2         4       2.86   #1 acts again, goes to 3
more ▼

answered Nov 20 '11 at 03:36 AM

Owen Reynolds gravatar image

Owen Reynolds
11.3k 1 7 45

But how do I make it know WHICH character has a speed of 100 or 50 or so? I can understand the reasoning behind flipping the speeds, you're turning them into points in time.

p1ActionDelay=(p1Script.speed)/100;
p2ActionDelay=(p2Script.speed)/100;

(guess I'm going to have to make the "speed" values floats then..)

How I want this to work is;

Time calculation stops, player chooses actions for each character. AI automatically sets actions for the mobs. Time calculation starts, lowest ActionDelay gets triggered, time calculation stops, action is complete, time calculation resumes, second lowest ActionDelay gets triggered, time calculation stops, etc.

p1ActionDelay and p2ActionDelay are just float values, how do I make it understand which combatant they belong to?

Pseudocode will be more than enough. Thank you.

Nov 20 '11 at 09:43 AM Neten

You can leave the base speeds as int -- the internal "lower is better" speeds would be floats.

A cheap way is to give everyone an extra "moveNext" var. Each turn, search all mobs and players. Whomever has the lowest, moves. Since it's turn-based, speed doesn't matter.

A nicer way is to make your own array of Transform/actionNumber for as many players+mobs. The Transform only points to the real gameObject. Each turn, search it for lowest and that transform acts.

Nov 20 '11 at 06:11 PM Owen Reynolds

I couldn't:

-Find the lowest moveNext(which is what used to be "actionDelay", right?)

-Even if I could, find out to whom that moveNext belongs to. (At least when I tried it with Array.Sort(), which only returned a numeric value, which I couldn't identify whom it belongs to.)

Nov 20 '11 at 09:17 PM Neten

Let's say you use FindObjectsWithTag for each move. Go through the array yourself, finding the lowest Obj[i].GetComponenent<mobScript>().nextMove. Look at a standard "find the lowest/highest in an array" loop (general programming -- not Unity specific) if you aren't sure. The end result is the lowest# and the index where it was.

Then can call Obj[indexOfLowest].GetComponent<mobScript>.doMove();

If you have a separate array for them all (trickier,) group nextMove and transform together: class moveData { Transform mob; float nextMove; } Again, search for lowest. You could safely sort that array, but it takes longer (and you have to tell it to sort by moveNext.)

Nov 21 '11 at 12:33 AM Owen Reynolds

Ok, I got it now. You've been very helpful, thanks a lot.

Nov 21 '11 at 07:57 PM Neten
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3736
x1360
x206
x39

asked: Nov 19 '11 at 12:08 PM

Seen: 1338 times

Last Updated: Nov 21 '11 at 07:57 PM