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 Discussion / very noobish question

Author
Message
RYD
18
Years of Service
User Offline
Joined: 29th Apr 2008
Location: realspace
Posted: 19th Mar 2009 04:47 Edited at: 19th Mar 2009 04:52
Ok guys , am I just wasting my time doing something complicated because i've still not seen the use of arrays ,so can anyone give me an example of code with array use
Quote: "
dim array(1D,2D,3D)
"


ARM YOURSELF WITH KNOLEGE
That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 19th Mar 2009 05:18
dude, just go to the help menu and find some array codes, click them and open the example program.

ull need to learn arrays, they r a big part of many programs/games
RYD
18
Years of Service
User Offline
Joined: 29th Apr 2008
Location: realspace
Posted: 19th Mar 2009 05:30
man am i mad? of course i read that but still didn't i understand the use of dim array(3,2,4) generous is the one who waste his time to tell me the use of an array.(saving data? but a#=1
and z$="array" so what is dim serving(10) all about)

ARM YOURSELF WITH KNOLEGE
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 19th Mar 2009 05:37 Edited at: 19th Mar 2009 05:43
Heres a example of using two 2d arrays to make a grid, a collision map and move a square around it.

i think tdk has a tutorial on the forums about arrays though.





hope that helps but i think you should look up the tdk tutorial


oh and the dbc examples arn't that great i didnt get how to use arrays till i got on the forums but they are needed! hahaha

New Site! Check it out \/
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 19th Mar 2009 06:21
You use multi dimension arrays when you have alot of the same arrays and want to stack them all in one for ease of use.

To simplify to the best of my know-how, the use of multi dimension arrays are the same reason why you would use an array in the first place.

Instead of:
It's better to do this:


Now instead of:
It's better to do:


And so on...

3d Arrays are also important for tile-based games which uses multiple 2d arrays to store data for drawing the map and such. These 2d arrays are called layers and are used to draw different levels of the map such as Layer 1 will draw only the dirt tiles and layer 2 will draw the grass tiles.

So if you have 3 to 5 or more sets of 2d arrays it's easier to stack them all in one array.

So instead of:
It's better to do:
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 19th Mar 2009 07:12
yup

New Site! Check it out \/
Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 19th Mar 2009 07:57
The best example I can think of is a map: you have one dimension for the x axis, and another for the y axis, then you cross reference the two to get the square you want. ie map(x,y)


search for it, im sure i gave a long winded explanation to someone on this board quite recently.

The Universe has been erased by a mod because it was larger
than 240x80 pixels.
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 19th Mar 2009 18:03
Quote: "ull need to learn arrays, they r a big part of many programs/games "


Absolutely right. I could not program a complex game without arrays... Actually, I have never made a game without arrays...

You have to think of arrays like a grid. So if I want a 12 by 10 grid, I would use "dim grid(12,10)" to initialize the array. The numbers 12 and 10 are the maximum, so you cant use grid(13,120). An array acts the same way as a variable, except you don't need to make 120 variables. Anything can be stored in arrays and you can even save them to a file.

Now lets say I had a yellow dot, a red dot and a blue dot and I want to save these dots to a grid. If I wanted to put the yellow dot to the grid 7,2, I would say grid(7,2)=1. The same for the other dots.

I would write a quick example, but I don't have time and there are already enough examples around. SEARCH!

TheComet

Peachy, and the Chaos of the Gems

RYD
18
Years of Service
User Offline
Joined: 29th Apr 2008
Location: realspace
Posted: 19th Mar 2009 21:15
thanks everyone i think i understood that my problem was using variables
Quote: "azerty1#=1
azerty2#=2
azerty3#=3
azerty4#=4
azerty5#=5"

instead of arrays
Quote: "dim azerty(5)
for t=1 TO 5
azerty(t)=t :save array azerty(t)
next t"
well im still a newbie because what youre doing in a minute or two i would have done it in half an hour

ARM YOURSELF WITH KNOLEGE
That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 19th Mar 2009 22:48
no big deal if ur a noob, thats who this site was created for, to help noobs like us learn
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 19th Mar 2009 23:40
Amen!

New Site! Check it out \/
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 20th Mar 2009 08:02
Don't know if you still need the help with arrays, but here is a tutorial that I wrote a while back to help Irojo with a darkNOOBS project.

Quote: "
Ok, I will try my hand at explaining arrays.

What they are:

Arrays are essentially variables that can hold more than a single number. They are referenced through a coordinate like numbers.

A good way to think of arrays are like filing cabinets. First you have the variable name, which is the unit itself. It can be, just like a variable, an integer, a float (containing decimals), or a string (words). The number of drawers is the first number (so dim checkdata(3) would have 3 drawers). From there, you can sub-divide each drawer with folders, for arrays, it would simply be the next number, signifying how many "folders" for each drawer.

