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 / passing array name to a function?

Author
Message
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Jan 2011 21:02
Is there any possible way to do this? Best way I can explain it is through a basic example:



I just need some way for my function to interact with a user-defined array. I don't know if DB's pointers would allow me to do something like this or not.

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 4th Jan 2011 22:25
:S I don't think there is a way to...

Whenever I've needed to do something like this, I use memblocks or ianm's memory commands. I've never dealt with DBPro's built in pointers... so I'm not sure. I think I have some functions lying around that might be useful if you want to go in the matrixutil/memory route, but I'd have to dig them up.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Jan 2011 22:42
I'm trying not to use any plugins for this, but thanks.

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 4th Jan 2011 23:22
It's only possible with plug-ins (mine to be specific).

Here's an example, modelled on yours:


So it's possible, but it's not nice and a little too easy to get things wrong if you need to resize arrays and pass them around at the same time.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 4th Jan 2011 23:38
I can avoid dynamically resizing the array if I loop through my data twice. Maybe that'd be quicker anyway rather than a single loop which may keep resizing the array size? (not ever element in the loop is added to the array, its conditional)

"Only the educated are free" ~Epictetus
"Imagination is more important than knowledge..." ~Einstein
Diggsey
20
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 5th Jan 2011 02:21
Alternatively, if you know the upper bounds for the size of the array, you could set it to this size initially, and then resize it down at the end.

[b]
Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 20th Jan 2011 01:51 Edited at: 20th Jan 2011 04:53
IanM, this pointer system looks to be just the thing I need for my project!

Can I double check that basically, I can now create say five 2D arrays that all require processing via one common function, and by using your pointer system I can now make this happen?

This is opposed to writing 5 very similar functions that are each exactly the same, with the only different being that the array name in each function changes for each array?

So with your pointer system, the below should work...



Although I'm yet to properly test it with 2D arrays, I believe it should work. But I'm also wondering about ways to simplify this further, if possible. Maybe with the use of some string variables, or perhaps another function?

EDIT: I can get pointers working with 1D arrays, but not 2D arrays. Could you hint at any tips?

EDIT 2: Here's an example to show you what I'm doing. This actually works well, so I'm wondering if I should implement this method, or attempt Array Pointers again?



EDIT 3: Gah. I shouldn't rush my code when I get excited! I found the problem (forgot to link the array back!!) and now the pointers are working perfectly for 2D arrays. Very cool. Now I can manage my code much-much easier!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 20th Jan 2011 14:49
All that you want, you can do. You don't even need to pass the array dimensions around, as these are accessible from the array or arrayptr: GET ARRAY DIMENSIONS & GET ARRAY COUNT

Burning Feet Man
18
Years of Service
User Offline
Joined: 4th Jan 2008
Location: Sydney, Australia
Posted: 22nd Jan 2011 00:57
Ian, after spending the morning coding using your pointers, I can't thank you enough for them. They've absolutely slashed the coding required to make a decent game. Using your tools and advice, I now have coded the below to basically do what ever I want to in game objects;



It's absolutely brilliant.

Login to post a reply

Server time is: 2026-07-18 02:08:53
Your offset time is: 2026-07-18 02:08:53