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.

Newcomers DBPro Corner / Removing blank data from an array

Author
Message
300happy
16
Years of Service
User Offline
Joined: 21st Feb 2008
Location: Indiana, USA
Posted: 5th Jun 2009 01:11
Ok, I have an editor running with an array of objects. This looks somewhat like the following:

obj1,xpos,ypos,zpos,color,ect.
obj2,xpos,ypos,zpos,color,ect.

Now, when an object is deleted, I reset that array entry to "". This keeps things running in the editor, but obveously will eventually run out of IDs(I have a rather dirty system in place that will hopefully keep the ids between 1000 and 7000 for now)

Anyway, one solution I thought of was to remove the blank entries when the array is being saved. This would keep the array as a small list of what IS in the level, without saving "" lines for everything that WAS in the level but is now deleted. Basically instead of this:

obj1
obj2
BLANK SPACE WHERE obj3 WAS
obj4

I want this:

obj1
obj2
obj4

I haven't figured out how to get the length of the array for looping though. Is there a way to loop through an array, any array, without knowing the maximum limit of the array?

Basically my current problem is that the second array(the one with the deleted lines removed) becomes impossible to loop through once I remove anything from it. Since it no longer is the same length as the original, all the loops crash out with a "invalid array entry" type error because they still expect the blank rows to offset everything.

Anyone know of a solution to this, without doing something dumb like rebuilding the entire array each time I use save array?
Vathelokai
15
Years of Service
User Offline
Joined: 3rd May 2009
Location:
Posted: 5th Jun 2009 01:33 Edited at: 5th Jun 2009 01:34
From the core commands in the help file...



I hope this helps.


As a side note, Your camera snippet in the Code Base helped me out a lot.
Rudolpho
18
Years of Service
User Offline
Joined: 28th Dec 2005
Location: Sweden
Posted: 5th Jun 2009 01:48 Edited at: 5th Jun 2009 01:49
You could always store the array index to the saved file before each set of actual data, if that is relevant. Otherwise, just read in what you have from the file and make a new array based on that when loading.

It is always clever to save the length of an array to a file as well, if the array is supposed to be identically recreated at a later time.

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 5th Jun 2009 03:12
If you have a one dimensional array you could use ARRAY DELETE ELEMENT Array(0),slot to delete.

<-- Spell based team dueling game!
Cagedrei
15
Years of Service
User Offline
Joined: 18th Mar 2009
Location:
Posted: 5th Jun 2009 07:59
Quote: "Basically my current problem is that the second array(the one with the deleted lines removed) becomes impossible to loop through once I remove anything from it. Since it no longer is the same length as the original, all the loops crash out with a "invalid array entry" type error because they still expect the blank rows to offset everything."


Try looping backwards on the array while you remove elements. Odd things can happen when you try to iterate forwards over a shrinking array, in my experience.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 5th Jun 2009 19:31
You can add another variable to the array that's just a 1 or a 0 to represent if the object is used 1 or is deleted 0. That way you can reuse them when you create new objects by looking for the first zero in the list of valid/deleted objects.

bobbel
15
Years of Service
User Offline
Joined: 5th Jan 2009
Location: In my DBPro case xD
Posted: 5th Jun 2009 20:25
about the array index valid and array delete element commands, my experience is that they don;t work, and if they do they don;t do it good, so i suggest to just do it Grog's way cause thats easier to code i think

*ding ding*
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 5th Jun 2009 23:06 Edited at: 5th Jun 2009 23:06
The array delete element command works, it just doesn't work on string or float arrays (arrays that have a $ or # sign, I just figured this out when trying to make an example). But User Defined Types can help get around that. Here's an example it uses array delete element and array count:



<-- Spell based team dueling game!
bobbel
15
Years of Service
User Offline
Joined: 5th Jan 2009
Location: In my DBPro case xD
Posted: 5th Jun 2009 23:49
oh that works, thanks Sixty Squares!

*ding ding*
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 6th Jun 2009 01:12
No problem

<-- Spell based team dueling game!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Jun 2009 00:38
I'd like to see some examples of where arrays DON'T work - I'm currently not aware of any issues with arrays, and the 1D array commands.

Even the 'arrays with strings' works just fine for me:


I'm pretty sure that all the next/previous/valid commands and functions work too - I use then regularly.

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 9th Jun 2009 02:48 Edited at: 9th Jun 2009 02:49
IanM

By "Arrays with Strings" I meant this:



But I hadn't thought of doing it your way

<-- Spell based team dueling game!
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 9th Jun 2009 15:23
Just tried it - it works whether I use $ or 'as string'.

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 9th Jun 2009 22:45
Strange, it gives me that "dbp_program.exe has encountered a problem and needs to close. We are sorry for the inconvenience." error... (DBP v7.2)

<-- Spell based team dueling game!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 10th Jun 2009 22:59
When deleting arrays with strings, use:

free string$()

Otherwise you will suffer from memory leakage.

Try not to keep deleting and creating array elements, as discussed here:

http://forum.thegamecreators.com/?m=forum_view&t=141494&b=1

And preferably, use a stack system as described in the editorial here...

http://www.thegamecreators.com/data/newsletter/newsletter_issue_72.html

I've been down this path, and all of the above is invaluable and worth doing before it's too late.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 11th Jun 2009 05:53
Once I make a string it's my slave till the end of the program. They can dream about freedom all they want.

Login to post a reply

Server time is: 2024-11-24 05:42:05
Your offset time is: 2024-11-24 05:42:05