- Home /
"Dynamic" static variables
So here i am wondering, how would i go about this problem:
In my FPS game, there are BOTs. BOTs get a random name generated at the start of every game scene. Now what i want to do, is to only generate the name once at the very first start of the level, and then use the same one until i quit to the menu and choose a different map. How would i go about that?
I tried thinking about using static variables, however, these are counted as one and the same across multiple instances of the script. So that's a no, now making individually 12 or so static variables, and somehow make the script know which one to choose seems like a way too complicated approach to my problem. PlayerPrefs are even worse for this. The last idea i just had was to use some object that uses the DontDestroyOnLoad() function, but then how would i even go about assigning the bot objects when loading a new map again? Please help, i seriously have no idea.
but, you want one name for each typr of bot? you are destroying and instantiating multiple times that bot? i imagine that there ar emultiple type of bots and they get instantiate/destroyed all the time?
yeah, thats how i have it working right now.
and whats the difference between the bots? have different scripts?
Answer by talyh · Jun 15, 2019 at 02:54 PM
You could have a readonly variable. The caveat being, you need to generate the random name at the constructor.
public class Bot {
private readonly string _name;
public string name
{get {return _name; }}
public Bot()
{
_name = "randomlygenerated";
}
}
Hello, sorry for the late answer. I am trying to understand what this code really does. I will try this when i have the time, but if this somehow helps, my bots's gameobjects get destroyed when they are killed.
Answer by Kennai · Jun 21, 2019 at 01:44 PM
Create simple class with two variables - string Name and bool Taken
then create static class, where you will have a list of that class.
And two static functions:
TakeName() - will get first empty name (and mark it as taken) from list or generate new name (and also mark it as taken and add it to list) if all names are taken. Call this function in bot's awake function
ReleaseName(string name) - call this func inside bot's OnDestroy function. this function will uncheck "taken" attribute for that name.
Your answer
Follow this Question
Related Questions
Making an FPS arcade game. 0 Answers
[C#] Raycasts and Object Tags 1 Answer
Multiple Cars not working 1 Answer
Visibility Polygons for FPS AI to move tactically 0 Answers
2d AI: enemy targeting c# 2 Answers