Entity pointer passed to function

Posted By: Germanunkol

Entity pointer passed to function - 10/15/10 09:36

We can't seem to figure this one out, though I guess the answer is prett simple.
The below code should - in my oppinion - stop counting up my_temp[0] as soon as "t" is pressed. When t is pressed, the entity counter in the F11 debug panel is set back to 0, so the entity IS removed. Still, the function which I passed the entity to keeps running.
I don't understand why I can manipulate inLight fine (inLight.x += time_step; for example) but inLight != NULL never returns 0, even when the entity does no longer exist.
Funny thing: my_temp[0] gets reset to 0 when I press t, but then keeps counting up. it's like the real entity is removed but replaced by some fake entity so the function can keep running?

Code:
var my_temp[9];
FONT* fallbackFont = "Arial#12b"; // truetype font 

PANEL* deb_pan = {
	pos_x = 20;
	pos_y = 300;
	layer = 1200;
	digits = 0,12,8.4,fallbackFont,1,my_temp[0];
}


void runWhileEntity(ENTITY* inLight)
{
	while(inLight != NULL)
	{
		inLight.x += time_step;
		my_temp[0] = inLight.x;
	}
	beep();
	beep();
	beep();
}

void main()
{
	set(deb_pan,SHOW);
	level_load(NULL);
	wait(3);
	
	ENTITY* theEnt;
	theEnt = ent_create(NULL,nullvector,NULL);

	runWhileEntity(theEnt);
	while(key_t == OFF) wait(1);
	ent_remove(theEnt);
}


Posted By: WretchedSid

Re: Entity pointer passed to function - 10/15/10 09:42

Although you remove the ENTITY, the pointer still points to the memory area where the entity was. It doesn't gets set to NULL because you never set it to NULL.
Posted By: Germanunkol

Re: Entity pointer passed to function - 10/15/10 11:00

Well, ent_remove(theEnt); theEnt = NULL; doesn't work either, because it's not the same pointer.
So how do I set ENTITY* inLight to null? from outside the function? I can't, right? cause the scope of that variable's limited to the function...?


Posted By: WretchedSid

Re: Entity pointer passed to function - 10/15/10 11:24

You can pass a pointer to a pointer like this:

Code:
void runWhileEntity(ENTITY** inLight)
{
	while(*inLight != NULL)
	{
		*inLight.x += time_step;
		my_temp[0] = *inLight.x;
	}
	beep();
	beep();
	beep();
}

void main()
{
	set(deb_pan,SHOW);
	level_load(NULL);
	wait(3);
	
	ENTITY* theEnt;
	theEnt = ent_create(NULL,nullvector,NULL);

	runWhileEntity(&theEnt);
	while(key_t == OFF) wait(1);
	ent_remove(theEnt);
	theEnt = NULL;
}


Posted By: Quad

Re: Entity pointer passed to function - 10/15/10 11:33

use fuction as the action of the entity and use a while-me loop instead?
Posted By: Bunsen

Re: Entity pointer passed to function - 10/15/10 21:16

If you are using tasks within Lite-C (by calling the "wait" function) there are 2 solutions to exchange data:
Either you use global variables or you must implement a messaging system e.g. a queque.
The pointer of pointer method is not working here (although ok in other multitasking systems) because
in Lite-C on reentry the stack is not originally reconstructed.
Posted By: EvilSOB

Re: Entity pointer passed to function - 10/16/10 00:11

Code:
#define i_am_alive skill55   //any unused skill number will do

void runWhileEntity(ENTITY** inLight)
{
	while(inLight.i_am_alive == true)
	{
		*inLight.x += time_step;
		my_temp[0] = *inLight.x;
		wait(1);
	}
	beep();
	beep();
	beep();
}

void main()
{
	set(deb_pan,SHOW);
	level_load(NULL);
	wait(3);
	
	ENTITY* theEnt;
	theEnt = ent_create(NULL,nullvector,NULL);
	theEnt.i_am_alive = true;

	runWhileEntity(&theEnt);
	while(key_t == OFF) wait(1);
	theEnt.i_am_alive = false;
	wait(1);   //critical!
	ent_remove(theEnt);
}


Posted By: WretchedSid

Re: Entity pointer passed to function - 10/16/10 06:54

Originally Posted By: Bunsen
The pointer of pointer method is not working here (although ok in other multitasking systems) because
in Lite-C on reentry the stack is not originally reconstructed.

Wow, thanks for the info!
Posted By: Joey

Re: Entity pointer passed to function - 10/16/10 07:29

Originally Posted By: Bunsen
The pointer of pointer method is not working here (although ok in other multitasking systems) because in Lite-C on reentry the stack is not originally reconstructed.

