I'm making a game which involves placing objects in a room. The room stores a list of objects that can be placed in it, e.g.
List<RoomObject> possibleObjects = new List<RoomObject>(){ new Bed(), new Table() };
These objects are then each sent to buttons which allow the user to click the button, entering a placement phase for the chosen object, where the user then clicks in the room, and the object is placed.
e.g.
public Room currentRoom;
public RoomObject currentObject;
//...
public void onClick()
{
if (CanPlace) currentRoom.Add(currentObject);
}
My problem is, if the user wants to place more than one of the same object, the current way it's set up will mean the exact same object will be added to the room, and if that object is later edited in some way (e.g. Bed.occupied = true), it will affect all of the objects of that type in the room.
Is there a way to duplicate an object (to get a separate reference) without me having to use reflection (which I'm not very familiar with, and feel is unsafe code).
I assume the reflection way would be passing around a Type and then having to call constructors using Type.GetConstructor, but I'd rather not do this if possible. If reflection is the only way, could someone provide example code for how to do it?
EDIT - perhaps I need to specify that my variable currentObject will not hold a variable of Type RoomObject, but a subclass such as Bed. (Bed inherits from RoomObject).