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 / Multidimmensional array manipulation?

Author
Message
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 19th Aug 2015 23:57
So I have an array,

Cargo(nPlayers, 9)

I'd like to use the commands,

array insert at bottom Cargo(0, 0)
array delete element Cargo(0, 0), x

but it looks like it wont let me.

I know there are a number of ways to work around this, but none of those are quite as elegant or efficient.

Like a good little programmer I'm setting up the arrays empty, then adding an element as I go and deleting them when they are no longer needed in order to save memory.

Is there an option that doesn't involve creating,

type cargo_data
id as integer
contract as integer
limb as integer
objid as integer
endtype
global Cargo(0) as cargo_data

and having to loop through the entire array every time I need to find all the player's cargo data searching by Cargo(x).id ??
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 20th Aug 2015 10:33 Edited at: 20th Aug 2015 10:37
For this I suggest using IanM's array linking commands, they have proven to be a very effective workaround for the lack of arrays within UDTs while also offering some unique advantages. Here is an example of what I'm talking about:



If you have any questions feel free to ask.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 20th Aug 2015 18:50
That's just what I was looking for. Not sure what I would do without the Matrix1Utils.

Thanks for the great example.
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 20th Aug 2015 19:04
Glad to hear it man, I'm always happy to help.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 20th Aug 2015 23:55
Just to make sure I'm getting this...



and this will link a Cargo array with indexes 1-8 to each player?

Due to that, I'm guessing that when I remove a player from the game, the Cargo array can be removed and free up memory with...



If that's the case, and it's a pretty fast method, which I'm sure it is from IanM, then this is perfect.
revenant chaos
Valued Member
17
Years of Service
User Offline
Joined: 21st Mar 2007
Location: Robbinsdale, MN
Posted: 21st Aug 2015 11:52 Edited at: 21st Aug 2015 12:02
Yep that looks good for the most part (aside from "unlick array" ). The only problem I see is that you need to update your pointer to the cargo array after deleting it's elements. Any time you insert/delete elements from an array, the array's location in memory changes and so you need to store a copy of it's new address.



However removing all elements from the cargo array won't free up 100% of its memory, so a slightly better and perhaps faster approach would be to delete the array entirely using undim arrayptr.

KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 21st Aug 2015 18:49
Yeah, I was running that problem yesterday. It made it past the first delete, then crashed.

Thanks again. This took a lot of work yesterday to comb through 12,000 lines of code and make the changes, but it's going to save a lot of processing cycles over what I was going to do.

Login to post a reply

Server time is: 2024-04-24 07:25:34
Your offset time is: 2024-04-24 07:25:34