honestly? why is that? and what has it to do with the stack? that would be extremely counter-intuitive if you could pass pointers to the heap and not to the stack. i thought that would be the whole point in the coroutine stuff.
so basically what you're saying is that *inLight is not guaranteed to be theEnt anymore?
Posted By: Saturnus

Re: Entity pointer passed to function - 10/16/10 13:38

There were some threads about this. Here is one of them:
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=248692
Posted By: Joey

Re: Entity pointer passed to function - 10/17/10 13:03

Weird how I have once wondered the same thing...
Posted By: Germanunkol

Re: Entity pointer passed to function - 10/19/10 13:53

Thanks for the ideas, but none of them will work. I've tried the pointer to a pointer thing before posting. Also, the workarounds don't really work. The skill one won't work cause we don't wanna use an extra skill (it's a plugin which shouldn't use more than one skill) and "use fuction as the action of the entity and use a while-me loop instead?" won't work either cause this is already a simplified version of the whole setup... it would work here, but not with what we want, I believe.
Posted By: EvilSOB

Re: Entity pointer passed to function - 10/19/10 14:11

Then how about this...
Code:
#define I_AM_ALIVE	(1<<30)   //unused entity flag bit

void runWhileEntity(ENTITY* inLight)
{
	while(is(inLight, I_AM_ALIVE))
	{
		inLight.x += time_step;
		my_temp[0] = inLight.x;
		wait(1);
	}
	beep();
	beep();
	beep();
}

void main()
{
	set(deb_pan,SHOW);
	level_load(NULL);
	wait(3);
	
	ENTITY* theEnt;
	theEnt = ent_create(NULL,nullvector,NULL);
	set(theEnt, I_AM_ALIVE);

	runWhileEntity(theEnt);
	while(key_t == OFF) wait(1);
	reset(inLight, I_AM_ALIVE);
	wait(1);   //still critical!
	ent_remove(theEnt);
}


Posted By: Germanunkol

Re: Entity pointer passed to function - 10/20/10 08:12

Thanks, that is a way of doing it. Though annoying, I guess it'll work for now...
Posted By: EvilSOB

Re: Entity pointer passed to function - 10/20/10 09:00

If you dont like the "mess" in the 'main' function, how about this...
Code:
#define I_AM_DEAD	(1<<30)   //unused entity flag bit

void runWhileEntity(ENTITY* inLight)
{
	while(!is(inLight, I_AM_DEAD))
	{
		inLight.x += time_step;
		my_temp[0] = inLight.x;
		wait(1);
	}
	ent_remove(inLight);
	beep();
	beep();
	beep();
}

void main()
{
	set(deb_pan,SHOW);
	level_load(NULL);
	wait(3);
	
	ENTITY* theEnt;
	theEnt = ent_create(NULL,nullvector,NULL);

	runWhileEntity(theEnt);
	while(key_t == OFF) wait(1);
	set(inLight, I_AM_DEAD);
}



Or if its the "new" flag I_AM_WHATEVER that you dont like,
you could utilise one of the now almost-unused C-script FLAG1->FLAG8 flags.
Posted By: pegamode

Re: Entity pointer passed to function - 10/20/10 09:47

What about passing the handle of the entity pointer into the function?

I'm not 100% sure, but wouldn't it be possible to use the my-pointer inside the function ( + proc_mode = PROC_GLOBAL; )?

Something like that:

Code:
void runWhileEntity(ENTITY* inLight)
{
proc_mode = PROC_GLOBAL;
my = inLight;
while (my) {
  ...
}
...
}



The my-pointer is restored correctly, isn't it?
Posted By: EvilSOB

Re: Entity pointer passed to function - 10/20/10 10:34

BTW, you say you are already using one skill in the plugin...
What sort of data does it contain?
Could you pass a "kill" command through that skill?

eg
Code:
#define plugin_skill   "whichever skill the plugin is already using"

void runWhileEntity(ENTITY* inLight)
{
	while(inLight.plugin_skill != -999)
	{
		inLight.x += time_step;
		my_temp[0] = inLight.x;
		wait(1);
	}
	beep();
	beep();
	beep();
}

void main()
{
	set(deb_pan,SHOW);
	level_load(NULL);
	wait(3);
	
	ENTITY* theEnt;
	theEnt = ent_create(NULL,nullvector,NULL);

	runWhileEntity(theEnt);
	while(key_t == OFF) wait(1);
	theEnt.plugin_skill = -999;
	wait(1);	//critical wait
	ent_remove(theEnt);
}


© 2024 lite-C Forums