Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by harperrhett · May 27, 2020 at 03:48 AM · grideventsfor-loopiterategrids

How do I manage a game based on grid logic?

Please help... I'm tired and about to give up.


I have a game with a non-jagged 2D array. The array rows represent x, and the columns represent y. Each position in the 2D array can hold an object of class type Machine, which is a class I made that can interact with other objects in the grid. Example1


The player has the ability to place down sprites in the game on tiles that represent the grid positions that are in the code, and the sprites represent the objects of type Machine. This is where things get tricky. Objects in the grid have to interact with each other, simultaneously per frame.


The objects have to all interact simultaneously because if they interact iteratively (looping through each object) then there are inconsistencies. For example, say you have a grid of 3x3, labeling the objects numerically from bottom left to top right (0 - 8), so that the x axis ticks up the further you go to the right, and the y axis ticks up the further you move up. Now you do a standard double for loop iteration like so:

 for(int i = 0; i < factoryGrid.GetLength(0); i++) {
     for(int j = 0; j < factoryGrid.GetLength(1); j++) {
     }
 }

In my scenario one machine passes a value to another based on the direction it's 'facing'. Say object 0 passes a value to object 1, and object 1 passes a value to object 2. After the first full iteration, the value that was once in object 0 is now in object 2, whereas in a full iteration we only wanted it to go from object 0 to object 1. However if object 2 passes to object 1, which then eventually passes to object 0, the value would only be passed to object 1 because that's not the direction the loop iterates in.


The way I tried to fix this was to check if the value had been passed to the object the iteration is currently on, if true, then the current object is not allowed to pass it to another object. For some reason, this only worked as long as the object was not at a corner intersection. If it was a bottom left corner, it would skip the corner every time a value needed to be passed through, if it was a top right corner, it would take two iterations instead of one to get the value through. I have posted an image with this example, with the red squares being marked as the malfunctioning ones. Example2 I have no clue why this happens so I moved on to try a different solution.


Now I decide to try to activate all of my objects non-iteratively, with Unity events. This means I can tell all of my objects to pass a value at the exact same time, once per frame, so that no objects can do weird things like what happened in the corners when I was doing things iteratively. This summons a whole new problem. Now my values are passed through every possible object at the same exact time. If there are 4 objects, linked to pass the value from one to another, it all happens in one frame. I try to fix this by creating the same rule I tried iteratively, where an object is not allowed to pass a variable if that variables has already been passed, but unfortunately because the rule is being activated in all objects at the same time, the program is not able to catch the logic in that rule.


So that's my problem. I've been trying to debug this for 24 hours now, and because it's quarantine I haven't been doing much else. If you solve this, I would be forever thankful. Seriously.

untitled2.png (8.1 kB)
untitled.png (8.3 kB)
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 harperrhett · May 28, 2020 at 09:02 PM 0
Share

I am currently trying to figure out how to use a sorted list to keep any weird things to happen, but could still use a lot of help with this question...

1 Reply

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

Answer by harperrhett · May 28, 2020 at 10:44 PM

After taking a break of programming for a whole day, I have found what seems to be the solution. The solution would be to create two separate lists. Each list stores objects if they are on alternating squares of the grid. For example, if x is 0 and y is 0, it is stored in the first list, if x is 1 and y is 0, it is stored in the second list, and so on and so forth. Then you can use those lists separately in foreach loops to call whatever methods you're using in your class and everything is happy, without conflicts, it would seem.

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

205 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 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 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 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 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 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 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 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 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 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

ProBuilder Element Snap to Grid (Unity 2020.1.2f1) 0 Answers

Progrids not working with poly shape 0 Answers

I am very new to coding and was wondering how I would be able to turn this square grid script into a rectangle grid. 0 Answers

,Code in loop simultaneously executing on members of array rather than iterating one at a time 1 Answer

Can I attach a grid to a moving GameObject? 0 Answers


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