Altering a Built in Functiion

BlitzMax Forums/BlitzMax Programming/Altering a Built in Functiion

Rico(Posted 2008) [#1]
This may seem a bit strange but it makes sense for me!

When programming I put lots of DrawText commands in to overlay variable information (values) on the screen. I would really like to make all this text disappear (and reappear) with a touch of a button. The Drawtext commands are scattered through the program (they often print Local variables). I could for every Drawtext command - always precede it with a If flag then Drawtext ......... But it would be more convenient if I could just set a global variable which would be tested in the *actual* DrawText function itself and it could Return early.

Is it possible to do this? and if so how?

Thank you very much.


ImaginaryHuman(Posted 2008) [#2]
Use function pointers. If the parameters of DrawText are:

DrawText Text$,X,Y

Then you need to define a function with the same kind of parameters:

Function DrawNothing(TextToDraw:String,XPos:Int,YPosInt)
End Function

And then replace the DrawText function with this one:

DrawText=DrawNothing

I think that should work.


BladeRunner(Posted 2008) [#3]
You could do sth. like this:
Global DO:Int = True
Function DoPrint(txt:String="")
	Select DO
	Case True
		Print txt
	End Select
End Function

DoPrint "lala"
DO = False
DoPrint "lulu"



Retimer(Posted 2008) [#4]
Is that not basically the same as using an if statement blade?

I could for every Drawtext command - always precede it with a If flag then Drawtext ......... But


@imaginary, that seems like a nifty solution there - I wasn't aware you could do that in blitz, thanks.


dmaz(Posted 2008) [#5]
easy, all you need to do is overload drawtext with your own function

Global doDrawText = false

Function DrawText( text:String, x:Float, y:Float )
	If doDrawText Then BRL.Max2D.DrawText(text,x,y)
End Function



ImaginaryHuman(Posted 2008) [#6]
Oh yea, forgot you could do that directly.

Although he really doesn't need the IF ..., all you need to do is comment in/out the drawtext function:

'Function DrawText(text:String,x:Float,y:Float);End Function



dmaz(Posted 2008) [#7]
"touch of a button"... translation: while the app is running. ;)


Grey Alien(Posted 2008) [#8]
easy, all you need to do is overload drawtext with your own function
Yeah that's what I do with KeyHit and KeyDown amongst other commands. works like a treat.


Warpy(Posted 2008) [#9]
I can't get ImaginaryHuman's example to work. I've overloaded functions before, so I don't think I've got the syntax wrong, but
SuperStrict

Function DrawNothing(TextToDraw:String,XPos:Int,YPos:Int)
End Function

DrawText=DrawNothing


gives the error "Expression must be a variable". I'm using the latest SVN version, so if it works anywhere it should work for me.


tonyg(Posted 2008) [#10]
Don't you create a variable of the function :
local myfunction()
then set myfunction()=drawnothing? I think you'd still need to change all the drawtext statements but I could be very wrong.
I would do what dmaz suggested.


BladeRunner(Posted 2008) [#11]
Is that not basically the same as using an if statement blade?

Not really, because i think he meant an if everytime he uses the dtawtext command. Using the extra function will need him to do replace all drawtext with dodrawtext once and then he's free to switch the output as he wants.
Not an overloaded func, but it works.


ziggy(Posted 2008) [#12]
But you can overload it properly as long as you're not using fully qualified functions in your code as the dmaz example shown.
Function DrawText(Text:string, PosX:float, PosY:Float)
'Do whatever before drawing
.
.
.
'Do regular drawtext:
brl.max2d.drawtext(text,posx,posy) 
'do whatever after drawing:
.
.
.
End function



Rico(Posted 2008) [#13]
Thank you very much for all the help - I overloaded the function and it works nicely. Its good to get rid of that messy text sometimes :)


ImaginaryHuman(Posted 2008) [#14]
What code did you finally settle on that works?

I'm surprised the commented-out function didn't work?

This worked for me no problem:

Function DrawText(text:String,x:Float,y:Float);End Function
'Comment out the above line to let DrawText work

Graphics 320,240,0
DrawText "Hi",50,50
Flip
WaitKey



Rico(Posted 2008) [#15]
Yes that works too. to be honest I didn't understand what you meant - until you posted that example. That's a neat solution too :)
I just did what dmaz and ziggy did e.g

Graphics 1024,768,32
Global dt:Int=True  'DrawText yes/no

While Not KeyDown(KEY_ESCAPE)
     Cls
     DrawText "Hello ",0,0
     Flip 1
Wend

Function DrawText(Text:String, PosX:Float, PosY:Float)
     If dt=False Then Return
     brl.max2d.DrawText(text,posx,posy) 
End Function



tonyg(Posted 2008) [#16]
Personally, I'd want to control it by consts or variables rather than by commenting and uncommenting code.