Function pointer

Posted By: Walori

Function pointer - 05/14/11 18:41

GS version 7.86.2

This one's killing me, how can I use functions pointers they way that I have pointer x (let's say function xys(parameters) ) which I put to void* function_array[0] place and then call "xys" from the array void? Currectly I get the function called but it causes problems with movement. Below are the code parts that are relevant, to check full code file check the links.

MOVEMENT:
https://bitbucket.org/WinKIller0/acknexproject/src/424d047ac418/Data/movement.c
Code:
//SPAWN is a predefined struct which has VECTOR movement in it
//Me is a pointer to a entity inside SPAWN struct
...
	if(vec_valid(spawn->movement) != 0 && me->anim_attack == 0)//If there's movement and no attack
	{
		spawn->distance_moved = c_move(me,vector(spawn->movement.x*time_step,spawn->movement.y*time_step,spawn->movement.z*time_step),nullvector,GLIDE | IGNORE_ME | IGNORE_PASSABLE | IGNORE_SPRITES | IGNORE_CONTENT | IGNORE_MAPS | IGNORE_FLAG2);//Move entity by movement vector in characte struct
	}
	else spawn->distance_moved = 0;



EFFECT
https://bitbucket.org/WinKIller0/acknexproject/src/424d047ac418/Data/effects.c
Code:
//Based on aum99 example of hollogram effect, nothing weird here
function hollogram_effect(PARTICLE*p)
{
	set(p,UNLIT | BRIGHT | TRANSLUCENT);
	p.bmap = NULL;
	p.size = 1;
	p.lifespan = 0.1;
	//	vec_set(p.x,vector(p.x-1+random(2),p.y-1+random(2),p.z-1+random(2)));
	p.alpha = random(100);
	p.event = NULL;
}



ACTUAL EFFECT FUNCTION CALLED

Code:
float hollogram(ENTITY* ent)
{
//This function is passed to void* array outside of the function. This is called from the function below
	ent.alpha = 10;
	me = ent;
	for(scn_effect_i=0;scn_effect_i<ent.vertices;scn_effect_i++)
	{
		vec_for_vertex (effect_align, ent, scn_effect_i);
		effect(hollogram_effect,1,effect_align,nullvector);
	}
	return 1;
}



THE CALLER CODE
https://bitbucket.org/WinKIller0/acknexproject/src/424d047ac418/Data/scene_functions.c
Code:
float function_effect(ENTITY* ent);//Defined function pointer
void* spawn_effect[200];//Array for functions


me = ent_next(NULL);
while(me)//Loop until breaked internally
{
	...
	spawn_move(spawn_cur);//Call movement function (the first code)
	...
	me.my_effect = 1;
	if(spawn_effect[me.my_effect] != 0)
	{
		function_effect = spawn_effect[1];//Get "hollogram"
		function_effect(me); //Call hollogram
		//	function_effect = NULL;
		uu1++;
	}
	...
	me = ent_next(me);
}



Does anyone what may cause the c_move stop working? I have narrowed down the following that may have caused it:
*movement vector doesn't get values anymore(WORKS)
*Loop doesn't continue to next spawn anymore(WORKS)
*Loop inside the function that gets called causes movement freeze(tested,even with nothing inside the function, movement freezes)
*Movement code itself is freezing itself (WORKS, even if movement doesn't happen the c_mve inside the function is called)

+I have animations, sound and lod functions also which continue working when movement freezes
Posted By: Walori

Re: Function pointer - 05/21/11 06:39

Any thughts about this one? Still the same problem but can't seem to find the reason why effect only affects the movement and c_move.
Posted By: HeelX

Re: Function pointer - 05/21/11 07:23

Hey Walori,
I don't think that your problems are related to the use of function pointers, but the usage of the global entity pointer "me".

Can you please do the following?: In the caller code, open a local ENTITY* pointer like this:

Code:
ENTITY* e = ent_next(NULL);



and use "e" instead of me in the whole loop. Maybe you can put into the signature of spawn_move(spawn_cur); an entity parameter as well, whereas you pass "e" and use that one in c_move.

Using wildly the "me" or "my" pointer can cause many problems. Try to avoid that, except you are in an entity-action.
Posted By: Walori

Re: Function pointer - 05/21/11 12:36

Thank you for your help but this didn't make any difference unfortunetly. The function still freezes the movement
Posted By: Walori

Re: Function pointer - 05/28/11 18:06

Ok I solved the problem, however this provided a question. When I removed "return 1" statement from the float type function everything started to work well. However I thought float has to have (or can have) return statement at the end of the function, as the examples in manual have. With void functions you couldn't use return, in C at least (source: http://www.cs.cf.ac.uk/Dave/C/node8.html).

So why there's this type of behaviour in Lite-C and in Acknex. Why doesn't float function want return when it's put to void* array?

As my computer can't run the code in A8 I can't produce the problem with the recent version.
© 2024 lite-C Forums