Object "Instancing"

BlitzMax Forums/BlitzMax Beginners Area/Object "Instancing"

Gabriel(Posted 2006) [#1]
When I say instancing, I mean in the sense of making an object pointer which changes when the object does ( the object, not the data within, which of course is updated automatically. )

Something like this demonstrates the problem :

SuperStrict

Type pImage
	
	Field Width:Int
	Field Height:Int
	Field Data:Byte[]
	
End Type



Type pGhost
	
	Field Img:pImage
	
End Type



Global AnImage:pImage=New pImage
AnImage.Width=100
AnImage.Height=200
Global AGhost:pGhost=New pGhost
AGhost.Img=AnImage
AnImage=New pImage
AnImage.Width=99
AnImage.Height=199


' RESTORE THE GHOST IMAGE HERE
'/RESTORE THE GHOST IMAGE HERE


DebugLog AGhost.Img.Width


AnImage is now a different object, but AGhost still points to the old image. Obviously it's not an example I would use, but it's simpler to read than a real world example. A real world example ( to show why I need this ) would be when the user changes resolution mid-way through a game. I don't want to destroy all my game objects ( Ghosts ) but I have to reload all the images when the res changes. So I need to restore all the ghosts ( and other game objects ) so they refer to valid image objects.

I'd prefer not to do something unwieldy like maintaining lists of every game object which uses each image object, though this would work, I suppose.

I think it's my old nemesis, the pointer, doing my head in again.


Dreamora(Posted 2006) [#2]
You can't.
You will need to update all pointing references if you change the image or any content.


Gabriel(Posted 2006) [#3]
You will need to update all pointing references if you change the image or any content.

That was the purpose of the question. Determining the best to do it.


Dreamora(Posted 2006) [#4]
I would most likely create a new image object that holds the image and other stuff connected to it.
in that case its no problem to change the actual TImage reference ...
If needed you could even add a list to this image container with all other objects referencing to it, to propagandate the change of image.


FlameDuck(Posted 2006) [#5]
I think your design is backwards. Instead of updateing the pImage type, you should update the pGhost type. Since that has a reference to the Image you're trying to update, it can change the dimensions, if it needs to.