GLBasic forum

Main forum => Tutorials => Topic started by: mykyl66 on 2009-Dec-04

Title: Where can I learn more about using Types?
Post by: mykyl66 on 2009-Dec-04
I have had a look through the manual and one minute its talking about books then employees and somewhere along the way I got lost. :D

ANY tips would be very helpful.


Mike R
Title: Re: Where can I learn more about using Types?
Post by: Qube on 2009-Dec-04
This is the way I use types

Here's an example which will create 100 missiles at random coordinates (X and Y). Then removes some missiles.

Code: GLBasic [Select]

// our missile type - a simple x, y coordinate

TYPE oneMissile

// next we create two global variables so we can use them throughout our program (in functions for example).
// defineMissile is simply so we can define each missile easily
// allMissiles is an array of each missile we create

GLOBAL defineMissile as oneMissile
GLOBAL allMissiles[] as oneMissile

// this creates 100 missiles at random x,y locations

FOR counter = 1 to 100
   defineMissile.x = rnd(640)
   defineMissile.y = rnd(480)

   DIMPUSH allMissiles[], defineMissile // puts the contents of what we defined into our array

// now we'll remove any missile from our allMissiles array where x > 500 or y > 300
// FOREACH loops through every element in our allMissiles array. We don't need to know how many items there are.

FOREACH singleMissile IN allMissiles[]
   IF singleMissile.x > 500 OR singleMissile.y > 300
      DELETE singleMissile

// now we'll remove some more missiles at random
// NOTE we use FOREACH because it's A) easy and
// B) most importantly you don't have to keep track of how many missiles you're dealing with.

FOREACH singleMissile IN allMissiles[]
   IF rnd(100) > 50
      DELETE singleMissile

Hope some of that makes sense? - If not, let me know which part(s) don't and I'll try to explain further. If you have any thoughts on what you'd like to achieve with types then post them and I'll whip up some code to explain :)
Title: Re: Where can I learn more about using Types?
Post by: Ian Price on 2009-Dec-04
Here's a very quick and hopefully easy piece of code that can demonstrate how to quickly and easily implement Types.

Code: GLBasic [Select]
// --------------------------------- //
// Project: Types test
// Start: Wednesday, December 26, 2007
// IDE Version: 5.110

SETSCREEN 640,480,1

// Sprite 1 - red block
DRAWRECT 0,0,8,8,RGB(255,0,0)
GRABSPRITE 1,0,0,8,8

// Sprite 2 - blue block
DRAWRECT 0,0,8,8,RGB(64,64,255)
GRABSPRITE 2,0,0,8,8

// Sprite 3 - green block - PLAYER sprite
DRAWRECT 0,0,8,8,RGB(0,255,0)
GRABSPRITE 3,0,0,8,8

// Type arrays
GLOBAL re[] AS Tredblock

GLOBAL bl[] AS Tblueblock

GLOBAL gr[] AS Tgreenblock

// Red block Type
TYPE Tredblock

// Blue block Type
TYPE Tblueblock

// Green block Type
TYPE Tgreenblock

// Set up red blocks
LOCAL r AS Tredblock

// Create 20 red blocks
FOR n=0 TO 19

 r.x=RND(640) // X position
 r.y=RND(480) // Y position

 DIMPUSH re[],r// Push the Type information into the array associated with the Type

// Set up blue blocks
LOCAL b AS Tblueblock

// Create 20 blue blocks
FOR n=0 TO 19

 b.x=RND(640) // X position
 b.y=RND(480) // Y position
 b.direction=RND(3) // Movement diection (0=right, 1=down, 2=left, 3=up)

 DIMPUSH bl[],b // Push the Type information into the array associated with the Type

// Set up player sprite (green block)
LOCAL g AS Tgreenblock


DIMPUSH gr[],g


// Main Function


// Draw the blocks




// Draw red blocks
FUNCTION render_redblock:

// Go through every red block type in re[] array

 // Draw red block
 DRAWSPRITE 1,r.x,r.y



// Draw blue blocks
FUNCTION render_blueblock:

// Go through every blue block type in bl[] array
 // Move blue block in specified direction
 IF b.direction=0 THEN INC b.x,1
 IF b.direction=1 THEN INC b.y,1
 IF b.direction=2 THEN DEC b.x,1
 IF b.direction=3 THEN DEC b.y,1

 // Prevent blue block leaving screen
 IF b.x<0 THEN b.direction=0
 IF b.y<0 THEN b.direction=1
 IF b.x>632 THEN b.direction=2
 IF b.y>472 THEN b.direction=3

 // Draw blue block
 DRAWSPRITE 2,b.x,b.y



// Draw green block
FUNCTION render_greenblock:

// Go through every red block type in re[] array

 // Up
 IF KEY(200) THEN DEC g.y

 // Left
 IF KEY(203) THEN DEC g.x
 // Right
 IF KEY(205) THEN INC g.x
 // Down
 IF KEY(208) THEN INC g.y

 // Draw green block
 DRAWSPRITE 3,g.x,g.y



What this does is show three different Types (red blocks, blue blocks and green player block) and moves the blue blocks around the screen independently. There is no interaction between the blocks. Move green block with cursors.

I've separated everything out and commented so you can see what is doing what and when. Obviously this is a very simple example but you can play around with the code and see what each part does.

This is just a start to dealing with types - it does not include animation, collision detection, AI etc. etc.
Title: Re: Where can I learn more about using Types?
Post by: Hemlos on 2009-Dec-04
I dont have much time, but i think i can help you for a minute:

TYPES are basically predifined structures(STRUCT in C++), declarations of groups of variables, which you can fill the values in.

If you want to make a group for a player here is a sample:
Code: Text [Select]
TYPE PlayerData:

Now you can say you have a new player:

Code: GLBasic [Select]
GLOBAL Player1 as PlayerData

And now you can give this player some data:
Code: GLBasic [Select]
Player1.X =10
Player1.Y =10
Player1.Z =10
Player1.Speed =100

And now you can move the player:

Code: GLBasic [Select]
Player1=MovePlayerRight( Player1.Speed, Player1 )

FUNCTION MovePlayerRight AS PlayerData: Val, Update AS PlayerData
RETURN Update.X=Update.X + Val


Now this Player.X is at X=110

To update and pass info byref, the function must be AS type.
And that input must be AS type, and the call must be just the name of the Player1.

To use the data to just pass a value in, you use simply Myfunction(Name.Stuff), the function and function input dont need to be AS type.

Title: Re: Where can I learn more about using Types?
Post by: FutureCow on 2009-Dec-04
I'll try a different tack and see if I can simplify the theory.

