how to change entity pointer within a function

Posted By: Zapan@work

how to change entity pointer within a function - 06/18/10 17:43

I want to create a small list of Entites. For this I want to connect each pointer with its neighbours (prev, next), a simple linked list... The problem is that I cant change the given first/last pointer so it becomes valid outside the function scope:

#include <acknex.h>
#include <default.c>

ENTITY* pEmeter_first;
ENTITY* pEmeter_last;

#define ent_nextListObj skill97

//Add an object to the list
void ent_listAdd(ENTITY* p, ENTITY** first, ENTITY** last)
{
if (first == NULL) {
&first = &p;
&last = &p;
return;
}

last.ent_nextListObj = &p;
last = &p;
}

void main()
{
level_load(NULL);

wait(2);

pEmeter_first = NULL;
pEmeter_last = NULL;

ENTITY* p = ent_create(CUBE_MDL,nullvector,NULL);

ent_listAdd(p, &pEmeter_first, &pEmeter_last);

if (p == pEmeter_first) {
error("JUP!");
} else {
if (pEmeter_first == NULL) {
error("URGHS!");
} else {
error("???");
}
}
}


I want to see a "JUP" on my screen, so maybe you can help me? laugh

I've also uploaded the testfile here:
http://user.crew51.com/weinhold/main.c
Posted By: Lukas

Re: how to change entity pointer within a function - 06/18/10 18:06

This gives you a JUP:

Code:
#include <acknex.h>
#include <default.c>

ENTITY* pEmeter_first;
ENTITY* pEmeter_last;

#define ent_nextListObj skill97

//Add an object to the list
void ent_listAdd(ENTITY* p, ENTITY** first, ENTITY** last)
{
	if (*first == NULL) {
		*first = p;		
		*last = p;
		return;
	} 
	
	last.ent_nextListObj = p;
	last = p;	
}

void main() 
{
	level_load(NULL);
	
	wait(2);
	
	pEmeter_first = NULL;
	pEmeter_last = NULL;
	
	ENTITY* p = ent_create(CUBE_MDL,nullvector,NULL);	
	
	ent_listAdd(p, &pEmeter_first, &pEmeter_last);

	if (p == pEmeter_first) {
		error("JUP!");
	} else {	
		if (pEmeter_first == NULL) {
			error("URGHS!");
		} else {
			error("???");
		}
	}
}



&object returns a pointer to "object", *object returns the object "object" points to.

But are you aware that p.ent_nextListObj points to the entity itself if it's the first entity in the list?
Posted By: Zapan@work

Re: how to change entity pointer within a function - 06/21/10 12:39

Thank you, everything is working fine now.. laugh
© 2024 lite-C Forums