Calling a function pointer stored as a structure field?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Calling a function pointer stored as a structure field?

Post by Quin »

I have a structure that stores a callback pointer on it, but I can't figure out how to actually call it. This seems like something that should be trivial, but including the parens says garbage at the end of the line. Removing them says syntax error.

Code: Select all

EnableExplicit

Structure Test
Name.s
*Callback
EndStructure

Procedure TestProc()
MessageRequester("This is a", "Test")
EndProcedure

Global T.Test
t\Name = "Test"
T\Callback = @TestProc()
T\Callback()
User avatar
STARGÅTE
Addict
Addict
Posts: 2259
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Calling a function pointer stored as a structure field?

Post by STARGÅTE »

You have to use Prototype:

Code: Select all

EnableExplicit

Prototype TestProc()

Structure Test
Name.s
*Callback.TestProc
EndStructure

Procedure TestProc()
MessageRequester("This is a", "Test")
EndProcedure

Global T.Test
t\Name = "Test"
T\Callback = @TestProc()
T\Callback()
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Calling a function pointer stored as a structure field?

Post by Quin »

STARGÅTE wrote: Fri Dec 13, 2024 6:31 am You have to use Prototype:

Code: Select all

EnableExplicit

Prototype TestProc()

Structure Test
Name.s
*Callback.TestProc
EndStructure

Procedure TestProc()
MessageRequester("This is a", "Test")
EndProcedure

Global T.Test
t\Name = "Test"
T\Callback = @TestProc()
T\Callback()
Ah, makes sense. It all works, thanks! :)
Post Reply