# RPG turns system - arranging turns freezes up

Hi guys. I’m working on an rpg turn based script which will start by creating an array of turns, sorted by the participant’s speed stat. after a turn, the first entry in the array is destroyed and a new one is created at the end based on speed stats. it’s not based on time and is very similar to the turn based system in final fantasy 10.

in the following code;
a, b, and c refer to different game characters.
SStat refers to their “Speed Stat”. Count refers to their place in the queue. The code takes their speed and makes it a percentage of the sum of all their speeds, as a fraction of 10. then, a while loop keeps adding units to the array mentioned earlier until all 10 turns are taken. basically, their speed will determine how many times they take a place in that array and it makes sure that all the turns are spaced out proportionately. after all this, TurnsGui() will create a visual list in the array so that you can see where each unit is in terms of their turns. right now I’m focusing on getting those first 10 turns to show up. This script is added onto an empty game object which holds all of the players stats.

The problem is that when I begin the game, it just freezes. unity shows no errors and it doesn’t unfreeze. I’ve got a feeling that the first while loop is what is causing the problem, but I’ve had a good look at it and I can’t quite figure out what it is. Beyond that, I’m not sure if it will even work. I’ve gone as far as I can with what I’ve learnt, but I need some help with this one.

on a side note, This is the last script I have created in which I am using native arrays, as I’ve been told that “List” is much better, but I can’t find it in unity’s script reference, and thus I don’t know what methods it takes.

``````var aSStat : int = 200;
var bSStat : int = 150;
var cSStat : int = 90;

var speedTotal : int = aSStat + bSStat + cSStat;

private var aCount : int = 0;
private var bCount : int = 0;
private var cCount : int = 0;

// Calculates the speeds as percentages of 10

private var aPer : int = aSStat / speedTotal * 10;
private var bPer : int = bSStat / speedTotal * 10;
private var cPer : int = cSStat / speedTotal * 10;

var turns = ;

// this will create the first 10 turns. after that,
// a new turn is added after another is completed.

function PrepareTurns(){
while (turns.length < 10){

if (aCount < 10){
aCount += aPer;
}
else {
turns.push("a");
aCount = 0;
}

if (bCount < 10){
bCount += bPer;
}
else {
turns.push("b");
bCount = 0;
}

if (cCount < 10){
cCount += cPer;
}
else {
turns.push("c");
cCount = 0;
}

if (turns.length >= 10){
break;
// This is a little gimpy, but it should work.
}
}
Debug.Log (turns);
}

function TurnsGui(){
// This adds a tab for each active turn in the future.
// Each turn it renders the boxes for the turns gui.
var i : int = 0;
for (i=0 ; i < turns.length ; i++){
switch (turns*){*
``````

// x offset, y offset, x width, y height
case “a”:
GUI.Box(Rect(0,i * 200,400,200), “A”);
break;
case “b”:
GUI.Box(Rect(0,i * 200,400,200), “B”);
break;
case “c”:
GUI.Box(Rect(0,i * 200,400,200), “C”);
break;
}
}
}

function Start(){
PrepareTurns();
}

function Update(){
TurnsGui();
}

With a bit of help, I was able to get the script to work. I’ve included it here for reference should anyone else have a similar problem.

``````#pragma strict
import System.Collections.Generic;

var aSStat : float = 200;
var bSStat : float = 150;
var cSStat : float = 90;

var speedTotal : float = aSStat + bSStat + cSStat;

var aCount : float = 0;
var bCount : float = 0;
var cCount : float = 0;

// Calculates the speeds as percentages of 10

var aPer : float = aSStat / speedTotal * 10;
var bPer : float = bSStat / speedTotal * 10;
var cPer : float = cSStat / speedTotal * 10;

var turns = new List.<String>();

// this will create the first 10 turns. after that,
// a new turn is added after another is completed.

function PrepareTurns(){
Debug.Log(aPer);
var i : int;
while (turns.Count < 10){

if (aCount < 10){
aCount += aPer;
}
else {
aCount = 0;
Debug.Log("a");
}

if (bCount < 10){
bCount += bPer;
}
else {
bCount = 0;
Debug.Log("b");
}

if (cCount < 10){
cCount += cPer;
}
else {
cCount = 0;
Debug.Log("c");
}

if (turns.Count >= 10){
//break;
// This is a little gimpy, but it should work.
}
}
Debug.Log (turns);
}

function EndTurn(){
// Splice deletes and replaces. the first value is the place of the
// item being taken out, the second is the amount of entries being deleted
turns.RemoveAt(1);
PrepareTurns();
}

function TurnsGui(){
// This adds a tab for each active turn in the future.
// I might not need the whole function for it, but that might
// save processing power. This should go under Update().
// Each turn it renders the boxes for the turns gui.
var i : int;

for (i = 0 ; i < turns.Count ; i++){
switch (turns*){*
``````

// x offset, y offset, x width, y height
case “a”:
GUI.Box(Rect(0,i * 30,70,30), “A”);
break;
case “b”:
GUI.Box(Rect(0,i * 30,70,30), “B”);
break;
case “c”:
GUI.Box(Rect(0,i * 30,70,30), “C”);
break;
}
}

}

function Start(){
PrepareTurns();

}
function OnGUI(){
TurnsGui();
}