GLBasic forum

Main forum => Off Topic => Topic started by: chve on 2011-Nov-07

Title: Types and array question
Post by: chve on 2011-Nov-07
I have seen that in GLBasic is easy to understand how types works.

I always usea arrays instead types, but I would like to know when do you see it is better use types than arrays, becouse I dont see any diference.

Cant someone post an explanation and/or an example which shows when it is better use types than arrays?

Thanks   :)
Title: Re: Types and array question
Post by: Falstaff on 2011-Nov-07
To me, types and arrays serve different purposes. Keep in mind, you can actually have arrays of types, so you can have the best of both worlds ;)

Types are good when you have several pieces of information which, when used together, describe something in your program (ie an 'object').  So if you have strings, floats, ints, that all describe something (say, a Player), then you can group those different pieces together under one "Type". Then you can make different copies of that Type, and deal with it much easier. Speaking of copies..

Arrays are good when you have many copies of information which has the same type. The advantage of arrays is you can use things like FOR loops to quickly run through each copy of information.

As for some examples.. Say you have a game where you have a bunch of monsters, instead of doing something silly like doing

monster1x = monster1x + monsterspeed
monster2x = monster2x + monsterspeed
monster3x = monster3x + monsterspeed
monster4x = monster4x + monsterspeed
and so on...

Notice how of course every time we add a monster, we need a new variable, and we have to work with that variable everywhere else we had our other monster code. Why not use arrays! Now look how easy it is to have more monsters :)

For n = 0 to numMonsters
monsterx[n] = monsterx[n] + monsterspeed
Next

or of course, combining them is where you get the best of both worlds. This is similar to what I do in my game.

Type MonsterInfo
x
y
xvel
yvel
EndType

Global Monsters[] As MonsterInfo
Dim Monsters[MAXMONSTERS]

For n = 0 to numMonsters - 1
Monsters[n].x = Monsters[n].x + Monsters[n].xvel
Next

These are of course really primitive examples that wouldn't actually do anything, but yeah I hope they show off the power of working with both types and arrays :)

(p.s. why is this in the Off Topic section? This seems totally relevant to GLB :))
Title: Re: Types and array question
Post by: fuzzy70 on 2011-Nov-07
To me it all depends on what I am trying to achieve. I tend to follow a rule which is if the data is a set size then use arrays, otherwise use types. I am not saying that's the best way to do it but it is one that works very well for me, giving me consistency & an easy way to visualise them.

For example if I was creating a platform game I would store the map level tiles in an array as I would know the size in advance like

DIM map[30][30]

Now say I wanted to add a nice particle effect like a fountain or explosion, for that I would use types as the number of particles varies from a few to a lot (and back down to zero in some cases) like

TYPE particle
   x
   y
   xdir
   ydir
ENDTYPE

That way I can add new ones as they are needed & delete them when they are done with & easily update them.


Both the above examples can be done either way & like Falstaff said you can have an array of types giving you more options.

In the end though a lot of the time it comes down to personal preference & what you feel comfortable with & works for you. There is no hard fast rule really saying you have to do it one way or the other, just play around with both & see what you feel is better.

Have a look at the code in the samples folder & see where types and/or arrays have been used & try to work out why they was done like that. That's how I have learnt over the years & sometimes you learn other things while looking at the code as well  :)

Lee
Title: Re: Types and array question
Post by: chve on 2011-Nov-07
@Falstaff 
Your explanations from you are great and very usefull for me, believeme.  :good:

Than you very much!:)

Now it is more clear when I should use types or arrays.


@fuzzy70
Quote from: fuzzy70 on 2011-Nov-07
Have a look at the code in the samples folder & see where types and/or arrays have been used & try to work out why they was done like that. That's how I have learnt over the years & sometimes you learn other things while looking at the code as well  :)


I had seen  examples  of of tutorial of GLBasic , but it was not enough for understanting when I should use types.
Your example is more clear for me in this case. :enc:

Thank you very much! :)

I understand  that for objets or NPC with characteristic, etc = tpyes
For some other datas = arrays

Title: Re: Types and array question
Post by: kanonet on 2011-Nov-07
Quote from: chve on 2011-Nov-07I understand  that for objets or NPC with characteristic, etc = tpyes
For some other datas = arrays
I would say it so strict. It depends on which information you want to store and how much. And of cause most times its just a matter of personal preferences.

To use the use the monster example:
If you only need the information about the position, you dont need a type, arrays ya do this too:
DIM monsterx[4]
DIM monstery[4]
Or do it one in one array, but keep in mind, that multidimension-arrays are slower:
DIM monster[4][2]

But if you add more information, thinks get looking strange:
DIM monsterx[4]
DIM monstery[4]
DIM monsterlifes[4]
DIM monsterfirepower[4]
DIM monsterarmor[4]
Again you can  do this in one array:
DIM monster[4][5]
but you have to remember, that monster
Title: Re: Types and array question
Post by: chve on 2011-Nov-07

@kanonet

Yes, I usually do something like:

DIM monsterx[4]
DIM monstery[4]
-----etc
Many array  Dims  or even a 2 or 3 dimesion array too.

But sometimes, my code is very confussed.

And for this reason I was experimenting with types


I apreciate all expanations  :)
Title: Re: Types and array question
Post by: Hatonastick on 2011-Nov-08
And of course the best thing about types is the ability to put functions into them giving you semi-OOP capabilities in a procedural language.  Personally, I'm not much for OOP however I have found putting functions inside types useful on a few occasions such as a self-contained starfield generator or a particle generating system.
Title: Re: Types and array question
Post by: chve on 2011-Nov-08
@Hatonastick

I will try that, tanks  :)
Title: Re: Types and array question
Post by: mentalthink on 2011-Nov-24
HI chve I suggest you use types, it´s very easy, in in beggin can be complex but it´s very very easy.

You can make somthing more complex like a type of functions, and it´s very powerfull, or you can have a variables whit types, or types into another types... they are fantastic for working together whit arrays...
Title: Re: Types and array question
Post by: chve on 2011-Nov-24
Yes mentalthink

I have tryed with GLBasic and it seems to be more easer than other languages  :)

I have less practic with types than arrays, but I am experimenting, be sure

Thanks!  :)