In my multiplayer game, I’ve reached the stage where I need to make a scoreboard. For that I’ve decided to make a boolean for each of my ten characters that becomes true when they die and then make a separate script (GO) that checks who died and in what order.
Does anybody know how to check what boolean is true at what time? The sequence is very important, as the last man standing gets the most points.
You’ll have to keep book yourself. A boolean doesn’t know when it was changed, it only knows it’s state. The easiest solution is if your scorekeeper has a method (e.g. iHaveDied) that each player invokes, and the score keeper object notes the Player and the time (for example using a List<>, or even better, using a Dictionary<string, float> where you can note each player’s name and the time they died. Requires all Player names to be unique).
The best solution for this kind of things (i.e. when you want communications between scripts when something special happens) is to use events.
You can use unity events or system events, I personnally use system events because I’m not a fan of using events inside the editor.
public class Objects : MonoBehaviour
{
bool isDead = false;
public void Die ()
{
isDead = true;
MyEvents.CallOnObjectDied(this);
}
}
public class ObjectsManager : MonoBehaviour
{
List<Objects> allObjects = new List<Objects>();
void OnEnable ()
{
MyEvents.OnObjectDied += ObjectDied;
}
void OnDisable ()
{
MyEvents.OnObjectDied -= ObjectDied;
}
void ObjectDied (Objects _obj)
{
allObjects.Add(_obj);
}
}
public static class MyEvents
{
public delegate void OnObjectDeath(Objects _obj);
public static event OnObjectDeath OnObjectDied;
public static void CallOnObjectDied (Objects _obj)
{
if (OnObjectDied != null)
OnObjectDied(_obj);
}
}
This can also be used for UI to update informations without having objects to store references to UI elements and allows to stop using singletons.
There is called the observer pattern if I’m not mistaken, you can check that out for more informations.
No, this is impossible to know for boolean and for all other types unless you record it yourself. You should add another float near to that boolean and save (Time.realTimeSinceStartup - timeGameWasStarted) into that. And time timeGameWasStarted variable you should declare yourself and write Time.realTimeSinceStartup into when your game starts.
“Dying”, while a popular concept, is a concept of your game. Thus, you write and call functions that handle this kind of stuff yourself. If you want to know when people died, keep track of it when calling this function. For example, each character may have a list of “Deaths”, which is a data type containing the location, time and maybe killer / killing ability of a death. Anything you deem important, really. Everytime a character dies, you take a snapshot of these values (location, time, … abilitiy that killed them or whatever) and then save it in the respective list of that character. Later you can now easily check how many deaths a player had, when these happened, where these happened… what caused them - or whatever else you deem important to keep track of. From there it’s also easy to save these values to a database (after the game, if at all) to evaluate statistics later on, if that’s something you want or need to do for your game.