Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Having difficulty matching variable data with data in an array.

Author
Message
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 22nd Jul 2011 21:38 Edited at: 22nd Jul 2011 21:49
For some background, I'm working on my map loading/saving system which uses chunks to load specific parts of it at a time. When a chunk is loaded object plains are created for each tile in a grid, and when a chunk is unloaded those objects are supposed to be deleted. I have a single array which stores the ID and location of every object created, which is called arr_Instance_MasterList in the code below.


The first two code sections below are what runs when a chunk is loaded. The program loops through every tile in the array which stores world data (arr_World_Create_AllData) and then plugs that information into the object creation function.

func_Chunk_Load


func_Object_Create_Tile



The next two code sections are what happens when a chunk is unloaded. You'll notice that the first function below is almost identical to the first one above, the only difference being that it calls the function func_Object_Delete_Tile instead of func_Object_Create_Tile.

func_Chunk_Unload


func_Object_Delete_Tile



Now to my problem. When I run the function func_Chunk_Load a 32x32 grid of blocks (each 32x32 pixels) appears, which is functioning correctly. When I run the function func_Chunk_Unload the program tells me that the first array in the code below (taken from func_Object_Delete_Tile) is out of bounds. If I'm correct this means that it has gone through every single item in the array and can not find an instance where all of the location data (X, Y and Z) matches up.




I've gone over everything multiple times and can't track down why I'm getting this error. Using ARRAY COUNT I can see that the correct number of elements exist in the array arr_Instance_MasterList (32 x 32 + 1 = 1025), so somewhere the location data is getting messed up. Hopefully a fresh pair of eyes can sort out what I'm missing.
RiiDii
21
Years of Service
User Offline
Joined: 20th Jan 2005
Location: Inatincan
Posted: 23rd Jul 2011 02:23
Hey Alaror, I am at work looking at this, so I am not looking in detail as I should in order to provide solid advice. But to start with, the common problem I experience is the correct deletion of values from the array when the matching object is being deleted. Or similarly, the addition of a new object over a recently deleted object.

Alternatively, if you haven't checked it out, IanM's Matrix1 utilities dll has a great object grouping and management system. It's often so much better than using arrays. For example, you can group your "chucks" of objects into an object group. Then, when needed, you can delete all the objects in the group without having to search an entire array. You can also add / remove objects from a group without array management. Finally you can determine with a single command if an object is in a given group.
paul5147
20
Years of Service
User Offline
Joined: 11th Jan 2006
Location: Hot & Sunny
Posted: 23rd Jul 2011 07:23 Edited at: 23rd Jul 2011 07:23
Hi Alaror,what did you dim the array size to to start with,if for example you have 10 chunks with an x size of 32,diming an array with 10 x 32 elements for its x data would only give you 320 elements in the array.if you then call your set up routines in the way you have,

using var_chunkx of 10 and var_chunck_size of 32

var_ChunkX_Min = var_ChunkX_Min = var_ChunkX * var_Chunk_Size =320

var_ChunkX_Max = (var_ChunkX * var_Chunk_Size) + var_Chunk_Size - 1 =351

so it would take it out of bounds,try simply increasing the size of your array by 1 chunk size and see if that cures the out of bounds problem
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 23rd Jul 2011 18:59 Edited at: 23rd Jul 2011 19:02
@RiiDii

I'm not sure that's the issue. Did a bit more testing (explained below) which leads me to this conclusion. I'm trying to stay away from using any DLLs, but I'll definitely take a look at it if we can't figure this out.


@paul5147

The only two arrays used in these four functions are arr_Instance_MasterList and arr_World_Create_AllData. The first one is a dynamic array so it's DIMed as arr_Instance_MasterList(). The second depends on the size of the world, but it's DIMed as...



Doing it this way accounts for the fact that arrays start at 0. In the function that loads/unloads chunks I determine the start point as the chunk number (in this case 0, 0) times the number of tiles in each chunk (32).



So as I said I've done a bit more testing which has confused me even more. I tried using the function func_Object_Delete_Tile by itself, starting with the location (0, 0). This deleted both the object on the screen as well as removed the element in the array arr_Instance_MasterList. I then did the same thing but used the outer most locations instead, such as (1024, 1024). Remember, these are pixel widths, so it's the 32nd tile, all of which are 32 pixels wide. To my surprise this worked flawlessly too. What I can determine then is that the function func_Object_Delete_Tile is in itself working properly; the problem has to be somewhere else.

This led me to believe that the issue lies with the function func_Chunk_Unload. I changed the lines of the FOR/NEXT loops of the function so that they would stop at (0, 0), meaning that only one block would get deleted. To my surprise this worked without a hitch. I then kept increasing the numbers until I found that (18, 21) is the highest I can go; if I go any higher I'm given the same error as above (out of bounds, etc). That bit of code is below.




I noticed something else odd... every single block within the range of (18, 21) gets deleted except the one at (1, 1). I've attached a screenshot of that. No errors are given or anything else of the sort; it just didn't delete. I went back and tried using the function func_Object_Delete_Tile by itself again, but this time the object on screen didn't get deleted, but the element in the array arr_Instance_MasterList did get deleted.

Has anyone experienced something like this before or have some advice on how to track down what the problem actually is?
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 23rd Jul 2011 22:06
Quote: "Has anyone experienced something like this before or have some advice on how to track down what the problem actually is? "


Sounds like you are at the point where it might be worth including some printout before the error occurs. Do your own out of bounds check just before the relevant array is accessed and print whatever variables seem to be relevant. Then have a wait key or similar so you see the results before the error occurs. If you wait for the error then you might not see the printout when DBPro exits.

