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 / A Tutorial on Arrays

Author
Message
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 26th Jul 2006 13:51 Edited at: 21st Jun 2008 21:32
This assumes you already know what a variable is...

This should get you started with arrays.

What is an array? How do I make one?

An array can be thought of as a dresser with boxes inside:

The first number you specify is how many dressers there are.
The second number is how many cabinets are in each dresser.
The third number is how many drawers are inside each cabinet.
The fourth number is how many boxes are inside each drawer.
The fifth number is how many mini-compartments each box has.

You can only specify up to 5 different numbers or sections. However, you do not need to specify that many.
In fact, you could just specify 1 if you wanted to. If you wrote

dim Array(1)

it would create an array with one dresser. The DIM command is used for creating arrays. To erase an array, use UNDIM. You can call an array anything you want. I want to call it "hat" now. If you wrote

dim Hat(1,2)

it would create an array named HAT that has 1 dresser and that dresser would have 2 cabinets.

dim Hat(5,3)

it would create an array named Hat that has 5 dressers, each one having 3 cabinets.

dim Hat(5,3,4)

That makes an array named Hat that has 5 dressers, 3 cabinets in each dresser, and 4 drawers in each cabinet. We could keep on doing this, until we got to boxes with sections inside. I'm going to keep moving on though.


How to put stuff inside arrays


Here is where arrays come in useful: You can put stuff inside them! Here's an example:
Say you had an array named Hat again. Hat has 2 dressers and 5 cabinets in each dresser. And say you wanted to put the number 5 inside the third cabinet on the second dresser.

Well, you'd create the dressers and cabinets like this:

dim Hat(2,5)

and then you'd put that number five into it like this:

Hat(2,3)=5

It's that simple! notice how the first number says it's the second dresser and the second number says that it is the third cabinet. You can just as easily put variables inside arrays as well:


RaceCar=5
Hat(2,3)=RaceCar


You've put the variable called "RaceCar" (don't ask lol) inside the third cabinet in the second dresser.

You are not limited to only numbers though. Just like variables, arrays can take many different shapes or forms; three of which are INTEGERS, STRINGS and REAL numbers, also known as FLOATS. The one that we have been using can hold only INTEGER numbers. You could make an array that is meant to hold only REAL numbers. You could also make one that is meant to hold only STRINGS!

To make an array that holds REAL numbers (the ones with decimal points), write this:


dim Hat#(2,5)


Notice the "#" sign after "Hat". That's what makes it a real number holding array. Now how would you make one that holds strings? Take a minute to figure it out, then open the snippet below.



It's that "$" sign for strings! You would assign these arrays values (in other words, you would put things inside these arrays) in the same way as you would with our old array. For real numbers, write

Hat#(2,3)=5.4

With variables, it's


RaceCar#=5.4
Hat#(2,3)=5.4


The same goes for string arrays too.

Hat$(2,3)="This is a string cool!"

With variables, it's


RaceCar$="This is a string cool!"
Hat$(2,3)=RaceCar$



Alright, here's a final example that creates a STRING array with 5 dressers and 4 cabinets and prins what's in dresser 2 cabinet 3:



And here's an example of how to use variables to do it:





That's all for now, I'm in a rush so I may add some more later. Anyway, tell me what you think so far it's my first tutorial.


EDIT: Alright, I've decided to add more.


Saving and Loading Arrays

Saving arrays and loading arrays is not very hard at all. How do you do it? You can use the "SAVE ARRAY" and "LOAD ARRAY" commands, that's how! However these 2 commands are not as simple as they look. So, to get around this, let's try to save our old pal "Hat". Remember Hat? The last time we saw him he had 5 dressers and 4 cabinets. To create him we had written:

Dim Hat(5,4)

