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 / Problem with array lists

Author
Message
Yodaman Jer
User Banned
Posted: 28th Feb 2012 00:31 Edited at: 28th Feb 2012 00:38
Hi guys,

So I've been working on a level editor that heavily relies on array lists to keep track of object properties, what type of object an object is and various other details that my game will need for re-creating the level at runtime.

My problem is that whenever I create an object, I instantly add it to the array list using "array insert at bottom arrayName()", and then I modify certain variables right then and there within the array (like what type of object it is). This works great and exactly how I want it to. So why is it a problem?

The problem shows up when I go to delete an object that isn't the last object in the array list (i.e., the last element in the array list). For example, say I created 10 objects on the screen. I go to delete object 4 out of 10. I do so, and now I delete the element from the array with the "array delete element arrayName(index)" command. But then if I click on object 10, the program crashes because the program is convinced that array element(9) no longer exists!

I thought I could get around the problem by making sure to specify what element each object gets put in to, and then when I go to delete the object I use another variable that holds the currently selected object's number to specify that I want THAT array element deleted. BUT THIS STILL SHRINKS THE ARRAY LIST, even though I specified what element to delete. It doesn't matter, it still shrinks the list.

Is there ANY workaround to this, or do I have to use an element with a set size and then do clever sorting later?

Here's all of my code so far...



My problem lies exclusively in these lines, and the -1 NEEDS to be there or else the program crashes:


So does anyone know of a workaround this problem? Is there a way to make sure that no matter what object I delete, the end of the list still contains the last object I created?

EDIT:

The solution lies in shifting indices down, doesn't it? I just don't even know how to implement that.

WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 28th Feb 2012 02:08 Edited at: 28th Feb 2012 02:09
I couldn't run the code cos I ain't got d3dfunc installed on this yet and not used DBP for some time, but, if you've stored the objects ID in the array list then just running through the array list searching for the objects ID to find which element it is at would work.

If you're deleting element 4 from 10 items, then 5 upwards would all move down one element. And use "ARRAY COUNT( arrname() )" to stop you from going beyond the list.

EDIT: Also note that the size of an array may be return as 10 (which is right) but your data is stored from arr(0) through to arr(9).

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Yodaman Jer
User Banned
Posted: 28th Feb 2012 02:21
The problem is, I don't know HOW to manually shift indices down like that... I get what you're saying but I have no idea how to go about it. Could I possibly get an example please?