The values of those variables should give you important clues about what might have gone wrong.
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 24th Jul 2011 22:44
@Green Gandalf

Is there any way to export array data into plain text? I tried using the .txt extension and got a garbled mess. The easiest thing for me would be the ability to plug that data into a spreadsheet and do a quick check to see where the data isn't lining up.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 24th Jul 2011 22:59 Edited at: 24th Jul 2011 23:01
I'm not sure of the best way but this should work:



Edit Forgot to add that that creates a plain text csv (comma separated) file which can be opened in either Excel or Notepad and no doubt other spreadsheets as well.
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 25th Jul 2011 07:57 Edited at: 25th Jul 2011 07:58
Before I begin I wanted to mention that I found what was causing that rogue block at the tile location (1, 1). I had added a piece of code to place a single tile there earlier for testing and forgot to remove it


A huge thank you to you Green Gandalf; I was able to use the exported file in an Excel sheet perfectly.

I exported two files for comparison: one with the location data in arr_Instance_MasterList and one that had all the location data produced by the function func_Chunk_Unload. What this gave me was the data of the two things I compare when I get the "out of bounds" error. I put both these in the same Excel sheet and compared every value to see if there was any point where the data didn't match. What I found is that the location values matched perfectly. There was the exact same number of objects in both files, and these objects had the exact same location data. This is what I expected to happen, seeing as the function is working perfectly by itself. I can delete any object within the array individually using func_Object_Delete_Tile.

Now, remember how I said that I ran some tests earlier and was able to delete objects using func_Chunk_Unload as long as the parameters were the same as below?




Well I found two other combinations that work:






If I try to increase the amount after "TO" by 1 for any of these I get the "out of bounds" error. There is no relationship between these ranges that I'm able to see, and note that in the last one I'm even able to delete the max amount of Y values within the chunk.

This has to be an issue with Darkbasic Pro; I can't fathom what else it could be. Where do I go from here?
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jul 2011 14:27
Quote: "I can't fathom what else it could be. Where do I go from here?"


Are you doing what I suggested earlier? You should be able to trap that error before it is encountered by DBPro, i.e. before DBPro tries to access the offending array element. Print the values that DBPro has and inspect them.
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 25th Jul 2011 20:04
@Green Gandalf

I did, but from my test the array position and data all exists when I do it. I used the bottom most code snippet from my previous response, and as soon as it gets to the point where it would be 16 for the X position it tells me I'm out of bounds.

One thing I noticed though as I was going through is that the array position increases by one every time an object is deleted. What I mean by this is that if I start with five elements in the array and use the command ARRAY DELETE ELEMENT, shouldn't it move the entire array up by one, making the new total of elements 4? It appears that it's deleting the element where the data exists, but doesn't move any of the other data up through the array.

Right now I'm using the following to delete the elements:



It looks like there are two different ways you can format that command, the other being



When I use this one I'm given an error that says it could not understand the command. Might this have something to do with it?


Oh also, I tried the code in another IDE (CodeSurge) and have the exact same problem. I guess that means it's not a problem with Darkbasic Pro
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 25th Jul 2011 22:34
This:


Should be this:


It's also the version of the command you should use.

The other version does not do what you think it does:


First, the outer brackets are unnecessary. Second, the variable 'var_Array_Position' is ignored - this command deletes the 'current' item in the array, which will be the last element added (if you extended the array using the ARRAY INSERT XXXX commands), or the zeroeth item if you just used the DIM command, or the item selected if you've used the PREVIOUS ARRAY INDEX/NEXT ARRAY INDEX commands.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jul 2011 22:48 Edited at: 25th Jul 2011 22:50
Quote: "I used the bottom most code snippet from my previous response, and as soon as it gets to the point where it would be 16 for the X position it tells me I'm out of bounds."


Well, it should shouldn't it?



Quote: "One thing I noticed though as I was going through is that the array position increases by one every time an object is deleted. What I mean by this is that if I start with five elements in the array and use the command ARRAY DELETE ELEMENT, shouldn't it move the entire array up by one, making the new total of elements 4? It appears that it's deleting the element where the data exists, but doesn't move any of the other data up through the array."


I'm still a bit confused here. Are you using multi-dimension arrays? If so, how does ARRAY DELETE ELEMENT work? [Note to self: do a few tests to find out. ]

Quote: "It looks like there are two different ways you can format that command, the other being"


So there are. In fact I've no idea what the first one is supposed to do. The second however is quite clear from the Help file as this example demonstrates. The array elements are indeed moved up one as you'd expect.



Incidentally you've added round brackets which shouldn't be there - hence your error message I suspect.

Edit IanM posted while I was testing my snippet.
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 25th Jul 2011 23:01 Edited at: 25th Jul 2011 23:05
@IanM

Thank you so much! It's working perfectly now. Seriously man, you made my day


EDIT: @Green Gandalf

Haha, you posted as I was replying to him. I appreciate all the time you took to help figure this out. I can finally move on with the project!
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 25th Jul 2011 23:25
Thanks. Good luck with the rest.

Perhaps I should just sit back and let IanM deal with all these questions - he keeps beating me to it.
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 25th Jul 2011 23:37
Nah, don't do that - the help you provide to everyone here is absolutely invaluable.

Besides, right now, I've not got time (or energy) to do much more than hit-and-run type answers - If you hadn't helped Alaror up to this point, he wouldn't have got things to a point that fit my current answering profile

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 26th Jul 2011 01:40
Quote: "Nah, don't do that"


I won't.

Login to post a reply

Server time is: 2026-07-11 03:37:41
Your offset time is: 2026-07-11 03:37:41