Now to save him in a file. We can actually save Hat as whatever name we want to! In fact, he could be called: "Ping Pong Table". Secondly, we need to tell the command that we want to save Hat. Now the SAVE ARRAY command is one who isn't so bright. All he can know is how many dressers Hat has. If you tell him anything else he'll return some kind of annoying error. When you save arrays, they will appear as a file in its folder like everything else. You can go and browse your projects folder or something and find it there. So, in order to save Hat as "Ping Pong Table", we can write:

Save Array "Ping Pong Table",Hat(5)

Notice how I did not include the number of cabinets, just the number of dressers. The same goes for loading the array. IF you have already saved an array, you can load it again using the "LOAD ARRAY" command! To load Hat, just replace Save Array with Load Array! Both SAVE ARRAY and LOAD ARRAY are both not bright enough to handle more than the number of dressers that Hat has. Keep in mind that loading an array will overwrite what it has in it at the moment, and replace it with whatever you are loading. So, to load hat (you must have already saved Hat, of course) we would write:

Load Array "Ping Pong Table",Hat(5)

And that would load ALL of the contents of Hat into Hat again. You must have already created the array you are loading into (here it's Hat) using the DIM command though. You do not have to load into Hat though, even when you saved Hat! If you made another array called "PLANK" with the EXACT same number of dressers and the exact same number or cabinets in each dresser, you can actually load Hat into this new copy of Hat! For example, if you created "Plank" in the same way we did hat, like this:

Dim Plank(5,4)

We could load "Ping Pong Table" into Plank instead of Hat, like this:

Load Array "Ping Pong Table",Plank(5)

You can save and load String and Real number arrays in the same way! Say you made a string like this:

Dim Hat$(5,4)

You would save it like this:

Save Array "Ping Pong Table",Hat$(5)

All you need to do is add that "$" sign for STRING!

To load, do this:

Load Array "Ping Pong Table",Hat$(5)

And again, there's just that $ sign for STRING. The same goes for real numbers, but if you really need an example, here you go:

Dim Hat#(5,4)

There we create the array as a REAL number using that # sign.. and here is how we save it:

Save Array "Ping Pong Table",Hat#(5)

And here's how we load:

Load Array "Ping Pong Table",Hat#(5)


More Filename stuff

Did you know that you can use variables to save arrays as well!? Well, you can, and you can do it like this:


HatNeedsSaving$="Ping Pong Table"
Save Array HatNeedsSaving$,Hat(5)


You can also save it inside folders! You could save it in a folder called "Arrays" like this:

Save Array "Arrays/Ping Pong Table",Hat(5)

In order to save it in a folder called Arrays using variables, you could write:


HatNeedsSaving$="Ping Pong Table"
Save Array "Arrays/"+HatNeedsSaving$,Hat(5)


Or, you could write:


HatNeedsSaving$="Arrays/Ping Pong Table"
Save Array HatNeedsSaving$,Hat(5)


Both do the same thing. The filename is just like any other string. You can manipulate it using the STRING commands! This includes combining strings, like above! This would save it as "Table" by taking a part of the string "Ping Pong Table"!


HatNeedsSaving$="Ping Pong Table"
Save Array right$(HatNeedsSaving$,5),Hat(5)




And that's all I have to say (for now )! To finish up, here are some examples:

A FULL EXAMPLE:


Here's a very simple example. It's meant more for looking at, not running:




And if you wanted to, you could even make your own file extensions! You could make Hat into a .pingpong file if you wanted to. All you'd need to do is add a ".pingpong" to the end. To save that, you'd write:

Save Array "Ping Pong Table.pingpong",Hat(5)

and to load it, simply write:

Load Array "Ping Pong Table.pingpong",Hat(5)

There you are. Custom file extensions, to help you read arrays through your programs. This way, you could have one game called "The Big Car", and have it's array data saved as a "TBC" file, that way you could reuse the same name but just change the extension! Here's another way you might do it using variables:

First, you may specify the name of the file itself:
HatNeedsSaving$="Ping Pong Table"

Second, you might store the name of the extension in a variable.
HatExtension$=".pingpong"

Then, to save that you'd write this:
Save Array HatNeedsSaving$+HatExtension$,Hat(5)

So, in total that looks something like:
HatNeedsSaving$="Ping Pong Table"
HatExtension$=".pingpong"
Save Array HatNeedsSaving$+HatExtension$,Hat(5)


And that's all there is to that!

Indie Rock 13
20
Years of Service
User Offline
Joined: 1st Sep 2004
Location:
Posted: 29th Jul 2006 12:09
lol, I love how this tutorial lays it out as plainly and completely as possible, almost like its talking to a dumba$$. That's EXACTLY what I need!

I now feel 100 times more confident in my understanding of arrays. Other tutorials just skip into the details too quickly and don't show examples. Must... have... examples...

Thanks!

Neeeeeeeewom!
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 29th Jul 2006 12:49 Edited at: 29th Jul 2006 12:53
yay it helped somebody

EDIT: Glad to hear it!

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 29th Jul 2006 14:46
Okay I've updated the tutorial. It includes Saving/Loading arrays now.

Indie Rock 13
20
Years of Service
User Offline
Joined: 1st Sep 2004
Location:
Posted: 29th Jul 2006 17:21
Cool, that gives me a better idea of how to make a save game file *thumbs up*

Neeeeeeeewom!
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 29th Jul 2006 17:41


Acolyte Entertainment
19
Years of Service
User Offline
Joined: 28th Dec 2004
Location: Oregon, US
Posted: 1st Aug 2006 02:37
nice tut bro. most tutorials give a little glimps of them but now i fully understand it. lol, the dressers/closets/cabinets thing had me confused . hehe, thx

-snow

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 1st Aug 2006 03:32
Yeah, I kinda messed up there (Dressers don't have cabinets!) I couldn't think of anything else and I was in a rush lol.

Thanks for looking at it!

flock
18
Years of Service
User Offline
Joined: 10th Mar 2006
Location: Earth
Posted: 1st Aug 2006 04:47
Very good tutorial. You should write more on other aspects of programming.

a.k.a. "flockhound"
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 1st Aug 2006 04:49 Edited at: 1st Aug 2006 04:50
Thanks! I wrote part of this one for one person, but then I decided to make it into a tutorial for everybody! Maybe I will write more. Just not now.

Gen
20
Years of Service
User Offline
Joined: 6th Jul 2004
Location: Oklahoma, USA
Posted: 1st Aug 2006 05:33
Quote: "Yeah, I kinda messed up there (Dressers don't have cabinets!) I couldn't think of anything else and I was in a rush lol."


Use file cabinet...

Great tut, I have seen one other that was pretty good but this one is a little more informative and has more information.

Windows XP Pro Service Pack 2, Pentium 4 3.0 GHz, 2048 MB Daul Channeled PC3200 Ram, ATI Radeon X700 Pro 256 MB
Graiskye
18
Years of Service
User Offline
Joined: 29th Jul 2006
Location:
Posted: 1st Aug 2006 10:59
Thanks helped with my understanding of arrays a lot.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 2nd Aug 2006 12:37
Thanks for the compliments!

sadsack
20
Years of Service
User Offline
Joined: 27th Nov 2003
Location: here
Posted: 2nd Aug 2006 21:40
very nice sixty Squares
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 3rd Aug 2006 16:51
Thanks

Jaded Gamer
18
Years of Service
User Offline
Joined: 19th Apr 2006
Location: Iceland
Posted: 8th Aug 2006 17:17
This is a GREAT tutorial! I had no idea about the save and load arrays, what a wonderful bit of knowledge you have imparted here! I wish we could tag forum messages with a heart because I would surely tag this!

Thank you!

"So it goes."
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 8th Aug 2006 17:22
Thank you very much

Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 8th Aug 2006 18:57 Edited at: 8th Aug 2006 18:58
It seems you've helped a few people here. However, I must point out that one thing you've said is not correct and could be a little misleading.

Quote: "Just like variables, arrays can take on 3 different shapes or forms"


Variables can take on more than three forms. There are:
Real Numbers, Integers, and Strings (as you've mentioned) but there is also:
Booleans
Bytes
Words
Dwords
UDT's



Jaded Gamer
18
Years of Service
User Offline
Joined: 19th Apr 2006
Location: Iceland
Posted: 8th Aug 2006 19:52
I have a question.

Can the save and load array work with types?

example:


If so, how?

Thanks in advance

"So it goes."
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 8th Aug 2006 21:34
You've already written most of the code you need to test it and find out. Just put a value into the array, save it, delete it and then load it and see if your value is still there. If it's there then your answer is yes, otherwise no.


Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 8th Aug 2006 21:43
Ah, thanks Scraggle. I'll change that.

Jaded Gamer
18
Years of Service
User Offline
Joined: 19th Apr 2006
Location: Iceland
Posted: 9th Aug 2006 04:39
Sadly I can't get it to work with my types example, I guess because I use both string and byte data. It gives a message error 54 - Array type invalid.

"So it goes."
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 9th Aug 2006 14:50
Not sure if Save Array works on types, but you could always transform it into a .TXT or .DAT or another file and then load it into the array again using the file commands

Gamedesign er20
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: The one place you would never look...
Posted: 17th Aug 2006 22:40
Thanks so much, this tutorial really helped alot. I had no idea about the true features of arrays until I read your tutorial.

Cocacola and Pepsi aren't that differnt. Deal with it.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 17th Aug 2006 22:48 Edited at: 17th Aug 2006 22:53
thanks for looking at it! I'm glad it helped you

Jaded Gamer
18
Years of Service
User Offline
Joined: 19th Apr 2006
Location: Iceland
Posted: 21st Aug 2006 01:14
I think it would be helpful for a lot of people if this were to become a sticky or at least get a link through the tutorials.

"So it goes."
Gamedesign er20
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: The one place you would never look...
Posted: 21st Aug 2006 01:22
And you should you add to your tutorial how you can use the data command and turn your arrays into a grid type of map.

Cocacola and Pepsi aren't that differnt. Deal with it.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 21st Aug 2006 05:18 Edited at: 21st Aug 2006 05:18
Thanks Jaded


Quote: "And you should you add to your tutorial how you can use the data command and turn your arrays into a grid type of map."


You do that how?

MatriX
21
Years of Service
User Offline
Joined: 19th Oct 2003
Location: Deer Park, New York
Posted: 21st Aug 2006 10:11
Very nice indeed. We need more like this!

Thank you TGC for creating such amazing products!

Success does not come with meeting the minimum requirements.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 21st Aug 2006 11:23
Great Tutorial! Very Clear and complete!

Enjoy your day.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 21st Aug 2006 16:19
Thank you

Gamedesign er20
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: The one place you would never look...
Posted: 21st Aug 2006 21:09
originally posted by Sixty Squares
"You do that how?"

If you don't know how to do it, i'll write a quick tut, but it's really very simple. Basicly you are using two colors, and then saying if array(whatever you want to put in)=1 then put one of your two colors there, and you would use the data command to place your numbers in a grid style
for example:
data 0,0,0,0,0,0,0
data 0,1,1,1,0,0,0
data 0,0,0,1,0,0,0
data 0,0,1,1,1,0,0
data 0,0,0,1,1,1,0
data 0,1,1,1,0,0,0

Gamedesign er20

Cocacola and Pepsi aren't that differnt. Deal with it.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 21st Aug 2006 21:16

I guess I'll have to learn how to use the DATA command then...

Gamedesign er20
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: The one place you would never look...
Posted: 22nd Aug 2006 00:13
Try this



Cocacola and Pepsi aren't that differnt. Deal with it.
Daemon
18
Years of Service
User Offline
Joined: 16th Dec 2005
Location: Everywhere
Posted: 22nd Aug 2006 00:19
Just wanted to point out that the data statements of Gamedesign er20's code can be combined into 1 line, or they could be seperated into 100.



read simply takes the next thing from the list of data staments. You can use restore to start the read at a specific label.

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 22nd Aug 2006 00:55 Edited at: 22nd Aug 2006 00:56
Ah I see thanks guys. It's a bit complex but I made my own example now. I'm starting to get it...:



Cave Man
18
Years of Service
User Offline
Joined: 22nd Aug 2006
Location: North Carolina, US
Posted: 2nd Sep 2006 00:58
Nice one 60[]s
Unforunetly i already know how to use arrays...
But i will tell others about this great tutorial

Jaded Gamer, i experimented. It seems you can't savee UDT arrays
which just plain stinks...


Language: DBP 6.2
Cream cheese is your enemy. Sweet tea is your freind.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 2nd Sep 2006 04:18
Thank you, Cave Man.

Quote: "60[]s"

Heh, clever. Never seen that before.

Quote: "But i will tell others about this great tutorial"

Thanks!

DarkMasta
18
Years of Service
User Offline
Joined: 21st Apr 2006
Location: Typed on your screen.
Posted: 2nd Oct 2006 10:59
Quote: "60[]s"


I don' get that one...

Wherever you plan to go,
it's the next step you never know.
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 2nd Oct 2006 14:22
[] is a square. Look at my name, you should get it.

Ummm...
C0wbox
18
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 30th Oct 2006 02:32
Is it possible to put arrays into arrays?

like in 1 of those cool miniboxes inside a box inside a drawer inside a cabinet inside a dresser - put another array so you have a miniature dresser inside a minibox making even minier cabinets, drawers, boxes and miniboxes.

????????
Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 30th Oct 2006 02:44
Your question confuses me...

Tutorials:
Array
Functions
C0wbox
18
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 30th Oct 2006 14:01 Edited at: 30th Oct 2006 14:12
Lol, is it possible to put an array in an array:
|_|
|_|_|
|_|_|_|_|
|_|_|_|_|_|_|_|_|
.....|
Another array:
|_|
|_|_|
|_|_|_|_|
|_|_|_|_|_|_|_|_|

To create more tiers of the larger array so you'd have a mini dresser in the miniboxes of the fullsize dresser.

These ASCII art arrays may not work.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 30th Oct 2006 20:28
Nope... but you can use an array to reference another array.

C0wbox
18
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 31st Oct 2006 14:20
Is there a maximum number of boxes you can use in an array?

I'm using arrays to store information for a whole level each and I have the object maximum set to 255. It keeps dieing when I put objects too high up but I thought maybe it was actually to do with the number of objects I'm using in my array.

Is there a limit?
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 31st Oct 2006 22:53
Yeah it probably has to do with the number of objects. Most of the time with arrays the limit is based on the amount of memory you have in your computer.
C0wbox
18
Years of Service
User Offline
Joined: 6th Jun 2006
Location: 0,50,-150
Posted: 31st Oct 2006 22:59
Ahh don't worry, it was just my carelessness when programming. The map editor is now fully functional and we have started making a game with it.
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 31st Oct 2006 23:24
Cool
dark donkey
18
Years of Service
User Offline
Joined: 4th May 2006
Location:
Posted: 11th Nov 2006 16:43
great tut! i dont compleatly understand them but i probly will when i go over it a few more times.

thanks!!!!!!!

http://www.darkdonkeygraphics.tk

indi
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: Earth, Brisbane, Australia
Posted: 12th Nov 2006 14:37
I think this can be stickied as well, its on par with the functions lessons.
the two biggest hurdles it seems for newcomers is functions and arrays.
I think you have added a great service of information sixty for newcomers.

Login to post a reply

Server time is: 2024-11-25 11:38:13
Your offset time is: 2024-11-25 11:38:13