Hi, so I'm trying to make an events handler and I was doing a test on how the function pointers in freebasic work, I thought that would be a nifty way to accomplish my task. But I don't quite understand why this is a problem. If I am going about this the wrong way, I would like to know the proper way to do this...
#include "fbgfx.bi"
using FB
type EventHandler
dim OnSpace as sub
declare sub Update()
end type
sub EventHandler.Update()
if multikey(SC_SPACE) then
this.OnSpace()
end if
end sub
dim shared Events as EventHandler
type Program
private:
dim Text as string = "Space has not been pressed"
public:
declare sub Main()
declare sub OnSpace()
end type
sub Program.Main()
do
cls
print this.Text
Events.Update()
loop until multikey(SC_ESCAPE)
end sub
sub Program.OnSpace()
this.Text = "Space has now been pressed"
end sub
dim App as Program
Events.OnSpace = @App.OnSpace()
App.Main()
Thank you for your help!
Last edited by Game Coder on Jun 05, 2017 7:54, edited 1 time in total.
How would I be able to handle this then? I'd need some kind of workaround for that to have any kind of sensible organization for my library in terms of use...
I also attempted making the sub for OnSpace() static, but again I just get the same error on that line. Does it just not accept it from a UDT at all? The only workarounds I can think of are very poor and seem like very bad code to me, I would greatly appreciate any ideas you had.
A workaround to your target code, but that becomes a little twisted.
Your "wished" pointer to a non static member procedure is transposed to a pointer to a static member procedure with one parameter to pass (a 'Program' instance parameter to replace the 'this' hidden instance parameter of a non static member procedure):
#include "fbgfx.bi"
using FB
type _Program as Program
type EventHandler
dim OnSpace as sub(byref as _Program)
declare sub Update(byref p as _Program)
end type
dim shared Events as EventHandler
type Program
private:
dim Text as string = "Space has not been pressed"
public:
declare sub Main()
declare static sub OnSpace(byref p as Program)
end type
sub Program.Main()
do
cls
print this.Text
Events.Update(this)
loop until multikey(SC_ESCAPE)
end sub
static sub Program.OnSpace(byref p as Program)
p.Text = "Space has now been pressed"
end sub
sub EventHandler.Update(byref p as Program)
if multikey(SC_SPACE) then
this.OnSpace(p)
end if
end sub
dim App as Program
Events.OnSpace = @Program.OnSpace
App.Main()
Last edited by fxm on Jun 05, 2017 9:02, edited 5 times in total.
#include "fbgfx.bi"
using FB
type EventHandler
protected:
dim OnSpace as sub(byref as EventHandler)
declare sub Update(byref as EventHandler)
end type
sub EventHandler.Update(byref e as EventHandler)
if multikey(SC_SPACE) then
if this.OnSpace <> 0 then
this.OnSpace(e)
end if
end if
end sub
type Program extends EventHandler
private:
dim Text as string = "Space has not been pressed"
public:
declare constructor(byval as any ptr)
declare sub Main()
declare static sub OnSpace(byref as Program)
end type
constructor Program(byval p as any ptr)
base.OnSpace = p
end constructor
sub Program.Main()
do
cls
print this.Text
this.Update(this)
loop until multikey(SC_ESCAPE)
end sub
static sub Program.OnSpace(byref p as Program)
p.Text = "Space has now been pressed"
end sub
dim App as Program = @Program.OnSpace
App.Main()