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 / Using an array as a parameter in function.

Author
Message
Dark Dragon
19
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 23rd May 2010 00:50
Hey all,


DD here. Just writing a few generic functions for my project and ran into a problem. Look here:




The VARIABLEARRAY is what i'm talking about. How can I get DB to replace the name of VARIABLEARRAY within my function? What i want is somthing like...




Any way to do this?

Cheers, DD

baxslash
Valued Member
Bronze Codemaster
19
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 23rd May 2010 12:09
I don't think there's a way to do this, sorry!

There may be a way to replicate the functionality using Matrix1 utility plugin (I think it uses "Memory Banks") but I've never tried using them...

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 23rd May 2010 13:11 Edited at: 23rd May 2010 13:12
Or pass a memblock?

EG. if you have an array of 1000 floats, you can use a memblock instead:
make memblock 1, 1000*4

float number 345:
Float345 = memblock float(1, 345*4)

or writing:
write memblock float 1, 345*4, value#

It doesn't matter what you write in the memblock. The type can be different every time since a memblock is just a chunk of memory. It's just that DBP makes it very easy to read and write in this memory.

Cheers!
Sven B

Dark Dragon
19
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 23rd May 2010 18:25
@ Sven B

Would you mind explaining this in more detail? I've........always avoided memblocks as I hated them for some reason.

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 23rd May 2010 19:11 Edited at: 23rd May 2010 19:15
Sure:

Memblocks are basically chunks of memory that you can read from and write to. They have a certain size in bytes that is specified when they are created.

eg. make memblock 1, 1024
This command makes a memblock with ID 1 with a size of 1kB (1024 bytes).

Once this memory is created you can write to it or read from it using the commands:


For all these commands, Position is the position in the memblock in bytes. A WORD, a DWORD and a FLOAT however use more than one byte, and so they will also use more than one byte when written in/read from the memblock. Then it simply uses the following bytes.
A DWORD for example uses 4 bytes, so when you use write memblock dword 1, 5, value you actually write over bytes 5, 6, 7 and 8. It's the same for reading.

A memblock does not "know" which data is written at which position, so you can easily mix up data types if you aren't careful. This can also be used to your advantage though. For example, a color is a DWORD that has 4 bytes containing the red, green, blue and alpha component. This example writes the full color DWORD to a memblock and then reads only the 4th byte that contains the alpha component of the color.



That are the basics for memblocks. IanM's memory banks are exactly the same, but support more data types than memblocks and some more extra's (like searching in them). IanM also made it possible to convert memory banks into memblocks and vice-versa.
DBP also allows you to convert bitmaps, images, sounds, meshes and arrays into memblocks (and vice-versa). The structure of such a converted memblock depends on the media type. If you want to learn more about these I suggest searching the forum.

Now, what I meant in your case was that you could pass a memblock number in your functions, and use that as an array. For example, let's say I'd want to pass an array of 14 floats, then I would do something like:



There is one other possibility you might want to check out, and that's using make memblock from array:



I hope it helps.

Cheers!
Sven B

Dark Dragon
19
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 23rd May 2010 22:23
Hmmmm.......You know, This might be exactly what I need. I'd like to thank you, Sven B, This is going to help out a whole lot. The explaination is awsome,btw. I think I get it.



THANKS!

DD

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 23rd May 2010 23:02
I'm glad I could help

Sven B

Dark Dragon
19
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 23rd May 2010 23:27
Quote: "
There is one other possibility you might want to check out, and that's using make memblock from array"


Definatley my savior. I'll proboly post all my functions(They load in Images to free img numbers and Sprite sheets free arrays...) later, or maybe in a day or two....They need to be cleaned up.

Thanks again!

Login to post a reply

Server time is: 2026-07-25 16:22:10
Your offset time is: 2026-07-25 16:22:10