Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Captain_Dando · Aug 05, 2012 at 11:30 AM · guiarraysrpg

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 = [0];
 
 // 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[i]){
  
  // 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();
 }

Comment
Add comment · Show 10
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScroodgeM · Aug 05, 2012 at 12:07 PM 0
Share

r u sure that

var turns = [0];
is correct syntax?
avatar image ScroodgeM · Aug 05, 2012 at 12:08 PM 1
Share

insert Debug.Log(turns.length) in while block to check what happens. also add counter and ++ it every while cycle, break it when it reaches big value to avoid freezing and allow debugging

avatar image ScroodgeM · Aug 05, 2012 at 07:43 PM 1
Share

list.Add(item);

avatar image ScroodgeM · Aug 05, 2012 at 07:44 PM 1
Share

PS object[] and List is not the same.

object[] is array with fixed dimension, so you should declare size before filling it

or use List<TYPE> ins$$anonymous$$d

avatar image ScroodgeM · Aug 05, 2012 at 07:55 PM 1
Share

list.Remove(item); list.RemoveAt(index);

List<TYPE>l can contain only objects of type TYPE, if you want to store different types, create list of 'Object' type. string and numbers are inherited from 'Object', so list of Objects can store both.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Captain_Dando · Aug 06, 2012 at 05:06 PM

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 {
  turns.Add("a");
  aCount = 0;
  Debug.Log("a");
  }
  
  if (bCount < 10){
  bCount += bPer;
  }
  else {
  turns.Add("b");
  bCount = 0;
  Debug.Log("b");
  }
  
  if (cCount < 10){
  cCount += cPer;
  }
  else {
  turns.Add("c");
  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[i]){
  
  // 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();
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Remove Items and Item Tooltips 0 Answers

How to make a cutscene like RPG Games 1 Answer

Using arrays with OnTriggerEnter 0 Answers

need to shorten my code but unsure of how 3 Answers

Setting GUI textures 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges