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
0 registered members (), 18,561 guests, and 5 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
[SOLVED] Storing a function pointer in a skill #445163
08/29/14 02:46
08/29/14 02:46
Joined: Jan 2005
Posts: 330
USA
M
MatAllum Offline OP
Senior Member
MatAllum  Offline OP
Senior Member
M

Joined: Jan 2005
Posts: 330
USA
I'm trying to set up an expandable game framework, but ran into a rather important issue here: how do I store a function pointer in an entity skill?

I want to optionally have three functions that can be called by a player movement script, and each function is determined by the type of character or game mode. The simplest way to handle this is with a function pointer. However, the following code does not work:

Code:
// Function pointer defined
var bipedFunctionPtr(ENTITY* ent);

// This exists
void playerCamera(ENTITY* ent)
{
	...
}

// in function main:
player.bpd_postoutput_func = playerCamera;

// in player update loop:
if(ent.bpd_postoutput_func)
{
	bipedFunctionPtr = ent.bpd_postoutput_func;
	bipedFunctionPtr(ent);
}



Evidently, function pointers won't cast to a VAR and back again. Trying to use handles and ptr_for_handle doesn't work either - same result, crash in player loop. Any ideas how to handle (ha, ha) this issue?

Re: Storing a function pointer in a skill [Re: MatAllum] #445165
08/29/14 06:42
08/29/14 06:42
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
txesmi Offline
Serious User
txesmi  Offline
Serious User

Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
Hi,
you need to specifically cast the skill. I read JCL confirming it is perfectly safe and I have done it hundreds o times with no problem. Anyway it is a good idea to specify every cast so it shows you know what you are doing.

Code:
ent.bpd_postoutput_func = (void*)player_camera;
....
bipedFunctionPtr = (void*)ent.bpd_postoutput_func;



Alternativelly you can also build a struct with function pointers and save its handle on a skill.

Code:
typedef struct
{
   void action1 ();
   void action2 ();
   ...
} MYACTION;

void myCustomAction ()
{
   ...
}

...
MYACTIONS *myActions = sys_malloc ( sizeof(MYACTIONS) );
ent->skill1 = handle ( myActions );
myActions->action1 = myCustomAction;
myActions->action2 = ...

...
MYACTIONS *myActions = ptr_for_handle ( ent->skill1 );
myActions->action1 ();



edited____________________
Sorry but I failed. 'handle' does not work with custom structs. blush

Salud!

Last edited by txesmi; 08/30/14 06:12. Reason: error, error, error...
Re: Storing a function pointer in a skill [Re: txesmi] #445174
08/29/14 11:22
08/29/14 11:22
Joined: Jan 2005
Posts: 330
USA
M
MatAllum Offline OP
Senior Member
MatAllum  Offline OP
Senior Member
M

Joined: Jan 2005
Posts: 330
USA
Sweet, that should help.

Edit: Definitely works. Thanks dude.

Last edited by MatAllum; 08/30/14 22:19.

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