• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows (RTS HELP NEEDED) Counting Selected Units

P

Pixels_Everywhere

Guest
Im trying to count how many units I am selecting. I have been trying for about 3 hours and I haven't managed to get it to work. I have tried mostly arrays and for loops.
 

Smiechu

Member
First of all... is it you first game in GM???

I must say that RTS is quite hard topic for a beginner. I've also started with RTS, but I had already some experience in programming...

First of all you need to create a unit instance listing engine... you will need it for many other functions later i.e. minimap.
I hope that you have made your units properly, and all units (types, friendly, enemies, inactive etc) are instance of the same object, or you have made a nice parent/children system... if not... it will make all the things only harder for you.

This code should be in your "game" object, it creates a list of all units on the battlefield:

Code:
// Ship list
    global.ship_list_i = instance_number(obj_ship);  // this is the counter of unit instances
    for (i = 0; i < global.ship_list_i; i += 1)
        {
        global.ship_list[i] = instance_find(obj_ship,i);   // here id of the instance is saved in an array
        }
Below is an example code for unit object, which creates 3 lists of units in range:

Code:
var i = 0, j = 0, k = 0, l = 0;
for (i = 0; i < global.ship_list_i; i += 1)
    {
    if collision_circle(x,y,range,(global.ship_list[i]),false,true) // checks if the unit is in collision
        {
        if (global.ship_list[i]).player = player     // checking if the unit belongs to the same player / is it friendly or not...
            {
            in_range_friend[j] = global.ship_list[i]; // save the id of friendly unit into friendly units array
            j += 1;  // increase friendly units counter
            continue;
            }
        else
            {
            if (global.ship_list[i]).type = 1
            or (global.ship_list[i]).type = 3     // checking if enemy unit is a fighting nit or not... variable "type" defines the type of unit the instance is "acting"
                {
                in_range_fighter[k] = global.ship_list[i]; // save the id of enemy fighter unit unit into array
                k += 1;
                continue;
                }
            else
                {
                in_range_nfighter[l] = global.ship_list[i]; // save the id of enemy non fighter unit unit into array
                l += 1;
                continue;
                }
            }
        }
    }
in_range_friend_i = j;  // saving counter var - this is a number of in range friendly units...
in_range_fighter_i = k;
in_range_nfighter_i = l;
So for your need you could basically do only:

Code:
var i, j;
for (i = 0; i < global.ship_list_i; i += 1)
    {
    if (global.ship_list[i]).selected  
            {
            j += 1;
            }
     }
selected_i = j;
It should be in your "player" object...

This is only an example...
It is also possible that the instance saves it's id to a global.list... but then you need to remember to always reset the list in step begin event, save it in step event, and read it in step end event... or create system preventing from operating on the list before all the instances have saved their id into it...

You also need to be aware that this type of code inside of unit object is very heavy for the performance, so it should not be performed every step, but only when needed...
 
Last edited:
P

Pixels_Everywhere

Guest
Hi, Smiechu. thank you for replying! No, this is not my first time using GM. I have been using it for around 3 years. And dont worry, im not just copying and pasting code
 
P

Pixels_Everywhere

Guest
Ok! If you'll have any problems with implementing my code let me know...
Ok, im having a problem with the j var in your last little block of code. Even tho i am assigning it as a var, its still seems to think it doesnt exist. -.-
 
P

Pixels_Everywhere

Guest
its all good. My brain is pretty fried, sorry i didnt catch that myself. I have been working at this for a couple of hours now. After some thought, i dont think the scale of my game is going to be large enough to require me to this yet. Im pretty fed up with it right now, lol. I might try again tomorrow
 
Top