am i using the type's right?

Blitz3D Forums/Blitz3D Beginners Area/am i using the type's right?

melonhead(Posted 2011) [#1]
hi everybody i just want to know if im doing this right . im using "Types " and i want to know if im doing this right. im putting down the basic information of the weapons in the game also using types for the monsters and items as well. is there any i should know for "Types"

here the code for the types im using the "Include" command
so i can keep it organized.

Type level_1_sword
Field x
Field y
Field z
Field attact = 1
End Type

Type level_2_sword
Field x
Field y
Field z
Field attact = 3
End Type


Type level_4_sword
Field x
Field y
Field z
Field attact = 5
End Type


Type level_1_shld
Field x
Field y
Field z
Field block = 50
End Type

Type level_2_shld
Field x
Field y
Field z
Field block = 100
End Type


Yasha(Posted 2011) [#2]
The first thing you need to know is that the Field initialisers ("Field block = 100") don't do anything. They compile OK, but they're a vestigial feature that never made it into Blitz3D and simply sit there doing nothing; no value is actually set by that line. It's arguably a compiler error that it let you write that.

Secondly, it looks like you're confusing types and instances: a type is just a "layout" for data, the same as the difference between an int and a float (e.g. both use four bytes, but they store the number in different ways). You haven't actually created anything by defining a type because they have no independent existence.

To instantiate a type (thus creating an object, which is an instance-of-that-type), you need to use New:
Local L1_Sword.Sword = New Sword
L1_Sword\attact = 1

You can then set that object's member-variables (fields) by qualifying the field name with the name of the containing object, as above. You can use New as often as you want, and delete each object when done.

(I assume that this is what you want: to have "Sword" as a category of thing and to have four different swords with different stats; so you only need to define the one sword type, and then to instantiate it four times, then put different values into the objects.)

The other thing to bear in mind is that objects are passed by reference, same as entities and banks; the object exists "off to the side" from the execution of the program and is merely referenced by the variable, unlike ints and floats where the whole of the value exists where it is, and assigning it to another variable copies it (consider: assigning an entity's integer handle to a second variable doesn't create a new entity, just means two integers are pointing at the same thing, and you later need to explicitly free the entity; this is exactly the same as using Delete on an object).

Don't worry, it's normal to be confused by this... the Blitz3D documentation is really vague and has at least a few actual errors in the way it's explained, which makes it a hard topic for beginners!

Last edited 2011


stayne(Posted 2011) [#3]
Yes, but for some reason I always have better luck at assigning things to fields upon creation. Also if x, y and z are entity locations you might want to use floats. For example:

Type level_1_sword
  Field x#
  Field y#
  Field z#
  Field attact = 1
End Type

this.sword = New sword
this\x# = 0.0
this\y# = 0.0
this\z# = 0.0
this\attact = 1


Last edited 2011


Midimaster(Posted 2011) [#4]
you could also use one type for all arms:

Type Arms
     Field x#
     Field y#
     Field z#
     Field Attact%
     Field Block%
End Type


Level_1_Sword = New Arms
Level_1_Sword\Attact=1

Level_2_Sword = New Arms
Level_2_Sowrd\Attact=3

Level_1_Shld = New Arms
Level_1_Shld\Block=50

....


Last edited 2011


melonhead(Posted 2011) [#5]
wow do i can to keep it together can i use the Include command i want to put all the global type and functions in there own .bb file and load it up on one .bb file


stayne(Posted 2011) [#6]
yes