So:
dim apples(3,2) would, to keep up the analogy, a filing cabinet with 3 drawers, and 2 folders in them (just to throw this out there, you CAN use and store data to apples(3,0) but that should only be used if it makes the code easier to read (don't go out of your way if you don't have to).

Now, since this is a computer, you can subdivide every drawer infinitely (to a point, but you will probably run out of memory before you hit the extents) as well as every folder. Inside each folder, though, is a piece of paper, which simply has a number or string (depending on the type) printed on it. This is what the value of that folder is.

How they are used:
You reference them just like variables, with code that looks like:
rocks(32,2)=6

In this case, it is saying (once again, to reuse the same example) go to the filing cabinet labeled "rocks", open drawer 32, and change what is in folder 2 so that it is 6 (pull out the old piece of paper and put in a new one with the number 6 on it).

Tricks that make arrays useful:

Here are two examples:
Example one, with no array-


That is ugly and a waste of space and time. Sections of code with many ifs should really be looked at to see if they can be condensed with loops and arrays.

Here is the second example with arrays:


Ok, so now, lets look at how arrays can be used to help us on our current project:

1 array can store the entire map, and that way it can easily be referenced in loops.
map(3,3) could be used for a 3tilex3tile map. Then you can reference it with x and y values. So if there is a rock at 3,1 then the check could read:


Think about how you would do this without arrays. There would have to be 9 different variables and 9 different checks, which could be done with 1 array and 2 for-next loops like:



So, what could have been 18 lines of tedious and messy code (and remember, the more lines you have the more mistakes you can make) can be done in 5.

Hopefully, this has helped you out. I remember I didn't get arrays when I first started either, then one day I read it and it clicked and suddenly they helped me out. I did go overboard at first and make EVERYTHING an array in my programs...don't do that, it doesn't always help.

A few tips and tricks I have learned:
-you can't inc or dec arrays. You either have to do it the old fashioned way (rocks(rock,2)=rocks(rock,2)+1) or set a variable equal to it, inc the variable, and set the array to the variable.

-you CAN re-define an array (using dim 2 times with the same name but different numbers). Data is only lost if it doesn't exist anymore so:

Will work perfectly (it should, anyway). This really should only be done if necessary, and even then try not to, but it is nice to know the rules so that you can bend them later .

I will let you in on more as I think of them. Let me know if this helps, I "stole" the filing cabinet example from a book I read a while back, it helped me understand arrays so I figured maybe it would help you too."


Hope this helps

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Robert The Robot
19
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 20th Mar 2009 12:20
Wow, that was fascinating - I didn't know that redefining arrays was allowed and that the data would be preserved.

There are two other tricks that you didn't mention, though - firstly, there's a "zeroth" place in the array:


Also, when you have a multidimensional array like Test(5, 5, 5), you can write data to (and read data from) Test(n) or Test(n, m), where n and m are in this case less than or equal to 5. It's an unusual feature, that one, but it's been quite useful to me on a number of occasions.

"I wish I was a spaceman, the fastest guy alive. I'd fly you round the universe, in Fireball XL5..."
Caleb1994
17
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 20th Mar 2009 16:46
wow same here i didn't know you could redim arrays like that! and he actually did mention the "zeroth" place. breafly but he did mention it.

New Site! Check it out \/
That1Smart Guy
17
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 20th Mar 2009 18:44
not to sound gay but wat would we do without bn2?
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 22nd Mar 2009 07:27 Edited at: 22nd Mar 2009 07:29
Probably wait for latch to post, who makes me feel like a noob quite a bit. Thats good though, as you always learn something when that happens.

[EDIT]
Sorry for the bad info guys. Even though I distinctly remember testing out the re-dim of arrays and having it preserve data, for some reason I haven't been able to recreate it lately, will let you know if I can figure out how I did it the first time.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Robert The Robot
19
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 22nd Mar 2009 14:08
Quote: "and he actually did mention the "zeroth" place"

Oh, sorry! I missed that...

Quote: "Even though I distinctly remember testing out the re-dim of arrays and having it preserve data, for some reason I haven't been able to recreate it lately, will let you know if I can figure out how I did it the first time."

Actually, I've a feeling I've managed it before. I'll have to check, but I think I had to ensure that the new number was less than the original:


I'll check this later, and edit this post if I get it working.

"I wish I was a spaceman, the fastest guy alive. I'd fly you round the universe, in Fireball XL5..."
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 23rd Mar 2009 08:28
Quote: "Actually, I've a feeling I've managed it before. I'll have to check, but I think I had to ensure that the new number was less than the original:"


Good, I was afraid I had slipped off the deep end. Glad to see I wasn't the only one . Let me know how it goes, still haven't had any luck on this end.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose

Login to post a reply

Server time is: 2026-07-21 21:24:10
Your offset time is: 2026-07-21 21:24:10