How to cycle through entities without C_scan or c_trace

Posted By: Anonymous

How to cycle through entities without C_scan or c_trace - 03/12/14 13:53

I have a level where I ent_create balls at run time depending on user input. Because I would like to ent_remove or teleport any ball at anytime depending on user input, I need tobbe able to loop through all the balls. Please, how do I do that?
Posted By: 3dgs_snake

Re: How to cycle through entities without C_scan or c_trace - 03/12/14 13:55

Hi,

To loop through all entities, look for ent_next in the manual.
Posted By: Superku

Re: How to cycle through entities without C_scan or c_trace - 03/12/14 13:59

As 3dgs_snake already mentioned a simple way would be to use ent_next. You have to make a little adjustment though when you want to remove the entities:

Code:
you = ent_next(NULL);
while(you)
{
	ENTITY* you2 = you;
	you = ent_next(you);
	if(you2._type == type_ball) ptr_remove(you2);
}


A more sophisticated approach would be to create an array (or better: a list) yourself in which you save all ent_created balls.
Posted By: Anonymous

Re: How to cycle through entities without C_scan or c_trace - 03/14/14 20:44

Thanks guys! I already thought about creating an array but I see two problems with that. Correct me if I'm wrong.
1. It would have to be an array of pointers, not an array of actual entities
2. If I delete a ball, the slot it took in the array will still be there. So when I create another ball later, the total number of balls may not have changed but I won't be able to access ball 6 by checking index 4 of the array, since index 4 was originally for Ball 5 which got deleted.
Posted By: Superku

Re: How to cycle through entities without C_scan or c_trace - 03/14/14 21:28

ENTITY* ent_array[100];
gives you an array of 100 entity pointers.

In most cases I just use arrays as well and no lists.
When I remove some entity which is not the last one in the array I simply take the last one and put it at that spot instead (which solves your problem 2).
Posted By: Anonymous

Re: How to cycle through entities without C_scan or c_trace - 04/10/14 15:49

So I'm using the code below and it's deleting ALL the molecules at once.

Code:
function RemoveMolecules()
{
	while(NoOfMolecules != (n + 1))
	{
		you = ent_next(NULL);
		while(you)
		{
			ENTITY* PointAndKill = you;
			you = ent_next(you);
			
			if(PointAndKill.skill51 == 3619)
			{								
				NoOfMolecules = NoOfMolecules - 1;
				ptr_remove(PointAndKill);
			}
			wait(1);
		}
	}
}



I'm not sure what I'm doing wrong. The entire application can be found at https://www.dropbox.com/sh/85xl9xl2fv6ds9k/3-RS95REnK. I have poor internet reception right now so I don't think the dropbox files will be up to date, but if you copy and paste the function above into the code and assign it to a keyboard shortcut, you can recreate the problem. Maybe someone can find a solution before I do. Thanks again guys. P.S: My project deadline is 2pm tomorrow (Friday, April 11)
Posted By: Superku

Re: How to cycle through entities without C_scan or c_trace - 04/10/14 19:40

Give this example a try, it should help you in solving your problem (btw. why is there a wait instruction in your removal function and do you only call the latter once?):

Code:
void main()
{
	fps_max = 60;
	level_load(NULL);
	var i;
	for(i = 0; i < 100; i++)
	{
		you = ent_create(CUBE_MDL,vector(50+random(200),random(100)-50,random(100)-50),NULL);
		your.skill1 = i%2;
	}
	wait(-2);
	you = ent_next(NULL);
	while(you)
	{
		ENTITY* you2 = you;
		you = ent_next(you);
		if(!you2.skill1) ptr_remove(you2);
	}
	wait(-1);
	you = ent_next(NULL);
	while(you)
	{
		ENTITY* you2 = you;
		you = ent_next(you);
		ptr_remove(you2);
	}
	wait(-1);
	for(i = 0; i < 100; i++)
	{
		you = ent_create(SPHERE_MDL,vector(50+random(200),random(100)-50,random(100)-50),NULL);
		you = ent_create(CUBE_MDL,vector(50+random(200),random(100)-50,random(100)-50),NULL);
		your.skill1 = 123; // only remove cubes
	}
	wait(-2);
	you = ent_next(NULL);
	while(you && i > 50) // i == 100 at start
	{
		ENTITY* you2 = you;
		you = ent_next(you);
		if(you2.skill1 == 123) // remove half of the cubes
		{
			ptr_remove(you2);
			i--;
		}
	}
}

Posted By: Anonymous

Re: How to cycle through entities without C_scan or c_trace - 04/10/14 22:00

I call the removal function whenever the user presses a certain key. Calling it once seems to delete all the molecules (unless I called it multiple times unknowingly). I put the wait(1) instruction because otherwise, the entire app freezes the moment I call the removal function. I'll try out your code now and report back.
Posted By: Anonymous

Re: How to cycle through entities without C_scan or c_trace - 04/10/14 23:23

Thanks, Superku! Studying and tweaking your code helped me solve my problem. On creation, I set skill51 = NoOfMolecules for each molecule. Afterwards, this code did the trick.
Code:
function RemoveMolecules()
{
	NoOfMolecules = NoOfMolecules - 1;
	NoOfMolecules = clamp(NoOfMolecules, 2, 31);
	you = ent_next(NULL);
	while(you)
	{
		ENTITY* PointAndKill = you;
		you = ent_next(you);
		if(PointAndKill.skill51 > NoOfMolecules || PointAndKill.skill51 == NoOfMolecules)
		{								
			ptr_remove(PointAndKill);
		}
		wait(1);		
	}
}



Thank you! So how do I paste code the way you've been doing, instead of pasting as text? For your amusement, "Point and Kill" is a Nigerian slang used to refer to the way catfish is bought. You look at a bowl filled with live catfish (and almost no water) and point at the specific ones you want to buy. They are slaughtered and packed up for you on the spot laugh Not that I've ever bought them personally, but its catchier for me than "ENTITY* you2"
Posted By: Anonymous

Re: How to cycle through entities without C_scan or c_trace - 04/11/14 01:11

So this is kinda related. The code below is self explanatory. Somehow, it always fails to affect one of the molecules. If there are 10 molecules, only 9 will be affected. 1 is always left out. Any idea why?

Code:
function DecreaseVolumeInEngine()
{
	VolumeChanging = 1;
	
	/*Cycle through molecules and move them all to spawn point before deleting the container.
	Otherwise, the container will shrink past the location of some molecules, leaving them
	outside*/
	var teleported = 0;
	you = ent_next(NULL);
	while(NoOfMolecules > teleported)
	{
		ENTITY* NextMolecule = you;
		you = ent_next(you);
		if(NextMolecule.skill51) //if it has a skill51, meaning its a molecule
		{
			NextMolecule.x = (SpawnPoint.x + (2000*V)) + random(100);
			NextMolecule.y = (SpawnPoint.y + (2000*V)) + random(100);
			NextMolecule.z = (SpawnPoint.z + (2000*V)) + random(100);
		}
		teleported+=1;
		wait(1);
	}
	
	ent_remove(Container);
	Container = ent_create("HollowCube.WMB", vector(0, 0, 0), ResizeTheCube);
	wait(-1);
	VolumeChanging = 0;
	wait(1);	
	
	//Center the camera behind the cube. New camera location is dependent on V (Volume of Cube)
//	camera.x -= V * 13000;
//	camera.y += V * 5000;
//	camera.z += V * 5000;
//	wait(1);
}


© 2024 lite-C Forums