I still don't get it...

BlitzPlus Forums/BlitzPlus Programming/I still don't get it...

Kyler(Posted 2008) [#1]
Ok, I've been programming for nearly 3 years. And I can't move forward, until I learn this very basic concept. I've read the book like 4 times, and looked at sample code after sample code, and I STILL don't understand the Dim function. If anyone can give a VERY DETAILED description, and example on what it's used for, and how to use it, I will be eternally grateful. Thanks.

Kyler


GfK(Posted 2008) [#2]
um.... it defines the dimensions of an array.

eg:
Dim firstname$(10)
Dim lastname$(10)

firstname$(0) = "Banana"
lastname$(0) = "Man"
firstname$(1) = "Super"
lastname$(1) = "Dave"

For n = 0 to 10
  Print "Name " + n + " is " + firstname$(n) + " " + lastname$(n)
Next


If you've read a book four times that still hasn't managed to explain arrays properly, I'd set fire to it, tbh.


ziggy(Posted 2008) [#3]
Wich langanguage are you talking about? It deppends on he flavour of Basic.
On vb.net DIM is used to declare a variable or an array.
If used on the declarations area of a class, you're declaring a private field. If used on the declarations area of a class and specify an array size, you're creating a private array inside the class. If used inside the body of a function or sub, then you're creating a local variable, but if used inside the body of a sub or function with an array dimension, then you're creating a local array. Then if you're using it inside a module, you're creating a private shared variable, unless you set again an array size, then you're creatina a private shared array.

My advice would be that DIM lets you declare anything anywhere but the access to this 'thing' is limited to the scope of where the dim sentence is placed.

Example:
Function MyFunction(Value as Integer) as string
    Dim A as String
    A = "This is a value: " & Value
    Return A
end function


In this example, the variable A is created on every function call and destroyed when the program exits the function.

This is different:
Class MyExample
   Dim Counter as integer = 0
   Sub Add()
      Counter = Counter + 1
   End Sub
   Sub Rest()
      Counter = Counter - 1
   End Sub
   Function GetValue() As Integer
      Return Counter
   End Function
End Class

This example shows the usage of DIM in a class declarations area. All the functions and sub can access this variable and modify it, but this variable is still Private, so from outside the class, you can't see its value (that's what the functions GetValue() is for in this example). In this example you could replace the Dim statement by a Private statement, and it would do the same.

The same would go for arrays. If you're working with arrays you may see also the redim statement.


boomboom(Posted 2008) [#4]
well since hes posted it in the blitzplus forum I would say he wanted information about dim in blitzplus. So your probably confusing him more giving him VB examples.


Kyler(Posted 2008) [#5]
Hey Gfk, thanks for at least trying to help, but quite what I was looking for. All the is is a sample code. I could get that anywhere. I want to know what it means. And how I can use it in games. Thnx


Snarkbait(Posted 2008) [#6]
Dim is used to create an array. An array is like a collection of variables, or a list. It is best used when you have a known number of items in the list.

For example, say you have an RPG and you have a string variable containing the names of character classes. You know you have only 5 possible classes.

I am sure you know how to declare a standard string variable... the only difference is that an array needs to be 'dimensioned' (which essentially sets aside the memory for the entire array).

You would use the statement

Dim characterClass$(5)


This is essentially creating 5 (Well, actually 6 but I'll explain that later) different string variables, in a list, like such:

characterClass$(1)
characterClass$(2)
characterClass$(3)
characterClass$(4)
characterClass$(5)

you can then assign unique values to each of these members of the array.

characterClass$(1) = "Fighter"
characterClass$(2) = "Cleric"
characterClass$(3) = "Mage"
characterClass$(4) = "Paladin"
characterClass$(5) = "Thief"



Now, for your character in the RPG, you don't need to know the actual text description of the class, you only need to know the NUMBER. Say your character is a paladin, you just need to use a 4 to get the proper element of the array.

myCharacterClass = 4

Print "My character is a " + characterClass$(myCharacterClass)


Will print "My character is a Paladin." Note you can use another (integer only) variable as the subscript or index of the array. This is where arrays really become powerful.

Some notes:

Arrays should be DIMed fairly early in your code, as they cannot be dimensioned within a function, and they are ALWAYS GLOBAL.

You can fall into a bad habit with Blitz, like I did up above. In almost every other programming language, with a few exceptions (VB6 being one of them) Arrays are zero-based. This means for a 5 element array, you should actually Dim it like array(4), and then start the list at array(0). With Blitz, however, you can do it like I did it above, and just not use the (0) index position.

Do not let your program reference the array beyond the limit, it will cause your program to crash. (For instance, in the example above, if you tried to reference characterClass$(6) )

Arrays can be multi-dimensional... using 2, 3 or any number of dimensions. This is great for using say, a puzzle grid, a lookup table, high score table, 2d tilemap data and so on. I can describe this in more detail if you would like.

To sum up, an Array:

1. is a numbered LIST of one type of variable
2. needs to be DIMensioned to an exact size
3. should be dimensioned near the beginning of your code
4. is always GLOBAL in scope
5. each member can be referenced by its corresponding index or subscript
6. you can use another integer variable in place of the subscript
7. can have multiple dimensions
8. the index of the array cannot be lower or higher than the dimensioned amount

Limitations:

If you do not know the exact number of members of the list, you need to use another method, such as Types

There is another kind of array called a Blitz Array. The docs are murky on this, so I won't bother explaining them. The main difference with them is they are zero-based, can only be one dimension, are local in scope and can be passed to a function.

Arrays in Blitz cannot be passed to a function, however, since they are global they can be used from inside a function, but they must be dimensioned outside of a function. This is actually considered bad form from a structured programming standpoint, but hey, we're just making games here, right?


Kyler(Posted 2008) [#7]
Thankyou Snarkbait. You said some thing about tile maps. Grids. Whatever. I'm very interested in how to do that. Right now, the only way i know how to do that is to make the game an editor. So the player has to manualy build the level before he's able to play it. That's not too much fun...can these arrays help me to make a tilemap? If so, I would love to know more about it. A sample code to go along with it would bee nice too.


Sauer(Posted 2008) [#8]
When you want to make a tilemap, you basically use an array to hold the numbers corresponding to the frame of the image you want to use for a specific area of a map.

So when you start a tilemap, I always like to make a little commented list of what each number stands for.

; grass=0
; sand=1
; forest=2
;mountains=3


Ok so now we want to use a data block to store what our level looks like, using the numbers above. At the end of your code, you're going to want something like this:

.level1 ;denotes that this is the data block for level 1
data 0,1,2,3,3,2,2,1,0
data 1,2,3,2,0,1,0,2,1
data 2,3,1,0,0,0,0,1,1


Ok now that we have a data block, we need to write a sequence that reads the data block, stores it in a array, then displays it to a screen.

So when you want to reset the level, you always start with the RESTORE command, which directs the computer to the data block you want. Then, you're going to READ each element in your data block, and store it into an array. Then, you want to cycle through your array and display each tile to the screen.

Ok so here we go. First lets declare the array near the top of your code where you have the rest of your globals:

Dim leveltiles(3,9)  ;we're creating a 9 x 3 table


Ok, now, in your code that resets the levels, add something like this:
RESTORE level1  ;direct the computer to level 1's data block
FOR y=1 TO 3  ;scrolling through the data block...
     FOR x= 1 TO 9 ;scrolling through the data block....
                READ num  ;read the element of the data block
                leveltiles(y,x)=num  ;whatever that element was is now an                           element of your level table
      NEXT
NEXT


Now you're almost there. When you draw your level to the screen, add some code that scrolls through your level table (leveltiles) and draw the tiles to the screen.
FOR y=1 to 3
   FOR x=1 TO 9
       drawimage tileimage,x*32,y*32,leveltiles(y,x)    ;this is assuming you have 32 x 32 tiles, and it draws your animimage with the frame you designated way back in your data block
    NEXT
NEXT

And thats about it. This code is merely for reference, but its almost exactly what you want to do. I did it quickly, but I believe it is correctly done.

It takes a bit of practice to understand whats going on, but once you get it, you GET it.

Hope it helps, good luck.


Kyler(Posted 2008) [#9]
Thank you, it helped ALOT! But the blocks are not types. So is it possible to move them? Or make collisions on them? If so, how? Thanx


Pineapple(Posted 2008) [#10]
Here is what I think the best and most easily comprehended explanation anyone has ever given.

Basically, Dim MyVar(4) would be a substitute for writing MyVar0=0:MyVar1=0:MyVar2=0:MyVar3=0:MyVar4=0 .

Using an array (which is what Dim creates) only saves you the huge hassle of making a long list of integers each serving a similar purpose and makes going through them much easier too.

My most common use for Dim arrays is tilemaps. Arrays can also be multidimensional, which means instead of having one number (Dim Var(X)) you can have two or even more numbers in the parentheses (Dim Var(X,Y)).

To use an array as a field for a type, simply use brakets [] rather than parentheses () when referring to it. For example, Type Obj:Field Array[7]:End Type

Another good thing to know, is that when you define an array with 5 in the parentheses, there are actually 6 numbers; Var(0) is also a valid variable.

When you try to access an array number that's out of the created range, you get an irritating error. Like, if you had Dim Array(6) and tried to reference to a variable Array(7) you'd end up getting an error.

To cycle through all your variables and do something to each one of them, use a for loop. For x=0 to 5:Array(x)=Array(x)+1:Next

I hope that helped, that's all the basics for Dim arrays.


Sauer(Posted 2008) [#11]
Well think about it, say you wanted to slow the character down when it hits sand. What you would normally do is use IMAGESCOLLIDE to see if your character image is colliding with the sand image.

There is no real difference here, because in the end, you're just drawing the images to the screen just as you would any other image. So, just do:
IF IMAGESCOLLIDE(playerimage,playerx,playery,frame,tileimage,tilex,tiley,1)