Types???

Blitz3D Forums/Blitz3D Programming/Types???

Spot(Posted 2003) [#1]
Allright I have a couple of simple questions about types.
1. How does the computer tell the difference between them... If they all have the same name/thing then how does it know whem apart?

Example:

type player
field x,y,z
end type

p.player=new player
p\x=10
p\y=11
p\z=12

p.player=new player
p\x=24
p\y=60
p\z=100

Now how does the computer know the difference between the first p.player and the second p.player?

2. Am I doing that code wrong and if so then how do I fix it?

3. If I am doing the code right, how would I tell the computer to say delete the first one but not the second?


GfK(Posted 2003) [#2]
1. it doesn't.

2. Add an extra field called "PlayerNumber%".
Select P\Playernumber
  Case 1 : ;Do player 1 stuff
  Case 2 : ;Do player 2 stuff
End Select

3. :-
For P.player = each player
  If p\playernumber = 1
    Delete p
  Endif
Next



big10p(Posted 2003) [#3]
Using your example, all the player types you create (using the New command) are stored in a list. As such, Blitz doesn't actually need a separate variable to point to each instance of player, it's only if you (the programmer) need to have access to a specific instance that you can then assign a variable to point to it.

Often, the programmer only needs to go through the entire list of a specific type to do some calculation, and this is achieved with:

For p.player = each player
    p\x = p\x + 1 ; Add 1 to the x field of every player type
next


Blitz also has other commands to move through a list of types, specifically: First, Last, Before and After.


Spot(Posted 2003) [#4]
Okay. How does it know which p.player to write too?


big10p(Posted 2003) [#5]
You mean in the For loop? Well, first of all p.player is set to point to the first player type in the list, then after the next statement p.player points to the next player type in the list - and so on until all instances of player have been addressed.

Is that what you meant?


Spot(Posted 2003) [#6]
Not exactly...

How would know to write to the second string as opposed to writing to the first one...

Example:
type player
field x,y,z
end type

p.player=new player
p\x=10
p\y=11
p\z=12

;How would this know to write to the first p.player

p.player=new player
p\x=24
p\y=60
p\z=100
;And this know to write to the second p.player?


Anthony Flack(Posted 2003) [#7]
Every time you type "new player", another player is added to the list. Unless you move it (you can change the order using the INSERT command), it will be last in the list.

To answer your original question, "how would I tell the computer to say delete the first one but not the second?" - the answer is simple:

Delete first player

But to delete the SECOND player and not the first, would be more like:

p.player=first player ;points to first player
p=after p ;now it points to the next type object
delete p ; zap!

So the commands you'll want to keep in mind for navigating the lists are:

FIRST
LAST
EACH
AFTER
BEFORE
INSERT

...and also NULL - indicating a pointer points at nothing, eg: If p=NULL then (do something)