Little help with types

Blitz3D Forums/Blitz3D Beginners Area/Little help with types

Dax Trajero(Posted 2004) [#1]
I have 4 collections of playerdata

player1.playerdata
player2.playerdata
player3.playerdata
player4.playerdata

So far I've been using FOR EACH NEXT to loop through each collection. I'm a little puzzled as to how to implement what I need to do next...

For collision detection I need to compare:

player1\x with player2\x
then player1\x with player3\x
then player1\x with player4\x

Can someone advise the best way to do this as I'm new to types and lacking any good documentation.


jhocking(Posted 2004) [#2]
Since you have distinct handles to the four type instances, you can use those names directly in your code. Exactly as you've written it in fact, just make sure to declare the type instances as global so that the handles can be used inside of any function. A For/Each/Next loop is unnecessary.

The code would be:
(at the top of your code declare global handles for the type instances)
Global player1.playerdata
Global player2.playerdata
etc

(also at the top of your code, declare the playerdata type)
Type playerdata
Field x
End Type

(somewhere else in your code, possibly inside a function, create the type instances)
player1.playerdata = New playerdata
player2.playerdata = New playerdata
etc

(now access the fields of those instances anywhere you want)
If player1\x > player2\x etc


Dax Trajero(Posted 2004) [#3]
I'd like to add that I need to do the above as part of a FOR EACH NEXT LOOP

My main program loop involves

For loop.playerdata = Each playerdata

; compare loop\x with player2\x
; compare loop\x with player3\x
; compare loop\x with player4\x

Nexy


Dax Trajero(Posted 2004) [#4]
Thanks Joe - I've managed to do all you've mentioned already.

My problem comes when trying to do the comparison as part of my main game loop as mentioned above


jhocking(Posted 2004) [#5]
Way to add that little wrinkle AFTER I spent a couple minutes responding.


Matty(Posted 2004) [#6]
One way of doing this would be as follows:
In your type have a field PlayerID, example
Type PlayerData
Field x
field y
field z
field PlayerID
end type

then do the following:
For player.Playerdata=each playerdata
if player\playerid=1 then
for otherplayer.playerdata=each playerdata
if player\playerid<>otherplayer\playerid then "Compare the two players x .."
next
endif
next

There are better ways of doing this though, for example you
could make an array of types (see the custom types documentation in blitz) and do the check as follows:

type playerdata
field y
field z
field x
end type

dim player.playerdata(4)
for i=0 to 4
player(i)=new playerdata
next

for i=2 to 4
"compare" player(1)\x with player(i)\x
next


Dax Trajero(Posted 2004) [#7]
I'm sorry about that!


Dax Trajero(Posted 2004) [#8]
thats great guys - thank you


Dax Trajero(Posted 2004) [#9]
just implemented it in my game and my car to car collision is working perfectly!

thanks Matty for the code and JHocking for your patience