Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 zero_rogue · Nov 11, 2011 at 06:40 AM · arraydynamic2d array

Adding elements to a 2d array.

I'm working on a game that will use an 2d array to store data. I need the array be dynamic as it will grow as the player plays the game.

I am planning on using the built in 2d arrays (var x = new int[];) because there faster and easier to use.

I need to add additional elements to the beginning and end of the array as the player goes through the game.

How would I go about doing this? Or if someone has a better idea to my problem that would be helpful.

This is for JavaScript btw

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 syclamoth · Nov 11, 2011 at 06:41 AM 0
Share

How about using dynamic lists ins$$anonymous$$d? Resizing arrays is impossible, so you may as well use a data type that has that kind of functionality built in already.

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Rod-Green · Nov 11, 2011 at 09:02 AM

As mentioned you can create a List type like so:

   List<myType> myList = new List<myType>();

And to the add to this list all you need to do is:

   myList.Add(myObj); // where myObj is of type myType

To insert at the beginning do:

   myList.Insert(0, myObj);
Comment
Add comment · Show 7 · 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 zero_rogue · Nov 11, 2011 at 09:42 AM 0
Share

looks interesting, however your example does not seem to be 2 Dimensional. How would you implement it to be 2 Dimensional? From what it looks like you would need to make it into a Jagged array that would make adding and verifying elements a lot more difficult than the simple built in system. Also from what I read this does not have any better performance than the standard JavaScript Array's....

avatar image Rod-Green · Nov 11, 2011 at 10:02 AM 0
Share

I didn't see this was for JS specifically or for a 2D array. However what concerns do you have about using a jagged array? Seems like you wouldn't have much choice considering you want to add and insert to it.

You could rebuild the 2d array every time but that would get expensive if you do it a lot/large arrays.

So really you can just replace

   List<myType> myList 

With

   List<List<myType>> my2DList

And add to it the same as before

  my2DList.Add(myObjList); // where myObjList is of type List<myType>

If you know that the y size of the array is fixed then it could be

   List<myType[]> my2DList


Also note 2D lists and jaggad arrays don't serialize(save)

avatar image zero_rogue · Nov 11, 2011 at 11:31 AM 0
Share

I wanted to use a real 2d array because it represents a 2d grid, would make it simpler to implement. The Array will be used to keep track of a randomly generated game world. $$anonymous$$y intention is to have the world generated as the player explores and in turn resize the array when it gets bigger than its current size.

avatar image syclamoth · Nov 11, 2011 at 11:43 AM 0
Share

The point being made here, is Arrays don't resize. If you want to change the size of an array, it basically comes down to creating a new, bigger array, copying all the elements of the old array over into it, and deleting the old array. $$anonymous$$uch simpler to use data types which are built to be dynamically sized.

avatar image Rod-Green · Nov 11, 2011 at 07:04 PM 0
Share

Syclamoth is correct. You can't have both.

However why don't you just store the random seed used to generate the world and in turn any subsequent plays of the game would regenerate the same. This is NOT how $$anonymous$$ecraft works. ;) So all you'd need to do is store the differences or changes the player made and not the world itself.

Show more comments
avatar image
0

Answer by crazyKnight · Nov 11, 2011 at 09:14 AM

select the one which best fullfils your need

link

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

check occurence of int in 2darray c# 3 Answers

2D array generated with custom inspector, null when starting game? 1 Answer

Tracking ojects grid positon 1 Answer

Array Within Index 1 Answer

Mouse Position to 2D array 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