removing large sprite entities all at once

Posted By: ncc1701d

removing large sprite entities all at once - 02/12/10 05:57

is their a way to create and/or remove large number of sprite entities without naming them one by one in code.
Say I want to create a forest of sprite trees. They are all the same sprite just repeated but in different locations. Is there way to say plant them in many locations without refering to each tree code line by code line?
Is their a way to remove these entities all at once using ent_remove but many all at once as opposed to each one by one.
follow?

I can potentially imagine using random and ent_create to make more than one tree even if I dont know how yet but then what to do to rem_ entity all at once as opposed to one by one.?
thanks
Posted By: KDuke

Re: removing large sprite entities all at once - 02/12/10 06:41

Well random and ent_create is the way to go. You simply use a loop with which you create the sprites.
If you use an array to store these created entities you can easily remove them with a loop of the same kind, using the array which stores the pointers to the sprite entities and removing them with ent_remove.
If you would like a dynamic number of these sprites you can use sys_malloc for dynamically creating an array with a given number of space to handle the sprites.
Posted By: EvilSOB

Re: removing large sprite entities all at once - 02/12/10 09:07

Another way would be to loop generating all the sprites.
This loop also gives each entity a simple action that watches one particular
global variable.
If this global variable equals a set number, the entities then self-destruct.

EG
Code:
var trees_on = 1;

action tree_alive()
{  while(trees_on==1)
   {   wait(1);   }
   ent_remove(me);
}

function create_trees()
{  ...
   for(i=0; i<150; i++)
   {  
      ent_create("tree.tga", vector(random(2000)-1000,random(2000)-1000,0), tree_alive);
   }
   ...
}


// then just set "trees_on == 0" for one frame and all the trees will remove themselves.


Posted By: Widi

Re: removing large sprite entities all at once - 02/12/10 09:57

I preferer the Solution from KDude. With the solution from EvilSOB you have a lot entities that use a action (are dynamic) and are in a while loop. That is bad for your fps.
Posted By: EvilSOB

Re: removing large sprite entities all at once - 02/12/10 13:59

Widi does have a point.
Mine will be a little bit fps-hungry if youve got a hell of a lot of trees.
But seeing as the tree-action is so small theres not going to be that fps-intensive.

BUT, if you are just using this process for a "remove trees at end of level"
type system, then mine is seriously in-efficient and KDukes is far more appropriate.
Posted By: muffel

Re: removing large sprite entities all at once - 02/12/10 14:18

"Entities created by ent_create are always dynamic." (from Manual)

I would use EvilSOB solution.
You can always remove all trees if you want.
And the point that KDukes method saves fps because of static Entitys is wrong because they are not static.
I guess that you can set them static in a action(reset(me,DYNAMIC);)

muffel
Posted By: Joey

Re: removing large sprite entities all at once - 02/12/10 15:04

i think he was talking about the action code which consumes execution time, not to the entity being dynamic or static.

i would use a mixture of both: save the entities into an array and assign an action which resets their dynamic flag. then you can delete them from within the array.

like that you could even do some culling tree algorithm if you don't use a plain array but a tree structure, saving even more performance.
Posted By: MrGuest

Re: removing large sprite entities all at once - 02/12/10 17:08

I'd just store all the trees handles in an array,

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

#define trees_max 50

var hnd_trees[3][trees_max];

void trees_remove(int group){
	
	ENTITY* ent;
	
	int i;
	for(i = 0; i < trees_max; i++){
		
		you = ptr_for_handle(hnd_trees[group][i]);
		if(you){
			
			ent_remove(you);
		}else{
			
			break;
		}
	}
}


void trees_create(VECTOR* offset, int group, int count){
	
	if(count > trees_max){
		printf("TOO MANY TREES, Increase #define on line 6");
		return;
	}
	
	int i;
	VECTOR pos;
	
	pos.z = 0;
	
	int i;
	for(i = 0; i <= count; i++){
		
		pos.x = offset.x + (random(500)-250); //-250 to 249.99
		pos.y = offset.y + (random(500)-250);
		
		hnd_trees[group][i] = handle(ent_create("tree.mdl", pos, NULL));
	}
	
	
}

void main(){
	
	wait(1);
	
	
	level_load(NULL);

	vec_set(camera.x, vector(0, 0, 5000));
	camera.tilt = -90;
	
	trees_create(vector(0, 0, 0), 0, 10);
	trees_create(vector(-1000, 1000, 0), 1, 10);
	trees_create(vector(1000, 1000, 0), 2, 10);
	
	random_seed(0);
	
	int i;
	while(1){
		
		i = random(3);
		trees_remove(i);
		trees_create(vector(random(2000)-1000, random(2000)-1000, 0), i, integer(random(25)));
		
		wait(-2);
	}
}



make sure your tree model exists
doesn't stop trees spawning in each other
doesn't choose more than 1 model

hope this helps
Posted By: Widi

Re: removing large sprite entities all at once - 02/13/10 19:40

@muffel: Yes, entities with ent_create are dynamic, but you can reset the DYNAMIC flag at the end of the action. That saves a lot of fps. wink
© 2024 lite-C Forums