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.

Author
Message
Regnuar
15
Years of Service
User Offline
Joined: 25th Dec 2008
Location:
Posted: 18th Sep 2009 23:04
Hello!

So, I just don't get the arrays, i'v ridden some tutorials and tried to understand them, no success.

could someone make me simply an example where is 3 enemies with their health and names. and when "player" collision them, then they disappear or something. just a simple example with nice descriptions.

Regards, Jukka Korhonen
BowserYo
16
Years of Service
User Offline
Joined: 4th Jan 2008
Location:
Posted: 18th Sep 2009 23:51
Have you looked at the basic AI tutorial? That's how I learned my basic knowledge of arrays. It's in the tutorial sticky.

ßõw§€r¥¤
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 19th Sep 2009 20:14
Check out TDK's tutorials... these are in the Darkbasic Classic section but most of his tutorials work in Pro too.

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 20th Sep 2009 00:53 Edited at: 20th Sep 2009 00:53
Quote: "could someone make me simply an example where is 3 enemies with their health and names. and when "player" collision them, then they disappear or something. just a simple example with nice descriptions."


I think it's better to see an example without distractions like collision or making an object disappear though...

Sven B's fast introduction to arrays and UDT's
Let's say we want to have 1 player with the following information: a name, a life and an object number. The simplest way to implement this would be:



Now let's say we want to make a multiplayer game for 4 people! Well I guess we could expand our code to



You can see what happens if we want to expand this even further to let's say 100 players. You would need at least 400 lines, just to give the variables a value! Not to mention how many lines you need if you want to handle collision, player movement, etc.

That's when an array comes into play. It allows us to make a list of variables of the same type, and number them. You give the array a name, and use a number to say which item from the array you want to use. To use an array, we first have to make it. This is done by using the keyword dim.



What's the benefit? Well, you can use the fact that you access an item by a number instead of by name to your advantage. An example would be using a for-next loop:



Even if we would want to make 100 players, the code would never be longer than the 6 lines we just used. This is the power of arrays.

On a side note: we can improve the readability of our code a bit further by using UDT's (User Defined Types). A UDT basically groups information under one type. You can declare another variable by this type using the name you gave your UDT. You can access the information of the type by using a dot ( . ). An example will make things more clear:



This allows us to make 1 list of "tPlayer"s so it is easier to read. Combining everything until now makes a very short and readable code:



And that's it.
More things can be done with arrays, like adding an element or deleting one, but the above is probably the core of one should know when using arrays.

Cheers!
Sven B

BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Sep 2009 12:14
How to use arrays to manage your assets. The example uses 2 tribes of 10 aliens, and shows how to decrease their health as they battle

http://www.thegamecreators.com/data/newsletter/newsletter_issue_28.html#9

Login to post a reply

Server time is: 2024-11-24 01:32:27
Your offset time is: 2024-11-24 01:32:27