Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/20/24 20:05
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, kzhao, alibaba), 627 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
I was wondering if there was a better way to do this #242644
12/24/08 20:56
12/24/08 20:56
Joined: Oct 2008
Posts: 218
Nashua NH
heinekenbottle Offline OP
Member
heinekenbottle  Offline OP
Member

Joined: Oct 2008
Posts: 218
Nashua NH
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:

Code:
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.

Last edited by heinekenbottle; 12/24/08 20:58.

I was once Anonymous_Alcoholic.

Code Breakpoint;
Re: I was wondering if there was a better way to do this [Re: heinekenbottle] #242691
12/25/08 06:33
12/25/08 06:33
Joined: Aug 2000
Posts: 1,140
Baunatal, Germany
Tobias Offline

Moderator
Tobias  Offline

Moderator

Joined: Aug 2000
Posts: 1,140
Baunatal, Germany
Yes, there is a better way I think, in fact two better ways. First, I would use only one function, copying and pasting 11 functions is awful especially when you want to change something afterwards.

function assign_units()
{
var unit_number;
if (key_1) unit_number = 1;
else if (key_2) unit_number = 2;
... etc.
and then use the unit_number var in your function to assign the group to.

and assign the same function to all 11 keys:

...
on_1 = assign_units;
on_2 = assign_units;
... etc.

Second, your code is very slow due to the wait()

you = ent_next(you); //find the next entity
wait(1); //avoid endless loops

but there is no endless loop, an endless loop would begin with while(1). So without the unnecessary wait your function will run much faster.

Re: I was wondering if there was a better way to do this [Re: Tobias] #243452
12/30/08 01:51
12/30/08 01:51
Joined: Jul 2008
Posts: 1,178
England
M
MrGuest Offline
Serious User
MrGuest  Offline
Serious User
M

Joined: Jul 2008
Posts: 1,178
England
hey,

personally i catch all key presses on 1 function then determine what to do with it from the game state, then the key,

Code:
#define game_state_loading 0
#define game_state_playing 1
int int_game_state = 0;

function fnc_keypress(key_scancode){
   switch(int_game_state){
      case game_state_playing:

         switch(key_scancode){
            case 2: //key 1
//...

void main(){
   on_anykey = fnc_keypress;
}

so as you're using keycode 2 to 12 you could just put

Code:
//...
         case 2: case 3: case 4: //etc...
         case 11: case 12:
            if(key_ctrl){
               fnc_set_group(key_scancode - 1);
            }else{
               fnc_select_group(key_scancode - 1);
            }
            return;

so when you're assigning the group or getting the group number you can use (obviously above the previous code)
Code:
function fnc_select_group(int grp_number){
   while(you){ //etc
      if(your.group = grp_number){ your.selected = true; }

}


hope this helps, (i've just written this so beware of elusive / stray brackets :P )


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

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