I have a function that would work for an RTS game. What it does is if ctrl and a number is pressed, the selected units are assigned that number and if that number is pressed, but not control, the units of that group are selected.
The function works, but there are two issues that make me wonder if there is an easier way to do this.
First, it uses "function on_#_event", so there are 11 functions to copy and paste (10 groups and key_minusc to ungroup a group).
Second, it uses ent_next, which searches for ALL entities. It is fine now, but I'm worried that if there were a lot of entities (particle effects, rockets, missiles, hordes of troops, trees, workers, buildings, etc, etc) as is typical with an rts that this might be too slow. It works now, but I've only got 9-12 entities in my test.
The code is as follows:
function on_1_event()
{
if(!key_ctrl) //if control is not pressed, select the group
{
you = ent_next(NULL);//find the first entity
while(you) //while you is not null
{
if(you.group == 1) you.selected = 1;//if the entity's group is one, select it
else you.selected = 0; //otherwise unselect it
you = ent_next(you); //find the next entity
wait(1); //avoid endless loops
}
}
else //if control is pressed, assign the group
{
you = ent_next(NULL); //find the first entity
while(you) //while you is not null
{
if((you.group == 1) && (you.selected != 1)) you.group = ungrouped; //ungroup the previous group 1, if they aren't selected
if(you.selected == 1) you.group = 1; //group selected units
you = ent_next(you); //find the next entity
wait(1); //avoid endless loops
}
}
}
//and copy/paste, change numbers as needed
As I said, it works the same way as it does in Age of Empires, or most other RTS games, I'm just wondering if there is a faster way to do this.