- Home /
How would I set up an order for turns using multiple scripts?
In my game I have characters that are shot from a catapult at targets. They then walk back to the catapult to be shot again. (There will be about 7 or 8 characters total.) I am trying to figure out how I to set up an ordering system so they are shot only when it is their turn. Since each character has their own script, I'm not quite sure how to go about this.
I am using the catapult script to keep track of the number of characters "OnDeck" but I'm not sure how to tell them to take turns. Right now when I launch the character in the catapult, all the characters launch at the same time. Any suggestions would be greatly appreciated.
Answer by Peter G · Aug 01, 2013 at 03:21 AM
This is actually a very similar (almost identical) problem to recycling objects when they're destroyed. Especially on mobile platforms its common practice to have a pool of objects such a projectiles that just keep getting reused when they are needed. It significantly reduces the amount of memory needed compared to instantiating new ones.
But I digress. For your problem, you'll want your catapult to keep track of all the characters in an array. Then when its time to fire, you want to keep an index of the current fire position starting at 0. The first guy is launched, then we move the index forward 1 to the second. Then that character is launched when the time comes. And then we repeat.
It could look something like:
private var projectiles : YourScript[];
//Whatever script is attached to your characters
private var launchIndex = 0;
function Start () {
projectiles = FindObjectsOfType( YourScript );
}
function Fire () {
if ( projectiles[launchIndex].isAvailable ) {
//some check to make sure the character isn't walking back still.
//Launch the projectile
launchIndex++;
if (launchIndex == projectiles.Length )
launchIndex = 0;
}
}
Just a few questions. So using this method, I would only need two scripts, right? The CatapultScript and the YourScript (YourScript being the one that is attached to each of the characters)? And the code you gave here goes in the CatapultScript, correct?
Would I list the different characters when declaring the array? For example:
private var projectiles : YourScript[Donner, Cupid, Blitzen, Vixen, Comet, Dancer, Prancer, Dasher];
Is the launchIndex the number of characters waiting at the catapult then? If so, why would you add to it in the Fire function? I've never used an array before so this is a little confusing. Thanks.
Well yes, you really only need those two scripts.
No, you don't need to do that. You could make a Dictionary later if you needed to, but the current array set up as I showed above should work just fine. FindObjectsOfType()
should automatically find all of them.
the launcherIndex
is the position in the array of the next character that we are going to launch. And I highly recommend that you learn how to use arrays. They are a crucial part of program$$anonymous$$g.
Ah. Cool. Yeah, I'm still learning how to script so I guess I had better find some good array tutorials cause this does seem very useful. Thanks for your help.
No problem. Don't forget to mark it correct if it helped you. :)
So I've been looking for examples of how to create and use game object pools, but unfortunately I've found it to be very confusing and the best examples I've found are in C# ins$$anonymous$$d of java. I'm able to create the array and show my list of characters in the hierarchy view when the game runs, but I'm still not sure how to "create" the new characters in the game when I need them. So far I have this code in my CatapultScript:
var characters : GameObject[];
var lastCharacter : int = -1;
var characterCache : int = 8;
function Awake()
{
characters = new GameObject[characterCache];
for(var i : int = 0; i < characters.Length; i++)
{
characters[i] = new GameObject ("Character-"+i);
}
}
function GetNextCharacter()
{
lastCharacter += 1;
if(lastCharacter > characterCache - 1)
{
lastCharacter = 0;//reset the loop
}
return lastCharacter;
}
I'm not sure if I need anything else in here or what to put in my CharacterScript to use this. To be honest, I'm not totally sure this is even the kind of code I need for my problem. I'm pretty lost on all this. Thanks for any advice.
Answer by boni · Aug 02, 2013 at 07:33 PM
I'd put the shooting stuff into a Coroutine. Then create a manager-script, that knows all catapults. Inside of that you use a coroutine, to launche the catapult-coroutines in order.
It's kinda hard to explain like that, but some pseudocode would look like this in C#:
Manager:
List<Catapults> catapults;
IEnumerator MakeCatapultsShoot() {
int i = 0;
// this actually waits
// until the catapult-coroutine is finished
yield return StartCoroutine(catapult[i].StartShooting());
i++;
if(i > catapults.Count) i = 0;
}
Catapult:
public IEnumerator StartShooting() {
.. do shooting stuff
}
You'd have to call StartCoroutine(MakeCatapultsShoot) whenever you want a catapult to shoot, and they'll take turns then.
Answer by Kiloblargh · Aug 02, 2013 at 07:48 PM
I don't know what your game looks like but couldn't you have a physical queue that uses colliders and triggers the way the loading system of an automatic gun works? So all the characters are trying to move toward a closed invisible door, constrained by invisible walls and capsule colliders on the characters in front of them, and every time the catapult resets, the door opens, lets one into the catapult if it is standing there, then closes behind it? The catapult script doesn't need to know which character it's firing and the character script doesn't need to know how to do anything except "try to walk forward until I can't anymore".
I've made that kind of thing long ago with Quake 1's triggers, I'm sure it would be easier with the physics in Unity...
This is a good idea too. I might try this as well. I still should probably learn to use arrays, but I may try this as a quick fix.
Your answer
Follow this Question
Related Questions
How does Unity "read" JavaScript code? 3 Answers
Label not updating when text changed through javascript send message. 1 Answer
How i can a variable clasification in javascript? 3 Answers
How much data can I send from the browser through unityObject.SendMessage()? 1 Answer
Send Message Overload 1 Answer