GLBasic forum

Main forum => Bug Reports => Topic started by: MrTAToad on 2012-Jul-02

Title: PROTOTYPE Improvement
Post by: MrTAToad on 2012-Jul-02
Would be nice if PROTOTYPE could return a type as a return parameter, something like :

PROTOTYPE _ONPROCESS AS tResult:speed

At the moment, it generates a compiler error.
Title: Re: PROTOTYPE Improvement
Post by: mentalthink on 2012-Jul-02
Hi MRTatoad, the fact for make the prototype of a function... it´s like declare a variable?¿... I don´t understand too much reading C++ books, says it´s obligatory (other not), but it´s only like a "visual Help"
Title: Re: PROTOTYPE Improvement
Post by: MrTAToad on 2012-Jul-03
The type would be known though - after all you can pass types as a parameter :)

In addition, it would be nice if you could pass a function directly rather than having to use a variable.
Title: Re: PROTOTYPE Improvement
Post by: mentalthink on 2012-Jul-03
THX!!!
Title: Re: PROTOTYPE Improvement
Post by: MrTAToad on 2012-Jul-04
No problem!
Title: Re: PROTOTYPE Improvement
Post by: Slydog on 2012-Jul-04
Quote from: MrTAToad on 2012-Jul-03
In addition, it would be nice if you could pass a function directly rather than having to use a variable.

+1  :good:
Otherwise it makes your code less 'clean' and harder to manage.
Title: Re: PROTOTYPE Improvement
Post by: bigsofty on 2012-Jul-04
You know, this is one of those commands I have never touched. I had to do a quick manual lookup to check out the details. Although I understand its function, what is the practical use?

The suggestions that Mr. T proposed sound perfectly logical to me too BTW.
Title: Re: PROTOTYPE Improvement
Post by: Slydog on 2012-Jul-04
I use it in my GUI library.
When defining a new button, for example, you can pass a function that will be called automatically when that button is clicked.

It could be used for a timer library that will call a specified function when the timer runs out.

It basically allows your libraries to have custom / user specified events.
Title: Re: PROTOTYPE Improvement
Post by: MrTAToad on 2012-Jul-04
Quote from: Slydog on 2012-Jul-04
Quote from: MrTAToad on 2012-Jul-03
In addition, it would be nice if you could pass a function directly rather than having to use a variable.

+1  :good:
Otherwise it makes your code less 'clean' and harder to manage.
Yes - at the moment I have to stick all the function pointers in an array and then pass that to a function.
Title: Re: PROTOTYPE Improvement
Post by: MrTAToad on 2012-Jul-04
Quote from: bigsofty on 2012-Jul-04
You know, this is one of those commands I have never touched. I had to do a quick manual lookup to check out the details. Although I understand its function, what is the practical use?

The suggestions that Mr. T proposed sound perfectly logical to me too BTW.
It is GLBasic's pointer type and thus is only really used for advanced situations...
Title: Re: PROTOTYPE Improvement
Post by: Slydog on 2012-Jul-04
Here's some code to illustrate:

Subset of my MainMenu module:
Code (glbasic) Select
GLOBAL fn_button_options AS PGui_Button_Click
fn_button_options = MenuMain_Options
guiMain.ButtonAdd("Options", button.x, button.y, button.width, fn_button_options)

FUNCTION MenuMain_Options: state%
GameState(STATE_MENU_OPTIONS)
ENDFUNCTION


In my GUI library:
Code (glbasic) Select
PROTOTYPE PGui_Button_Click: id%

FUNCTION ButtonAdd%: caption$, x%, y%, w%, fn_click AS PGui_Button_Click
LOCAL button AS TGui_Button
button.Set(caption$, x, y, w, fn_click)
DIMPUSH self.buttons[], button
ENDFUNCTION


[Edit] This is how it would look in the MainMenu module if you could pass a function directly (no need for the 'fn_button_options' variable):
Code (glbasic) Select
guiMain.ButtonAdd("Options", button.x, button.y, button.width, MenuMain_Options)

FUNCTION MenuMain_Options: state%
GameState(STATE_MENU_OPTIONS)
ENDFUNCTION
Title: Re: PROTOTYPE Improvement
Post by: bigsofty on 2012-Jul-04
Ah, that quite clear now. It could be very handy in many situations, I am thinking of possibly a quick'n'dirty way of emulating function overloading esp.

It reminds me of ALIAS for some reason too, even though that command only deals with variables.

Thanks for the info. :)
Title: Re: PROTOTYPE Improvement
Post by: MrTAToad on 2012-Jul-05
It can be rather useful.  To expand on the demo above, currently I have to use :

Code (glbasic) Select
module.OnProgramStart=titleOnProgramStart
module.OnProgramEnd=titleOnProgramEnd
module.OnResolutionChange=titleOnResolutionChange
module.OnDisplay3D=titleOnDisplay3D
module.OnDisplay2D=titleOnDisplay2D
module.OnRoutineStart=titleOnRoutineStart
module.OnRoutineEnd=titleOnRoutineEnd
module.OnProcess=titleOnProcess
module.OnLanguageChange=titleOnLanguageChange
IF _setup.RegisterModule("title",module)=TRUE
module.OnProgramStart=activatePlayersOnProgramStart
module.OnProgramEnd=activatePlayersOnProgramEnd
module.OnResolutionChange=activatePlayersOnResolutionChange
module.OnDisplay3D=activatePlayersOnDisplay3D
module.OnDisplay2D=activatePlayersOnDisplay2D
module.OnProcess=activatePlayersOnProcess
module.OnRoutineStart=activatePlayersOnRoutineStart
module.OnRoutineEnd=activatePlayersOnRoutineEnd
module.OnLanguageChange=activatePlayersOnLanguageChange
IF _setup.RegisterModule("activateplayers",module)=TRUE
module.OnProgramStart=mainProgramOnProgramStart
module.OnProgramEnd=mainProgramOnProgramEnd
module.OnResolutionChange=mainProgramOnResolutionChange
module.OnDisplay3D=mainProgramOnDisplay3D
module.OnDisplay2D=mainProgramOnDisplay2D
module.OnProcess=mainProgramOnProcess
module.OnRoutineStart=mainProgramOnRoutineStart
module.OnRoutineEnd=mainProgramOnRoutineEnd
module.OnLanguageChange=mainProgramOnLanguageChange
IF _setup.RegisterModule("mainProgram",module)=TRUE
_setup.Main("#title")
ENDIF
ENDIF
ENDIF
Title: Re: PROTOTYPE Improvement
Post by: Falstaff on 2012-Jul-07
Yeah I've been using Prototype for emulating 'event driven' code for my GUI stuffs as well. It's ok, although you can get some confusing errors sometimes when doing Prototype/Alias stuff.
Title: Re: PROTOTYPE Improvement
Post by: MrTAToad on 2012-Jul-09
Yes, you have to be careful with it :)