Find Object of a Type

Blitz3D Forums/Blitz3D Programming/Find Object of a Type

maverick69(Posted 2004) [#1]
I'm using the following sourcecode to find an object in an type and update it.
Function UpdateSprite(CurrentSprite,x,y)     
  For Sprite.Sprite = Each Sprite
     If Sprite\Sprite=CurrentSprite Then Exit
  Next
  Sprite\x = x
  Sprite\y = y
End Function


In source from other people I found a function called Object, it looks something like that:

Sprite\Sprite = Object.Sprite(something);

but I didn't find anything in Blitz3D-Helpfile, so I just wondering how this command works.


Stevie G(Posted 2004) [#2]
Do a search for Handle and Object on the forums - plenty of code there to demonstrate it uses. Alot of folk store the Handle of the type instance in the Entities Name - not sure if you can do this with sprites.

Anyhoo, you may want to consider using an array of types for this purpose.

Global sprites = 100

Type sprite_type
 field sprite 
 field x, y
end type

dim sprite. sprite_type ( sprites-1 )


Then to construct the type instance use something like this ..

for l = 0 to sprites-1
 sprite ( l ) = new sprite_type
 sprite ( l )\x = whatever
 sprite ( l )\y = whatever
next


Then you can directly access the type instance with this ..

update_sprite ( sprite( current_sprite ) , 5, 6 )

Function update_sprite ( check.sprite_type, x, y )
  check\x = x
  check\y = y
End Function


This should probably work quite well for you although I'd recommend recyling the type instances rather than deleting and creating new ones when required.

Stevie.


Zethrax(Posted 2004) [#3]
Basically, the unofficial command 'Handle()' accepts a Type pointer as a parameter and spits out an integer number which is a reference to the Type element stored in the Type pointer.

The unofficial command 'Object.whatever_type()' accepts an integer value (which should be a value output by the 'Handle()' command) and spits out a Type pointer value. The 'Object' keyword should be followed by a dot and then by the name of the Type being output.

This allows you to use Types in a polymorphic fashion.

Example:

Type my_type
Field whatever
End Type

Global this_is_my_type.my_type = New my_type
Global this_is_my_other_type.my_type

integer_variable = Handle( this_is_my_type.my_type )
this_is_my_other_type.my_type = Object.my_type( integer_variable )