MrValentine
AGK Backer
15
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 28th Feb 2012 02:27 Edited at: 28th Feb 2012 02:35
I was wondering whether I would be better off using a database like DarkData for this kind of thing... or is an array best? [I am not concerned about speed if that's the reason]

I see so many issues surrounding Array's that I feel its not an easy thing to use

could be just me

I hope you work this one out Yodaman Jer

EDIT

Bolded, sorry did not spot that no disrecpect intended

WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 28th Feb 2012 02:33
I'll work out an example and re-post here in a short while...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Yodaman Jer
User Banned
Posted: 28th Feb 2012 02:37
@MrValentine:

This is one reason I want to use C# and XNA for my game eventually instead of DarkBASIC, because array/list handling makes so much more sense in C#. DBP just kind of makes it really confusing and very easy to accidentally use the wrong commands by mistake which can give all kinds of errors, at least for me.

On the other hand, though, I want to find the solution to this problem before moving on to XNA/C#. However, if finding a solution ends up being more of a headache/hassle than learning some more C# and XNA then by golly I know what I'm doing...

I think i coded myself into a really tight little corner; I can't seem to make heads or tails of a solution at its current state. I kind of spaghettied it all over the place.

@WLGfx:

Thanks! I'm looking forward to it!

MrValentine
AGK Backer
15
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 28th Feb 2012 02:47
Yodaman Jer... I wish i could jump into those languages but I can not get my head around C/C++ yet... more the includes thing and setting up but I will eventually make the jump into GDK sometime in the near future... and maybe one day who knows something else like XNA or even OGL

[Whips WLGfx]

WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 28th Feb 2012 02:58 Edited at: 28th Feb 2012 02:59


Something like this...

Basically it stores the data in the array but I'm grabbing the arrays size each time I'm going to use it...

EDIT: Erm... Shows up as 6, gonna read up on the ARRAY COUNT() command...

I'm a C/C++ programmer...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
MrValentine
AGK Backer
15
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 28th Feb 2012 03:02 Edited at: 28th Feb 2012 03:03
[Whips WLGfx some more]


here's some coffee buddy

I still would love to see how to use this inside a database if its possible [perhaps once your done with the array method WLGfx you could try DarkData?]

EDIT

Forgot the coffee

WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 28th Feb 2012 03:05
@MrValentine - To use something like this in a database then just like SQL or MS Access the ID field could be the identifier to an element in another array. It's how 99.9% of relational databases work by linking the tables. It's also used for linking objects to data in games more often than not.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 28th Feb 2012 03:09
It looks as if ARRAY COUNT() is returning the size of the array minus one. I was assuming in the above code that it would return the actual count and not the last element number.

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
MrValentine
AGK Backer
15
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 28th Feb 2012 03:12
thanks WLGfx ,

I was thinking earlier is it not possible to FILL IN a deleted array spot? perhaps a small workaround?

you know like stubs for functions

Yodaman Jer
User Banned
Posted: 28th Feb 2012 03:47
I still don't get it. I've tried everything I can think of and it just doesn't work. My program keeps crashing.

It looks like I'll have to tackle this in XNA after all.

Thanks anyway WLGfx!

WLGfx
18
Years of Service
User Offline
Joined: 1st Nov 2007
Location: NW United Kingdom
Posted: 28th Feb 2012 03:57
The bug in the code above that I did before is that ARRAY COUNT() will return -1 if the array is empty and then 0 if the array has one element and 9 if the array has 10 elements.

I'll have a look at your code in the morning because I'll need to dig out d3dFunc dll to use it. Plus I'm ready to head up the wooden hill too.

Fingers crossed you'll be able to get it fixed... Good luck...

Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!
Yodaman Jer
User Banned
Posted: 28th Feb 2012 05:07
Thanks WLG, I think part of my problem is that I coded myself into a neat little corner without much forethought of the program flow/design. As a result, since I'm new to lists and using them correctly, in order to correct the issue I've determined I need to start the editor from scratch after I spend some time planning it out and maybe learning some more array list techniques in DBP.

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th Feb 2012 16:52
That's what the problem is - naive design.

You've linked the object id to it's position within the array. When you delete an item from the array, everything beyond that point shifts down one place.

So either:
1. Don't delete from the array (maybe have an 'inuse' flag within your type).
2. Put the object id into the type and break the link between the object id and the array completely.

Option 1 would be the easiest with your current code.

Yodaman Jer
User Banned
Posted: 28th Feb 2012 22:13 Edited at: 28th Feb 2012 22:15
Quote: " When you delete an item from the array, everything beyond that point shifts down one place."


Ah, so this is why my OBJ(selectedObject - 1).objPosX# code wouldn't work I suppose? Although the "- 1" was added much earlier due to a similar problem (program crashing due to an out-of-bounds error), I thought it would still prevent the program from crashing when I click the last item created (the last item in the array).

EDIT: Ah, yeah, I now see what you're getting at. Because of the way it's set up it definitely can't work the way I've coded it; because I have the IDs linked with the array indices, when I delete an object that isn't the last object in the list, everything shifts down, so my "OBJ(selectedObject - 1)" code ends up breaking the program. Yup, totally coded myself into a tight little corner... END EDIT

Either way, your solutions sound very good so I think I'll re-start the editor with the flag idea in mind. The only problem is that I use the array to save out level data, which is why I wanted them expanding/shrinking for objects; but maybe I just can't have my cake and eat it too, and the more I research this it sounds kind of impossible.

I'll do some more re-coding and let you know how it works out...

Login to post a reply

Server time is: 2026-07-09 20:55:24
Your offset time is: 2026-07-09 20:55:24