Types and array question

Previous topic - Next topic

chve

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   :)

Falstaff

#1
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 :))

fuzzy70

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
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

chve

@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


kanonet

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
  • [2] is lifes, monster
  • [3] is firepower... it works, but it starts looking wired. But it only works, if your informations all use the same type of variables (e.g. integers), if you want to add other informations (e.g. strings), you cant store them in the same array (ok, you can store integer and floats in strings, but this would really slow down your program, so dont do it)!
    so you can do it:
    DIM monsterx[4]
    DIM monstery[4]
    ...
    DIM monstername$[4]
    Or in 2 arrays:
    DIM monster[4][5]
    DIM monstername$[4]
    Until now you could do everything without types, of cause it dont keep your code clean, and noone would do so...
    To keep your code clean and store all information together, that belongs together you really should use a type:
    TYPE TMonster
    x%
    y%
    lifes%
    firepower
    armor
    Name$
    ENDTYPE

    Local monster AS TMonster[]
    DIM monster[4]

    But there is one case where you definitely need to use types, instead of using arrays: arrays in (type-)arrays:
    e.g. every monster can now have several targets to hunt for:
    Type TMonster
    ...
    target%[] // this can be an index of moster[], so a monster can hunt an other monster... be carefull with indexes, when you delete entries from the array
    ENDTYPE
    If you would try to do this in one array you need multidimensional array with at least DIM monster[4][6][4] what would create a very huge memory overheat... and of couse it would be very hard to handle.

    And of cause you can use types in types:
    TYPE TMonster
    ...
    target[] AS TTarget
    TYPE
    but thats not what you asked for...


    To summarize everything:
    It dont depend on what you want to create, it depends on what information you need to do so.
    If you
    -need more than 1-3 information or
    -need information from different types (integer, float, strings, types) or
    -need variable sized arrays or
    -want to keep all informations together
    than use types.
    And of cause it depends on your preferences and experience... And dont forget, you can add functions to types, which is a nice feature and can help to encapsulate a hole library in one type...
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

chve


@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  :)

Hatonastick

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.
Mat. 5: 14 - 16

Android: Toshiba Thrive Tablet (3.2), Samsung Galaxy Tab 2 (4.1.2).
Netbook: Samsung N150+ Netbook (Win 7 32-bit + Ubuntu 11.10).
Desktop: Intel i5 Desktop with NVIDIA GeForce GTX 460 (Win 8.1 64-bit).

chve

@Hatonastick

I will try that, tanks  :)

mentalthink

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...

chve

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!  :)