Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
3 registered members (NewbieZorro, TipmyPip, 1 invisible), 19,045 guests, and 8 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
function pointer problem #156433
09/23/07 07:24
09/23/07 07:24
Joined: Oct 2006
Posts: 106
i_program_games Offline OP
Member
i_program_games  Offline OP
Member

Joined: Oct 2006
Posts: 106
Ok, here we go. The code below will crash in the demo_event function where it is commented and trying to call a function via function pointer. This method of using a function pointer works in CScript. How do I get it to work in lite-C?

If you can copy/paste the below into a test project. Attach the demo_action to an entity and mouse click that entity.



function pFunction();//pointer to function

function fBeep()
{
beep();
}

function demo_event()
{
if(event_type == EVENT_CLICK)
{
pFunction = ptr_for_handle(my.skill50);
pFunction();//WHY DOES IT CRASH HERE?
}
}

action demo_action()
{
my.emask = ENABLE_CLICK;
my.event = demo_event;
my.skill50 = handle(fBeep);//store handle to the fBeep function
}

Last edited by i_program_games; 09/23/07 07:43.

Chaos is a paradox consistently inconsistent.
Re: function pointer problem [Re: i_program_games] #156434
09/23/07 07:36
09/23/07 07:36
Joined: Aug 2005
Posts: 390
Florida
O
oldschoolj Offline
Senior Member
oldschoolj  Offline
Senior Member
O

Joined: Aug 2005
Posts: 390
Florida
Im not that good of a coder, but don't you need ptr_for_handle?

example:

my.skill50 = handle(fBeep);//store handle to the fBeep function
pFunction = ptr_for_handle(my.skill50);//now pFunction points to fbeep


also, I'm not sure, but did you define skill50 like this:

#define SOMETHING skill50

Hope I helped


you can find me with my face in the keyboard, unshaven, listening to some nameless techno tragedy, and hashing through code over a cold cup a stale joe. __________________________________ yours truly
Re: function pointer problem [Re: oldschoolj] #156435
09/23/07 07:44
09/23/07 07:44
Joined: Oct 2006
Posts: 106
i_program_games Offline OP
Member
i_program_games  Offline OP
Member

Joined: Oct 2006
Posts: 106
Yes, you are correct. It was a typo which I have now corrected in the code above but still crashes. Thanks for the response though.


Chaos is a paradox consistently inconsistent.
Re: function pointer problem [Re: i_program_games] #156436
09/23/07 08:00
09/23/07 08:00
Joined: Oct 2006
Posts: 106
i_program_games Offline OP
Member
i_program_games  Offline OP
Member

Joined: Oct 2006
Posts: 106
I just created 4 global variables for debugging. In each variable I stored a handle to a separate function. When looked at the values they are all the same even though each function handle I stored should have been different.


Chaos is a paradox consistently inconsistent.
Re: function pointer problem [Re: i_program_games] #156437
09/23/07 08:07
09/23/07 08:07
Joined: Oct 2006
Posts: 873
S
Shadow969 Offline
User
Shadow969  Offline
User
S

Joined: Oct 2006
Posts: 873
AFAIK ptr_for_handle and handle are only for entities, for other structures you have to use something else, like

my.skill10 = fBeep;
pFunction = *(void)my.skill10;

I am almost sure it is wrong, because i'm not very familiar with all pointer-based stuff, maybe some users with C knowledge can help you

EDIT: you can use "Edit" button to modify or edit your previous posts, please dont post twice

Last edited by Shadow969; 09/23/07 08:09.
Re: function pointer problem [Re: Shadow969] #156438
09/23/07 08:16
09/23/07 08:16
Joined: Oct 2006
Posts: 106
i_program_games Offline OP
Member
i_program_games  Offline OP
Member

Joined: Oct 2006
Posts: 106
You could be correct but the documentation even says that handle will work with "actions" which are similar to functions. Anyway, this method DID work with CScript so what is the equivalent for lite-C. A pointer is simply an address storing an address to a physical location in memory and every handle I see coming back is the same number regardless of what function or action I save the handle to. Is handle broken when it comes to functions?

Last edited by i_program_games; 09/23/07 08:17.

Chaos is a paradox consistently inconsistent.
Re: function pointer problem [Re: i_program_games] #156439
09/23/07 08:40
09/23/07 08:40
Joined: Aug 2005
Posts: 1,558
HK
V
vlau Offline
Serious User
vlau  Offline
Serious User
V

Joined: Aug 2005
Posts: 1,558
HK
handle(object) just handle the following objects :
ENTITY*, ACTION*, STRING*, BMAP*, VIEW*, PANEL*, TEXT*, FONT*, SOUND*, or MATERIAL* pointer.

For function pointer in LiteC, you need to declare a function prototype like
Quote:


float myfunction(int a, float b); // define a function pointer named "myfunction"

float fTest(int a, float b) { return (a*b); }
...
myfunction = fTest;
x = myfunction(y,z);




quoted from the manual->Pointer.

Re: function pointer problem [Re: vlau] #156440
09/23/07 09:14
09/23/07 09:14
Joined: Oct 2006
Posts: 106
i_program_games Offline OP
Member
i_program_games  Offline OP
Member

Joined: Oct 2006
Posts: 106
I have read this documentation prior to posting. I have no problem creating a function pointer and assigning another function to it if done properly. However, I have yet to successfully store a handle to a function and return it via ptr_for_handle with lite-C (I always do it with CScript). Every time I use the handle function I get the same returned value regardless of what type of function I give it. This is also true of actions. I think the handle function may be broke in lite-C for functions and actions.


Chaos is a paradox consistently inconsistent.

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1