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 Morvar · Apr 24, 2014 at 09:35 PM · c#indexnegativetable

How could I store negative coordinates with negative indexes with C#?

Hey!

I'm looking for somekind of 2 dimensional "table" solution (with C#) which's size could start from negative indexes. For example From -3,-3 to 3,3. And this table should be able to store one value, most likely a string and I'd like to easily access offset values with iterators. Dynamic size would be a plus but not must.

I have this randomly generated world consisting of blocks, similar to Minecraft/Cubeworld/Trove etc, and I'd like to save my chunks with zero point of 0,0.

Currently my game stores the chunks in a normal 2 dimensional array, for example size of 100, so the zero point of the game would be 49,49 but this causes a bit too much hassle with all the other code.

So are there any kind of storing solutions supporting negative indexes?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Agemennon · Apr 24, 2014 at 10:04 PM

If you use a data structure like a Dictionary (in System.Collections.Generic), you can index chunks by their two dimensional coordinates (or rather, the hash of that value).

To my knowledge, this is the solution that Minecraft uses for indexing their chunks.

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 Morvar · Apr 24, 2014 at 10:49 PM 0
Share

That sounds promising but would you $$anonymous$$d clarifying what do you mean by hashing the coordinates? A quick look at dictionaries told that it's using keys for stored values and in my case those keys should be the coordinates. So could I for example have a table as they key? Like map[2][2] as a key which stores value X?

avatar image Agemennon · Apr 25, 2014 at 04:39 AM 0
Share

A Dictionary organizes items based on the hash of the key.

Usage would look something like:

 chunkDict[new Vector2(indexX, indexY)].Foo();

You may want to look at writing your own Vector2 class that uses integer values in order to avoid floating point issues, but otherwise that should work.

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Javascript array: Negative Indexes? 2 Answers

Indexing lists based on positions 2 Answers

Multi-Arrays help 0 Answers

Transferring variables between JavaScript and C# 0 Answers

NullReference 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