Using Type

Blitz3D Forums/Blitz3D Programming/Using Type

Lilprog(Posted 2004) [#1]
Im familiar with structures in C but Im having a little trouble with the type function in Blitz. I want to use type to define all of the players in my game, but I need to distinguish between each player. Is there a way to create specific names for each instance of a New Player that I create?


_PJ_(Posted 2004) [#2]
Well, you can assign a Field to store a name, combine this with a counting variable to increment each time New is used...

i.e.
For f=1 to 10
CreateNewInstance.MyType = New MyTpe
CreateNewInstance\NameRef=f
Next


How you then convert the NameRef variable into a string, is up to you, but each instance will have a unique identifier this way.


soja(Posted 2004) [#3]
or are you talking about the variable name?
Type player
    Field name$
    Field score%
End Type

p1.player = New player
p2.player = New player
p3.player = New player

p1\name = "George"
p2\name = "Jimbo"
p3\name = "Bob"

Now you have created three types, and you have three player variables: p1, p2, and p3.

Note that when you use New, the resulting type gets stored in an internal linked list, so p1 is the same as "First player", p3 is the same as "Last player", and p2 is "After First player" or "Before Last player".

You canalso store type variables in an array, just like any other var.


_PJ_(Posted 2004) [#4]
You can?

Cool!!!

So the following is possible?

Dim PlayerRef(10)

PlayerRef(1).Player = new Player

etc...


soja(Posted 2004) [#5]
Sure, but not quite like that. More like this:

Dim PlayerRef.Player(10)
PlayerRef(1) = New Player
...

You learn something new every day, eh? =)


PowerPC603(Posted 2004) [#6]
You can create (for example) 10 players like this:

; Specify the type and assign fields to it
Type Player
   Field Name$
   Field Score%
End Type

; Create the array (array-indexes 0 - 10) and specify the kind of data it will hold (pointers to the type "Player")
Dim PlayerArray.Player(10)

; Create 10 type-instances of type "Player" and put the pointer to that type-instance in the array (I've only used array-indexes 1 - 10)
For i = 1 to 10
   PlayerArray(i) = New Player
Next

; Then set some names for the players
PlayerArray(1)\Name$ = "Chris"
PlayerArray(2)\Name$ = "Chandler"

; Add 10 points to the score of player 5
PlayerArray(5)\Score% = PlayerArray(5)\Score% + 10

; Compare 2 scores
If PlayerArray(1)\Score% > PlayerArray(3)\Score% then
   ; Do something
EndIf