Functions modifying variables they shouldnt

BlitzPlus Forums/BlitzPlus Programming/Functions modifying variables they shouldnt

Mordax_Praetorian(Posted 2005) [#1]
I'm trying to make a function that will highlight an item on-screen when the player walks near it

To do this I'm altering the image for the item, putting it in a different variable and swapping between them at the apropriate time

Flamberge\ImageClose = ShowTile (Flamberge\Image)


This function however seems to modify the original Flamberge\Image before putting it in the new variable

The function itself looks like this, and uses a function I did earier that I'll also paste at the bottom:


Function ShowTile (tile,save=0)
	If Save = 1
		Flip False
	EndIf
	tile = ColourSub(tile,0,0,0,0,255,255)
	If Save = 1
		Flip False
	EndIf
	Return tile
End Function


Function ColourSub(Image,OldR,OldG,OldB,NewR,NewG,NewB)
	MaskImage Image, OldR,OldG,OldB
	ClsColor NewR,NewG,NewB
	Cls
	DrawImage Image, 0, 0
	GrabImage Image, 0,0
	ClsColor 0,0,0
	Return Image
End Function


Update: Putting the image into a new variable inside the function has no affect, and skipping the top function and just using the ColourSub function directly also seems to have no affect

I'm quite sure that functions usualy dont affect variables being passed to them unless being returned to the same variable


Blaine(Posted 2005) [#2]
Blitz ALWAYS passes types and thier contents as pointers, meaning that you are working with the actual data, not a copy of the data as you normally would.


Grey Alien(Posted 2005) [#3]
So variables remain unaltered (actually sometimes it would be nice to pass them by reference so they could be altered) but Images and Sounds and Channels etc are just pointers so the memory that they point at will get altered. Also if you reassigned the image/sound/channel pointer in the fucntion, it would still point at the old value outside of the function.


Mordax_Praetorian(Posted 2005) [#4]
hmm, thats inconvienient but I think I can work around it

Thanks for the help


Grey Alien(Posted 2005) [#5]
It's inconvenient for your function, but very sensible in the long run. Otherwise thingk of the memory and CPU overhead if an image was copied every time it was passed into a function! That's what you need to do btw, use the Image passed in to call temp=CreateImage with the correct sizes, then set the buffer to Image and do a grab image to temp and then work on temp.


Blaine(Posted 2005) [#6]
You can also just use CopyImage(), I believe. :)

Also, I do agree that we need more control over memory (pointer) stuffs in Blitz. :)


Grey Alien(Posted 2005) [#7]
yeah thanks Blaine, What *was* I thinking!


Mordax_Praetorian(Posted 2005) [#8]
ok, thanks for the help people, I have the functions working now