Hey guys, I have made a GlobalVariables.js to keep track of all my globals. In there I have a list of gameobjects that have an isOn boolean value. I want to run a for loop to check all of the gameObjects to see if they all test true. How can I accomplish this? Should I do a return true if all the objects return true, and a false return if one of them returns false?
Could I do a
if(GlobalVariables.objectList == true){
}
or do I need to write a for loop that returns true if all items are true?
No, the code you show above won’t work. You’ll need to use some method to test each element (a for loop, linq, etc). Though, as you mention, you could short-circuit the loop on the first encountered false…
Jeff
Do you have any pointers on how to structure the boolean test?
function Update(){
for(crystalight in GlobalVariables.lightList){
if(crystalight.lightActive == true){
}
}
}
If you’re just looking for “true if all true and false if any false”, then something like this:
function Update(){
for (crystalight in GlobalVariables.lightList)
{
if (!crystalight.lightActive) { return false; }
}
return true;
}
Though you’ll need to ensure that your loop logic is properly constructed. I don’t know enough about your stored objects to comment on that…
EDIT: Also, do you really need to check this in Update (every frame)?
Thanks for the responses. well you see I have these “checkpoints” that light up when you run past them. They then count down and if you do not make it to the next checkpoint before it gets to zero, you have to start over. So the all true check is to see if they have all been turned on. This check is also needed to re-activate the already hit checkpoints to restart their timer when you hit a new checkpoint.
For example:
player hits starter checkpoint
starter checkpoint = max time and isOn;
player hits second checkpoint within time limit
starter check point = max time and isOn;
second checkpoint = max and isOn;
player hits third checkpoint which is also the last
starter check point = max and isOn and not counting;
second check point = max and isOn and not counting;
third check point = max and isOn and not counting;
door opens since all of the checkpoints are active at the same time.
While I understand the general concept you’re going for, I don’t understand parts of the example you laid out above. Anyway, it still seems like scanning all checkpoints for “true” once per frame is (way) over kill. I’d guess the only time you’d need to do a checkpoint scan is at the time a checkpoint status changes (say, when it’s activated by a player passing by). With that in mind, I’d probably put a small script on each checkpoint that could notify some higher-level game manager when it’s state changes. Ideally, you’d probably do that by passing a message or firing an event.
Assuming you were to use events, when checkpiont A’s status changes, it would basically say something like "Hey, in case anyone’s interested, my status just changed to “active”. Then, the event listener (in this example, some game manager object) would respond to that event by running the checkpoint scan on all checkpoints.
That way, the checkpoint scan would only run when the status of a checkpoint changes. Now, since you’re trying to run it from within Update, it could be running tens or hundreds of times per second - which is just unnecessary.
Yeah I see what you mean. I will see if I can design a system using the message broadcasting system. Thanks again for all your help.
Ok, been looking at this for a while now, I cant seem to get it working the way I want. Here is the code I have…
Sorry the comments are a little messy.
LightTrigger:
var Root : GameObject;//Grab the topmost gameobject for this prefab.
function OnTriggerEnter(other : Collider){//Get player collide
Root.BroadcastMessage("LightActivate");// Turn on the Lantern.
Root.BroadcastMessage("SoundActivate");//Play activate sound.
}
LightDimmer:
var lightActive : boolean = false;
var dimmable : boolean = true;
var lightBrite : float = 0;
var waitTime : float = 60;
var tryCount : int = 0;
var currentBrightness : float;
private var startTime : float;
var myLight : Component;
var Root : GameObject;
function Update(){
if(lightActive dimmable){//Check if I am active.
lightDimmer();//Fade the light over time.
}
if(myLight.light.intensity <= 0){//Check to see if I am out.
tryNumber = tryCount + 1;//Increase count for difficulty adjustment.
Root.BroadcastMessage("EmitterActivate",false);//Turn off my emitter.
lightActive = false;//Set my state to off.
myLight.light.intensity = 0; //Reset my light to zero.
}
}
function lightDimmer(){//This dims my light over time.
waitTime = waitTime + (10 * tryCount);//Span to dim the light over.
currentBrightness = (waitTime - (Time.time - startTime)) / waitTime;//What my brightness should be now in percentage.
myLight.light.intensity = currentBrightness * lightBrite;//Adjust percentage to actual brightness.
}
function LightActivate(){//Turn on my light.
myLight.light.intensity = lightBrite;//How brite i should be initially.
Root.BroadcastMessage("EmitterActivate",true);//Turn on my emitter.
startTime = Time.time;//When i was turned on.
lightActive = true;//Set my state to on.
}
EmitterController:
var emitterActive : boolean = false;
function Update(){
if(emitterActive){//Check to see if I am active.
gameObject.particleEmitter.emit = true;//Start emitting.
}
else{
gameObject.particleEmitter.emit = false;//Stop emitting.
}
}
function EmitterActivate(answer : boolean){//Function that tells me to start emitting.
emitterActive = answer;
}
GlobalVariables:
static var playercolor = "White";// The players current color for block puzzles.
static var hasItem = false;//Does the player have a block.
static var lightTotal = 0;//Number of lights in the level.
static var lightList = GameObject.FindGameObjectsWithTag("CrystaLight");//Array of all lights in the level.
LightTracker:
var listToUpdate;//Array of lights to reset.
function turnOn(){//Function to turn on the lights "unwritten"
}
function restartAllLights(){// function to reset all lights timers to 100%.
var listToUpdate
for(crystalight in GlobalVariables.lightList){//Iterate through the lights in the scene.
if(crystalight.lightActive == false){
return false;
}
else{
return true;
}
}
}
Look up information on bitvectors. No point in having an array of booleans when you can stuff them all into a single 32/64 value and then use bit math to efficiently test them all in one fell swoop.