why brakes when add new object ?

Monkey Targets Forums/Desktop/why brakes when add new object ?

teremochek(Posted 2011) [#1]
Greetings.

...
Method OnUpdate()
 If KeyHit( KEY_1 )	
  Local g:GameObject=New Player()
  g.image= LoadImage ("IMAGES.png")
  g.x=192
  g.y=300
  g.srcX=0
  g.srcY=48*19
  g.srcWidth=48
  g.srcHeight=96 		                   
  g.rotation=0
  Self.GameObjectList.AddLast(g)
 endif
end
...

When I create many objects there is a big pause ...


therevills(Posted 2011) [#2]
Maybe because you are loading the image every time you are creating a new object.

Cache the image and then assign it to the object:

Field badguyImage:Image

Method OnCreate()
  badguyImage = LoadImage("IMAGES.png")
End

Method OnUpdate()
 If KeyHit( KEY_1 )	
  Local g:GameObject=New Player()
  g.image= badguyImage
  g.x=192
  g.y=300
  g.srcX=0
  g.srcY=48*19
  g.srcWidth=48
  g.srcHeight=96 		                   
  g.rotation=0
  Self.GameObjectList.AddLast(g)
 endif
end


Also you may want to use constructors...


Shinkiro1(Posted 2011) [#3]
As therevills said, pre-cache the image.
I've once written a Bmax class for this, here a conversion to monkey (untested):

'------------------------------------------------------------------------------

' Used so images are not loaded twice

' How to use: myImage:Image = CachedImage.Load( path )

'------------------------------------------------------------------------------

Class CachedImage


Private

'------------------------------------------------------------------------------

' This Map contains all images already loaded

' key: path:String | value: image:Image

'------------------------------------------------------------------------------	

	Global RessourceMap:StringMap<Image> = New StringMap<Image>

	
	

Public

'------------------------------------------------------------------------------

' Lookup in the RessourceMap if the Image is already loaded

' If loaded     => Return the already loaded Image

' If Not loaded => Return the newly loaded Image

'------------------------------------------------------------------------------

	Function Load:Image( path:Object, flags:Int = -1 )

		If RessourceMap.Contains( path )

			Return Image( RessourceMap.Get( path ) )

		Else

			Local image:Image = LoadImage( path, flags )

			RessourceMap.Set( path, image )

			Return image

		End

	End

	



'------------------------------------------------------------------------------

' Remove the image with the speciefied path

'------------------------------------------------------------------------------

	Function Remove:Void( path:Object )

		If RessourceMap.Get( path )

			RessourceMap.Remove( path )

			Return

		End

	End

	

	

'------------------------------------------------------------------------------

' Clear the RessourceMap of all loaded Images

' Info: You still have to manually clear all other references to Images

'       in your own Classes

'------------------------------------------------------------------------------	

	Function Reset:Void()

		RessourceMap.Clear()

	End

	

End


The idea is that every image you load has a reference in the RessourceMap.
Once loaded it won't load it again but only pass the reference to the new image.

Use it like this:
myImage:Image = CachedImage.Load( path )



teremochek(Posted 2011) [#4]
Thanks.