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];
}
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.