Say you want to store some information about an item like a book. (I'll go with a book and use the Bible as the main example to keep things easy as that's what's in the documentation)

You would normally use variables in your code to describe the features of the book - a separate variable for each different piece of information
BookPages = 1050
BookHeight = 20               // inches or cm or whatever
BookCoverColour = Red
BookTitle = The Bible

So now we've got four variables that describe various features we want to know about our book. And we can do something with those features - eg. based on the number of pages we can display that it's a big book or a small book.
Code: GLBasic [Select]
if BookPages > 1000 then stdout "That&#39;s a really big book!"
if BookPages < 1000 then stdout "That&#39;s not a really big book!"

Hopefully you're with me up to this point.

Say for example though that we didn't use nice descriptive variable names. We just have a variable called size, and another called colour. Halfway through your code you'll begin to wonder "did Colour describe the paint colour my program uses on the walls, or did Colour relate to the cover of the books, or did Colour relate to what colour to draw the table in the game"?
The easy fix is to use descriptive variable names, but while a descriptive variable name helps - eg. ThisVariableHoldsTheColourOfMyBooksCover - it's inconvenient to write.
If you use [item name].[component" as the variable name though, it becomes substantially more easy to follow what your code does and keeps variable names short and to the point.
Code: GLBasic [Select]
>> Hard to follow <<
Colour = "Red"            // I can&#39;t remember whether Colour referred to the book cover or the wall colour!

>> Easy To follow <<
In the first one, Colour could refer to the colour of ANYTHING. In the second one though they both store a common element (Colour) it's obvious one refers to the Wall colour, the other to the Book.

Where types come in handy is when you want to store multiple bits of information about one or more objects - eg. multiple books, multiple ghost enemies etc. The important thing is that all the bits of information (number of pages, colour etc) relate to the one object (eg the book).
Yes, you could store a separate variable for each bit of information, eg. the Bibles number of pages, the Dictionary's number of pages etc, without using types. If your program is simple, then types are an additional overhead you don't need. As your code becomes complicated though you'll find grouping your variables with types makes your program substantially easier to manage, and makes it exceedingly more readable.

Think of a type as a box. In that box we put pieces of paper, and written on these bits of paper is some information about whatever the box refers to. When you're moving house for example you write on your box "kitchen pantry" and every item in that box relates to the one thing - the pantry in the new house - regardless of whether it's a jar of pasta or a box of icecream cones.

For our analogy, our type will be a box on which we write the name of the book so we know anything inside that box relates to the book with that name. Inside the box we will put lots of pieces of paper, each bit of paper is a fact about what the box relates to. So for example we have a box and call it "Information about the Dictionary". Inside the box we put a piece of paper that says "Number of pages = 1000", and another that says "CoverColour = Red". We know because the piece of paper with the Number of pages on it is in the box about the dictionary, we know this dictionary has 1000 pages.

In our code, first we have to tell the compiler what our box(es) relate to. We do this by giving it a name like "BookInformation".  We then define what the pieces of paper in the box will tell you about the books (eg. we specify that there will be a piece of paper telling you how many pages that book has, and another what the colour of the cover is).

Here's how we create our container that will store all our little pieces of paper and tell the compiler what will be on each piece of paper
Code: GLBasic [Select]
TYPE BookInformation:

So the computer now knows what all the boxes related to Books will have information in them about the number of pages in the book and the colour of its cover.

Now we need to create our first box (so far we've only described what it will be like).
This is the equivalent of in the real world writing the title of the book on the box's lid, writing the details on the pieces of paper and then storing them in the box.

So firstly create the book
Code: GLBasic [Select]
GLOBAL Bible as BookInformation
This line says I want to create a global type (ie. a box that everyone can see and open up to find out what's in it) and call it Bible (ie. the "GLOBAL Bible" bit). The "as BookInformation" part tells the compiler that the Bible box is to store the types of information (think pieces of paper) that the earlier "TYPE" statement described. In this case it will store NumberOfPages and CoverColour.

We can now query the information on the pieces of paper inside the box. Eg. if we wanted to find out the number of pages of the book (as written on the page inside the box) we would use [Box Name].[Information type] in place of a variable.
Code: GLBasic [Select]
if Bible.NumberOfPages > 100 THEN STDOUT "That&#39;s a really big book!"

We can now add another box and call this one for example "TheDictionary". As it is the same type of box it will contain pieces of paper describing the same things that the ones in the Bible's box do. ie. it will contain pieces of paper describing the number of pages of this particular book (the dictionary) and the colour of its cover.
Code: GLBasic [Select]
GLOBAL TheDictionary as BookInformation

There's one thing missing. The code as above created the boxes and pieces of paper in them, but didn't write on them to say how many pages were in each book or the colour of the covers. We can now do that
Code: GLBasic [Select]
Bible.NumberOfPages = 1050
Bible.CoverColour = Red
TheDictionary.NumberOfPages = 3200
TheDictionary.CoverColour = Blue

You could now do stuff with that information. eg.
Code: GLBasic [Select]
if "TheDictionary.NumberOfPages > 3000 THEN STDOUT "The dictionary has a lot of pages!"

Because we've given the boxes names it's easy to see, when we're looking at the number of pages for a book, exactly which book it refers to.

In short, it allows for a nice logical grouping of information relating to a common item, and makes it easy in code to see what item (eg. the bible or dictionary) the fact (number of pages, cover colour) relates to at a glance without having to look back through your code to work out (for example) which book we're talking about at the moment that has this many pages.

Hope that helps!

When this all stops your head spinning, lesson 2 is an array of types. What this allows you to do in short is for example
1) Define a type (just a collection of related data with a name) which describes an enemy in your game (eg. what their screen location is, how much health they have etc)
2) Create LOTS of enemies of that type quickly
3) By looping over the array that stores the enemies (ie. the array stores enemies, where because each enemy was created from a type they have their own screen location, amount of health etc) you can quickly do something to each and every enemy - like very quickly move all of them 10 pixels to the right.

Array's of types are VERY powerful and a very handy tool to understand.
Title: Re: Where can I learn more about using Types?
Post by: Hemlos on 2009-Dec-04
Also you can look up matrices, or matrix on the web.
TYPE is great for making them.
Makes programming alot easier.
Title: Re: Where can I learn more about using Types?
Post by: mykyl66 on 2009-Dec-04
Thank you very much for the replies folks. Will have a look at each of your examples and hopefully clear up this problem finally for myself.


Mike R
Title: Re: Where can I learn more about using Types?
Post by: mykyl66 on 2009-Dec-05
Just wanted to say that the fantastic help you people have given in this thread has cleared up all the issues in understanding types I had.

Thank you all so much.

Mike R