0 votes

So I made a big amount of code that relies on an array. That array gives the positions of certain objects using the 2d array (array[x][y]). The only problem is that the top left corner of the environment is at -1024,-600. And that is a problem because arrays cannot have negative indexes. So I was wondering if I could move the global origin to -1024,-600 or if there is another work around for my problem, thanks.

asked Jun 24 in Engine by stevepetoskey (44 points)

Using screen positions as array indices seems really strange. I'd guess there's a better way to organize your data, but it's hard to say without some more info and maybe some relevant code.

That said, you could always add 1024 to each X coord and 600 to each Y coord as you store them and then subtract the offsets back out on use...

But, again, the data storage here seems odd to me...

yeah the offset thing was what I was thinking but hoping I wouldn't have to, because changing the global origin would be less work.
As for the reason I am storing the data in a array:
I am doing a tile-like thing where each tile is a 32*32 sprite. The array stores the x y and tile value of said tile (aka array[x][y] = tile value). In order to set a value I divide the mouse position by 32 and round it. Which gives me the corresponding tile data in the array. This method worked fine until I expanded the world and made the top left corner not 0,0 but -1024,-600. Which apparently gave the array a stroke when it tried to compute negative numbers. Instead of godot giving a error when you try to put a negative value for a index in a array, godot just takes the highest index from the array and subtracts it by the negative value. Now this would be fine but with the way I was making my game I did not notice the bug. So I built everything in my game around this bug until recently when I found out that this was happening. Now my choices are: Reset the tile engine and start again but with the top left being 0,0, Remember to add offsets to everything that involves the array, or to change where the global origin is. The last one would be the easiest so that is why I went searching for how to do that. I probably should of said all this at the beginning but it didn't seem to matter at first.

1 Answer

+1 vote

Based on the above conversation, I might change the array to store a cell position rather than a screen position and then create a function that can convert a cell position to a screen position as needed.

That way, the upper-left tile would be stored at array index 0,0. Then you could convert that to screen position -1024, -600 (as in your example) with some pretty simple math...

answered Jun 24 by jgodfrey (5,742 points)

Thank you! This will do nicely

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.