• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by smnerat · Feb 24, 2013 at 09:35 PM · javascriptarrayarrays

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];
 
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikilo · Feb 24, 2013 at 11:21 PM 0
Share

Hello, can't you use the Undo/Redo system from Unity? Did you even know/look at this possibility?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image smnerat · Feb 25, 2013 at 12:20 AM 0
Share

This sounds much easier and makes much more sense to me and I would have not known about it otherwise. Thanks!

avatar image hoy_smallfry · Feb 25, 2013 at 12:36 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

getting "null" when using array. (javascript) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges