- Home /
Best way to maintain a list of selected objects? (Static Classes?)
I was wondering what the best way to maintain some selected options or game states. That doesn't really explain it very well. Let me give you an example.
Lets say I'm building an RTS game. Say have 2 buildings, a factory and a refinery. There are 2 gui buttons to reflect this. When I click factory it sets the 'buildingToBuild' variable to a GameObject of the factory. When I click on the ground to place the factory, it places the factory and then nulls the buildingToBuild variable. Now when I click on the building it sets the 'selectedBuilding' variable to the factory game object.
does that make sense? I would like the buildingtobuild and selectedbuilding to be accessible by all classes. I was thinking that I could have a 'GameVars' class that contained static variables.
Would this be an acceptable way to approach this problem or would this create more problems for me down the road?
Answer by jorashwil · Feb 27, 2013 at 04:16 PM
What I see is you needing a call reference. For example, I do a lot of JS. So at the bottom I'd add:
@Script RequireComponent (name) //This is the main controller
@Script AddComponentMenu (srclocation/"filename") // This is the script in question
You can have as many scripts attached as you like to each object. You need one main script for the building and then the 2 selection scripts your having issues with attached and called upon on selection. That's the simplest way I know how to explain it.
Well, all my 'buildings' will have a script for themselves.
the factory and refinery both would inherit from a building class. In that building class I would have something akin to
void On$$anonymous$$ouseDown()
{
if (GameVars.selectedBuilding == null)
{
GameVars.selectedBuilding = gameObject
}
}
I ideally would like to avoid adding more than one script to my prefabs.
Then I would suggest a src file just for those scripts. Add a loadscript line to call on those class scripts you need so you only need 1 script attached.
Ok, but would that make a new instance of those scripts?
No, all your doing is saying when one var is true call "this" script, when that var becomes false call "that" script. That's what I like to do. Yeah, it means more scripts but it helps the game engine stay running fast and not slow down.
Ok, so would this be similar to inheritance then? Would it be an alternative to inheritance?