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:
PlayerName$ = "Sven B"
PlayerLife = 100
PlayerObj = 1
Now let's say we want to make a multiplayer game for 4 people! Well I guess we could expand our code to
Player1Name$ = "Sven B"
Player1Life = 100
Player1Obj = 1
Player2Name$ = "Regnuar"
Player2Life = 100
Player2Obj = 2
Player3Name$ = "Grog"
...
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.
dim PlayerName$(4)
dim PlayerLife(4)
dim PlayerObj(4)
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:
for i = 1 to 4
input "Enter a new name:", NewName$
PlayerName$(i) = NewName$
PlayerLife(i) = 100
PlayerObj(i) = i
next i
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:
`First we build a UDT: tPlayer
type tPlayer
Name$ as string : `Will contain a player's name
Life as integer : `Will have the player's life
Obj as integer : `Will be the number of the object associated with the player
endtype
`Declare a variable as a tPlayer
Player1 as tPlayer
`Now we can access the information by using a dot
Player1.Name$ = "Sven B"
Player1.Life = 100
Player1.Obj = 1
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:
`First we build a UDT: tPlayer
type tPlayer
Name$ as string : `Will contain a player's name
Life as integer : `Will have the player's life
Obj as integer : `Will be the number of the object associated with the player
endtype
`We make an array using dim of 100 "tPlayer"s
dim Players(100) as tPlayer
`Now we can give each player a name, life and object number
for i = 1 to 100
input "Give player " + str$(i) + " a name: ", NewName$
Players(i).Name$ = NewName$
Players(i).Life = 100
Players(i).Obj = i
next i
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