Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
4 registered members (AndrewAMD, dr_panther, 2 invisible), 1,056 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
How to cycle through entities without C_scan or c_trace #438356
03/12/14 13:53
03/12/14 13:53

T
tolu619
Unregistered
tolu619
Unregistered
T



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?

Re: How to cycle through entities without C_scan or c_trace [Re: ] #438357
03/12/14 13:55
03/12/14 13:55
Joined: Feb 2010
Posts: 320
TANA/Madagascar
3dgs_snake Offline
Senior Member
3dgs_snake  Offline
Senior Member

Joined: Feb 2010
Posts: 320
TANA/Madagascar
Hi,

To loop through all entities, look for ent_next in the manual.

Re: How to cycle through entities without C_scan or c_trace [Re: 3dgs_snake] #438358
03/12/14 13:59
03/12/14 13:59
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
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.


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: How to cycle through entities without C_scan or c_trace [Re: Superku] #438460
03/14/14 20:44
03/14/14 20:44

T
tolu619
Unregistered
tolu619
Unregistered
T



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.

Re: How to cycle through entities without C_scan or c_trace [Re: ] #438461
03/14/14 21:28
03/14/14 21:28
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
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).


"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: How to cycle through entities without C_scan or c_trace [Re: Superku] #439822
04/10/14 15:49
04/10/14 15:49

T
tolu619
Unregistered
tolu619
Unregistered
T



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)

Last edited by rayp; 04/12/14 00:21. Reason: added code tags ... press edit to see how its done
Re: How to cycle through entities without C_scan or c_trace [Re: ] #439846
04/10/14 19:40
04/10/14 19:40
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
Superku Offline
Senior Expert
Superku  Offline
Senior Expert

Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
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--;
		}
	}
}



"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual

Check out my new game: Pogostuck: Rage With Your Friends
Re: How to cycle through entities without C_scan or c_trace [Re: Superku] #439853
04/10/14 22:00
04/10/14 22:00

T
tolu619
Unregistered
tolu619
Unregistered
T



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.

Re: How to cycle through entities without C_scan or c_trace [Re: ] #439856
04/10/14 23:23
04/10/14 23:23

T
tolu619
Unregistered
tolu619
Unregistered
T



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"

Last edited by rayp; 04/12/14 00:19. Reason: added code tags
Re: How to cycle through entities without C_scan or c_trace [Re: ] #439861
04/11/14 01:11
04/11/14 01:11

T
tolu619
Unregistered
tolu619
Unregistered
T



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);
}



Last edited by rayp; 04/12/14 00:20. Reason: added code tags ... press edit to see how its done

Gamestudio download | chip programmers | 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