- Home /
How to store aspects (position, name, tag) of a game object in an array to be used for reinstantiation?
Hi everyone! Here's some background: I have a game where a user merges two separate blocks together to create a single block. This is done via script by destroying one of the game objects and then modifying the second. As the player progresses this may be done 40 times or so.
The problem is if the player makes a mistake, there's no going back, they have to restart the level and progress all the way through from the beginning. Obviously I need a back button, I intend on doing this by storing all the variables needed to reinstantiate the two blocks and then create them. The variables needed are the name, tag, and position. At this point I havent made much progress. Despite quite a bit of time spent googling, I haven't been able to find anything (useful).
I'm having a problem storing the variables in my array as well when I use the code below (my best attempt so far) it actually compiles and runs, but the rest of my code is ignored. It seems like store function is called but it gets stuck, but I think this just may be a side effect of not using the array properly.
I'm hoping someone may have some insight or be able to point me in the right direction.
static var firstPiecePositionArray : Vector3[];
static var firstPiecePosition : Vector3;
static var firstPieceTransform : Transform;
function Update {
//do stuff
Store();
}
funciton Store(){
firstPiecePosition = firstPieceTransform.position;
firstPiecePositionArray[] = firstPiecePosition[firstPiece];
}
Hello, can't you use the Undo/Redo system from Unity? Did you even know/look at this possibility?
Answer by hoy_smallfry · Feb 24, 2013 at 11:12 PM
What you are attempting to do is something that can be designed with the Memento design pattern in mind.
First, you want to write a structure that holds all of points of data you need to reconstruct your blocks:
struct BlockMemento
{
Vector3 position;
string name;
string tag;
//.. other details
}
Then you want to write a function that serializes your objects to this struct, and another that deserializes it, aka functions that convert between the two types. In the link above, these two functions are part of the Originator class, as createMemento() : Memento and setMemento(Memento m) : void*.*
Finally, you want to use a data container called a stack (watch the first two slides). It's better to use this instead of an array to store instances of your BlockMementos. Stacks hold objects like arrays, but unlike arrays you can only put and take things from on top of the stack. This is perfect for what you want to do, because the last thing you put in the stack is what you get when you take from it. You can use Arrays like a stack in JavaScript and C# comes with a standard generic stack type; use those so you don't have to build your own.
So every time a block merge happens, you would serialize your GameObjects into BlockMementos then push the BlockMementos onto the stack. When you want to undo changes, you pop the BlockMementos off and deserialize into GameObjects.
If you want the ability to "redo changes", a suggest two stacks - one for saving undo states, the other for saving redo states.
I hope that gives you some insight on how it's usually done.
This sounds much easier and makes much more sense to me and I would have not known about it otherwise. Thanks!
The website I linked you to with the memento pattern is pretty good. it has a lot of useful patterns and design ideas you can use to help with software development.
Your answer
Follow this Question
Related Questions
Array problem -3 Answers
How to create an auido array (JavaScript) 1 Answer
Problem with moving objects in array? 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer