- Home /
Declaring/modifying lists at runtime
Hopefully I explain this clearly enough.
Intended behavior: A character moves from room to room, visiting random objects in the room. It never visits the same object twice and may return to previously visited rooms.
Possible solution: The room carries a list of objects within it, which it passes to the visiting character. The character creates its own list of objects for that room and removes objects as they are visited.
Problem: For this solution, I would need to be able to create a list for each room at runtime (the number of rooms/objects is undefined). Is it possible to create lists in this way?
I'm trying for a clean and simple solution that's flexible enough to accommodate any number of rooms/objects. Other solutions for the behavior are welcome as well.
Thank you!
Generic lists might be what you are looking for. Here is a Wiki article on several of the collection types available in Unity:
http://wiki.unity3d.com/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use?
Answer by Tomer-Barkan · Oct 28, 2013 at 05:59 AM
This depends a bit if you want to create the list of objects per room in the inspector in unity, or during runtime. I'll explain how to do it when you create the object list in runtime and then explain what to change if you want to manually create the object list in the inspector.
You can start by creating a List
of objects per room. First, reference the generic collections:
using System.Collections.Generic;
then, to your room class, add a new list of objects:
List<MyObject> roomObjects = new List<MyObject>();
next, in your Start() method of the room, fill the list with object
roomObjects = GenerateObjectList(this); // you have to implement GenerateObjectsList(Room room) to return a list of objects for the given room.
now, when the player enters the room, you will need to COPY the list, not just reference it, otherwise when you remove something from the player's list it will also remove it from the room's list.
private Dictionary<room, List<MyObject> playerRoomObjectList = new Dictionary<room, List<MyObject>(); // this will hold the list of objects in each room as the player sees them.
// when entering a room, check if the player already has the list of objects. If not, copy it from the room
void EnterRoom(Room room) {
if (!playerRoomObjectList.containsKey(room)) {
playerRoomObjectList[room] = new List<MyObject>(room.roomObjects);
}
}
There, now you have a list of objects per room in your player's script, to reference a list of a specific room use playerRoomObjectList[room]
. If you want to remove an object from a room list:
void VisitedObject(Room room, MyObject obj) {
playerRoomObjectList[room].remove(obj);
}
Now, if you want to populate the object list per room in the inspector, simply have an array of objects per room instead of a list (since you define this in advance in the inspector, it is a constant array and not a changing list):
public MyObject[] roomObjects = new MyObject[1];
Then, in the inspector, you can change the size of the array per room, and drag and drop MyObject scripts into it. The rest of the code should work the same.
The first implementation is exactly what I was looking for! I played around with the idea of using a list of lists or arrays, but the idea of using a dictionary with rooms and lists hadn't occurred to me. Thank you!
Glad it helped. Always keep dictionaries in $$anonymous$$d (or hashmaps in other program$$anonymous$$g languages). They are very useful and very efficient.
Are there any performance concerns with creating lists at runtime? Is it more of an issue on mobile? Right now I use object pooling to reuse GameObjects but I am newing collections at runtime.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
How to pull two objects at the same time? 0 Answers
Ordering a list of GameObjects 3 Answers
Can't add GameObjects to ArrayList 1 Answer
Keep adding targets to a list 2 Answers