- Home /
Find a GameObject without a reference
Hello everyone,
I am building a RTS-Lockstep implementation right now and have a question: I successfully instantiate gameObjects on all clients, but now I want to destroy one of them. How do I identify a gameObject without a reference (because every client has another ref.)? First thing I thought of is giving the gameObject a "public int ID" via script, but then I would have to iterate over all gameobjects to find the chosen one.`
There must be a better way....
Answer by Kiwasi · Dec 05, 2014 at 02:01 AM
Short Answer
Maintain all of your GameObjects in a List or Dictionary of some sort. Then call them by index, which can be your unique ID.
Long Answer
I do think you are getting lockstep wrong. Lockstep networking relies entirely on each client performing the simulation identically, based on a set of orders from all of the other clients. So the only data you should ever be passing through your network is the orders. When all orders are received each client will run the next step of the simulation.
So to lockstep an RTS you should never need to instantiate or destroy an object over the network. Rather you send the orders across all clients, and each client instantiates and destroys its own objects, based on a local calculation of the simulation.
The List idea is interesting.
And regarding your long answer: I do get it right, everything is as you described it. But if the player for example "sells" a building, the GameObject needs to be destroyed (or after a unit dies etc.).
Its less about the destroying itself but on how to address objects (for example a player has multiple towers and upgrades one, how do I guarantee that its on all clients the same tower without massive overhead)
A dictionary will probably be the better idea then a list. Dictionaries are typically faster for adding and removing. Give every GameObject a unique ID when creating. Add it to the dictionary with this ID.
Then when you send orders across the network you can tag them with the ID of the GameObject they effect.
That sounds wonderful, I think I am doing that.
Dict<int, GameObject> for the ID and the reference to the object. Due to the implementation of lockstep every ID represents the same object on every client.
Thanks alot!
Answer by jpthek9 · Dec 05, 2014 at 01:42 AM
GameObject LeObject = Network.Instantiate("Prefab"); int ID = NetworkView.ViewID;
Then you send that through and RPC or somefin...
GameObject LeObject = NetworkView.Find(ID).gameObject;
As side note: An RTS is a HUUUGEEEE pain to get going. I tell you this from experience. I recommend you try getting something like an FPS going or a game like Dungeon Crawlers before becoming this ambitious. Don't lose hope though. Just learn the basics so you know what you're doing. That way, you can code your game efficiently from the ground up and won't have to redo it after realizing a fatal error.
Its running fairly well so far ;-) And its not an "Network." instantiation, but two separate local GameObject.Instantiate ones, so there is no NetViewID.