I flatout don't understand types

Blitz3D Forums/Blitz3D Beginners Area/I flatout don't understand types

Cubed Inc.(Posted 2010) [#1]
For as long as I have been working with blitz3d, I have never been able to grasp the concept of types.

For about 4 months, I have been trying to add jumping into my 3d game, and I have yet to succeed.

I then decided to go with the concept of jumping that was in the castle example that came with blitz3d.
It seems that the jumping is created using types.
The problem is that I just absolutley don't understand types.

Can someone please explain how to use types correctly to my advantage?

I desprately need to add jumping in my game, and I'm running out of attempts on how to do it.

I really don't want to abandon my game becuase of this situation, becuase I believe that my game has potential.

So can someone please...please explain this and help me out please.
Thanks for your patience.

please reply.


Yasha(Posted 2010) [#2]
A type is a way of interpreting data, giving it structure so you know what to do with it. Blitz3D has three built-in types: integers, single-precision floating-point numbers, and dynamic strings. The type command lets you define new kinds of structured, type-checked data, for use in other situations.

For example, if you were writing a program that needed to manipulate simple vector images, an important concept is the 2D point - the X and Y values that define a location on the plane. One way to handle this is to simply pass the two pieces of data around next to each other, which would work - but you'd need to do a lot of rewriting if you ever decided to give the program a third dimension.

Since you're thinking about "points" as a single concept, we should pass them around as a single piece of data. So we can define a point as a complex type, in terms of more basic types that represent its components:

Type Point
    Field x#
    Field y#
End Type


You can now use the Point as a single concept when writing code:

Function SetSomethingsPosition(p.Point)
    Local somevar# = p\x        ;This is how you get at a component value
    ...
End Function


The fields of a type act like variables anywhere else: they can be int/float/string, references to other custom type objects, and even arrays (the square-bracket kind, not the Dim kind). You manipulate a field using the backslash, as shown above; and if the field is further subdivided (an array, or a reference to another object) you can use the array element access syntax, or more backslashes, until you reach the subfield or element you need. e.g. car/door/doorHandle/bolt[23]/head

Conceptually, it's that simple. The real opportunity for confusion comes next: all user-defined types like this are reference types, whereas all three built-in primitives are value types. When you pass around an int/float/string, the value is copied from one variable into another, and modifying the second one does nothing to the original; but reference types are like entities or images: they need to be manually created (in the case of type objects, with New), and if you simply copy the variable to another variable, you have two names that point to the same object - the object being somewhere else in conceptual space.

Similarly, like entities and images, custom type objects need to be manually freed after use, otherwise you waste memory. Instead of FreeEntity and FreeImage, you need to use Delete when you're done.

Custom types do have several advantages over unstructured data in banks (which, from a logical point of view, is the same sort of thing: the variable "points" to an object created "somewhere else" in the computer's memory), or entities, images and other Blitz objects, because they're type-checked, and the variable is automatically set to Null when the object is deleted (so you can't assign it to a variable intended for another kind of object, as you can with entities and images that are stored in integers, and you can quickly and safely test whether it still exists by checking against Null).

I found the documentation on this topic to be quite confusing too when I first looked at it. One problem is terminology: a lot of people say "type" and mean a specific instance or object, instead of the structure of the object. There's also the entirely separate issue of For/Each, Before/After, First/Last - you don't need to know anything about these to understand types and they don't have much to do with the subject in a theoretical way (when objects are created, they're added to a global list of all objects of that type - you don't need to make use of this though and it's best to leave it until you understand the basics).


Rob the Great(Posted 2010) [#3]
Types had me confused for the longest time. I couldn't figure out how they worked. After playing around with the examples for a long time, I've finally gotten the concept of types figured out. Now, I tend to over-abuse the type system in order to simplify the programming process.

Here's a hypothetical situation:

I'm the senior programmer for a game in development, and I have asked you to write me some code for a monster in this game. I have told you that the monster will be sort of a "template" for other monsters of the same family, and therefore I will need you to write me some code that will allow me to define where the monster starts in the level, how aggressive that monster is, how the monster will react under certain circumstances, how much life the monster has left, and how much damage the monster can inflict on our hero, all quickly and easily. And most importantly, I want to be able to store ALL of these values so that I can read them back later.

Without the custom types system, this can be a very tedious process. You would have to define a separate variable for EACH monster, and if there were thousands of these monsters wondering throughout your game world, you could very easily have to type out thousands upon thousands of individual variables for your program. Sounds like a headache, right?

This is where the beauty of types comes into play. Take a look at this code:
Type monster
   Field entity  ;Use this to hold a name for our monster
   Field startx  ;The Starting 'X' Position for that monster
   Field starty  ;The Starting 'Y' Position for that monster
   Field startz  ;The Starting 'Z' Position for that monster
   Field aggression  ;The Aggressiveness for that monster
   Field stateofbeing  ;The State of Being for that monster
   Field lifeleft  ;The amount of Life Left for that monster
   Field damage  ;The amount of damage that monster can inflict
End Type

You have just told Blitz that you're going to make a special "group" called the "monster" group. Every "monster" in this group is going to have it's own variable, stated by the "Field" command. Now, whenever you create a new monster (I will show you how below), they will each have their own data sets of "startx", "starty", "startz"...

Now, let's create some monsters for our game:
For number = 1 to 100  ;Do this part 100 times and then move on
   m.monster = New monster  ;Create a new 'monster' and call it 'm'
   m\entity = CreateSphere()  ;Create a ball to 'entity' to represent our monster
   m\startx = 5  ;The variable 'startx' for the new monster is now '5'
   m\starty = 0  ;The variable 'starty' for the new monster is now '0'
   m\startz = 10  ;The variable 'startz' for the new monster is now '10'
   m\aggresion = Rand(1,10)  ;The variable 'aggresion' is now randomly 1-10
   m\stateofbeing = idle  ;The variable 'stateofbeing' is now set to 'idle'
   m\lifeleft = 10  ;The variable 'lifeleft' is now set 10
   m\damage = m\aggresion  ;The variable 'damage' is now whatever 'aggresion' was
Next

The code above would make 100 monster spheres, and give them each their own unique name, starting point, aggressiveness, state of being, life, and damage they can inflict. You can define the variables yourself, or you can have Blitz randomly select them for you.

This is a great start, but now I, the senior programmer, have asked you to make a way in which I can cycle through hundreds, maybe even thousands, of monsters at one time and see how they will respond.

Here's how you could do this:
While Not KeyDown(1)  ;The Main Loop

   For m.monster = Each monster  ;For every single monster we've created
     If m\aggression > 5  ;If they're aggressive
         If EntityDistance(m\entity,ourhero) < 20  ;And if they're within 20 units of our hero
            m\stateofbeing = attack  ;Attack the hero!
         Endif
         If EntityDistance(m\entity,ourhero) >= 20  ;But if they're not close to our hero
            m\stateofbeing = idle  ;relax and drink some coffee for a while...
         EndIf
      EndIf
      If m\aggression <= 4  ;If the monster is 'calmer'
         If EntityDistance(m\entity,ourhero) < 10  ;And if they get really close to our hero
            m\stateofbeing = attack  ;Attack the hero!
         EndIf
         If EntityDistance(m\entity,ourhero) >= 10  ;But if they're not close to our hero
            m\stateofbeing = idle  ;relax and drink some coffee for a while...
         EndIf
      EndIf
      If WeHitTheMonster = True  ;If we hit the monster with our fist
         m\lifeleft = m\lifeleft - 1  ;Take some of the monster's life away
      EndIf
      If TheMonsterHitOurHero = True  ;If the monster hits our hero
         HerosLifeLeft = HerosLifeLeft - m\damage  ;take some of the hero's life away
      EndIf
      If m\lifeleft <=0  ;If the monster has no life left
         m\stateofbeing = die  ;It will die
      Endif
      If m\stateofbeing = die  ;If the monster has 'died'
         RespawnMonster(m\startx,m\starty,m\startx)  ;It's resurrected at the starting point
      Endif
   Next

Wend  ;End the main loop

This is a pretty specific example, but the above code would cycle through each monster created and perform actions based on the condition of each monster. You can see how this is much more efficient than checking each one individually, even with the help of functions.

Now, me being the jackass senior programmer that I am, I now approach you and demand that we be able to delete the monsters not needed anymore, to help save space for the rest of the game.

Here we go:
For m.monster = Each monster
   If m\stateofbeing = disappear
      FreeEntity m\entity  ;Don't forget this, otherwise the mesh will still exist!
      Delete m
   Endif
Next

Now that particular monster is gone forever.

Look out! Here comes the senior programmer who you secretly despise but have to suck up to in order to keep your job. He claims that the code you've provided takes too long to delete ALL of the monsters, and he wants you to write some code to do it all in one swipe.

This will do exactly that process:
For m.monster = Each monster
   FreeEntity m\entity
   Delete m
Next

And there you have it. Keep in mind that I threw this together very quickly, and it's very possible I made some mistakes along the way. This was designed for example purposes only, and the Blitz documentation covers more commands that I'm not so familiar with (e.g. "Before", "After", ect.).

In short, think of custom types as a giant container for a group of similar items. Types make it easy to handle many, many, many things at once without the added code. Typical items used in types could be bullets for a gun, arrows for a bow, enemy groups, special effects (fires, particles), basically anything which has a tendency to repeat itself, or anything that is part of a group or a family.


Charrua(Posted 2010) [#4]
hi

types are great stuff but i think that you don't need they for the jumping part of a game. I think you are confused somewhere. Following is a code without types for jumping. The key is y_vel variable that controls the y velocity of the player.

btw the EntityType command nothing has to do with types, it is related with the blitz collision system, but if you know that, please do as if i never write this line!



hope that help

Juan


Cubed Inc.(Posted 2010) [#5]
Rob The Great and Charrua

Thanks for the explaination on types and how to use them.

I understand them better now.

Thanks:)


Ross C(Posted 2010) [#6]
The problem i had with types at first, was the fact you couldn't instantly access a type object. Coming from using only arrays, it was a bit of an adjustment. Took me a good couple of months too. Stick with, do very basic examples and it'll come :)


Rob the Great(Posted 2010) [#7]
Couldn't instantly access a type object...I'm not quite sure what that means. Are you referring to always having to use a For...Next loop with types, and arrays don't require that?

Ironically, I've never learned about array process. I kinda-sorta understand what they are, but I've never found use for them in my games. It's probably not good programming practice to never use an array.


Who was John Galt?(Posted 2010) [#8]
Couldn't instantly access a type object...I'm not quite sure what that means.
I think he means that as opposed to a simple variable where declaring it creates a variable, with types you declare a 'template' and then separately instance it.

Arrays are good for storing tile maps, among other things.


Adam Novagen(Posted 2010) [#9]
The problem i had with types at first, was the fact you couldn't instantly access a type object. Coming from using only arrays, it was a bit of an adjustment. Took me a good couple of months too.

Same here. I finally cracked it when I had the idea to include an "ID" field in the Type. Now I can find any specific one like this:

Function FindType(TypeID)


For MyType.MyType = Each MyType
    If MyType\ID = TypeID
        
        ;Do stuff :D
        
        Return ;Type's been found, show's over.
    EndIf
Next


End Function



_PJ_(Posted 2010) [#10]
I was scared of Types when I first encountered Blitz, avoiding them like the plague I eventually had to take the plunge when looking through some code archive stuff that I really wanted to know how it worked. I still had to post here a few times to fully understand it (or at least, as fully as I do understand it) but eventually, it all fell into place.

Here's a small bit of code that should help showe how types work and interact with each other. I made this back when I was trying to get used to working with Tytpes myself!




stanrol(Posted 2010) [#11]
wy reinvent the weheel?

go read the help docs.


_PJ_(Posted 2010) [#12]
Because the help docs do not nrecessarily explain it all in a readily pertinent fashion.
From what I've seen from your code, stanrol, the Help docs haven't even been considered at lot either.