Pooling objects with Ignition X

Monkey Forums/Monkey Beginners/Pooling objects with Ignition X

chrisbck(Posted 2016) [#1]
Hi,

I'm pretty new with Monkey and only recently purchased Ignition X and I'm loving both of them. I've been experimenting with pooling objects to try get my head around it as I would find this a useful feature.

I put together the following code, based on the examples, which generates a runtime error 'TypeError t_s is null' when the pool is used up. I would expect it to stop when the pool is empty like the Ignition examples?

[CODEBOX]
Strict
' Experiment to get pooling function working
' it will create a pool of squares and draw where mouse clicks

Import playniax.ignitionx.engine
Import pooledSquareClass

Global gameScene:Game

Class Game Extends iEngine

Field square:squarePool = New squarePool
Field playfield:iPlayfield
Field layer:iLayer

Method OnCreate:Int()

playfield = New iPlayfield
playfield.AttachLast Self

layer = New iLayer
layer.AttachLast playfield

square.MakePool(10)

Return 0
End

Method OnUpdate:Int()

If TouchDown() square.MakeSquare(playfield.MouseX(), playfield.MouseY(), layer)

Return 0
End
End

Class MyApp Extends iApp

Method OnCreate:Int()
gameScene=New Game
iStart 60

Return 0
End

End

Function Main:Int()
New MyApp
Return 0
End
[/CODEBOX]

[CODEBOX]
Strict
' creates a pool of squares

Import playniax.ignitionx.engine


Class squarePool

Field squares:iPool

Method MakePool:Int(poolSize_:Int)

squares = New iPool(poolSize_)

For Local i:= 0 To squares.Length-1
Local square:= New iLayerRectangle
square.Size(30, 30)
squares.Allocate square
Next

Return 0
End

Method MakeSquare:Int(x_:float, y_:float, layer_:iLayer)

Local s:= iLayerRectangle(squares.GetAvailable())

s.AttachLast layer_
s.Position(x_, y_)

Return 0
End
End
[/CODEBOX]

Can anyone explain where I went wrong?


chrisbck(Posted 2016) [#2]
Figured it out:

[CODE]
Method MakeSquare:Int(x_:float, y_:float, layer_:iLayer)

Local s:= iLayerRectangle(squares.GetAvailable())
If s Then
s.AttachLast layer_
s.Position(x_, y_)
EndIf

Return 0
End
[/CODE]

Silly mistake:(


Playniax(Posted 2016) [#3]
okay :)


chrisbck(Posted 2016) [#4]
Been playing around with this for a while now that I got it running and have a couple of questions if you don't mind.

I've got that once the Alpha property reaches zero the pooled object is available again, is there any other ways for them to become available without disabling them all, or maybe overwrite one?

What does the Ghost property do? I didn't notice any difference when i set it.

Thanks,
Chris


Playniax(Posted 2016) [#5]
I've got that once the Alpha property reaches zero the pooled object is available again

This is with particles. When alpha is zero the objects is disabled or removed depending on Tattoo flag set or not. So it will be available again when it is disabled.

What does the Ghost property do?

I assume your are talking about the sprite Ghost flag? This flag is used by the collision system. By default the ghost colors are set to 255 so nothing will show. To change the colors you can do something like this:
sprite.GhostRed=255
sprite.GhostGreen=0
sprite.GhostBlue=0

Now the sprite will be red for one frame when a collision happens creating a flicker.

You can also set a ghost image by using GhostImagePointer.


chrisbck(Posted 2016) [#6]
Okay thanks, I'm getting more familiar with it each day, I'm impressed so far and still scratching the surface.


Playniax(Posted 2016) [#7]
Thanks! Great to hear...