GMS 2 [SOLVED] Help with RTS style game - tracking "selected units"

M

mtski

Guest
Hi everyone,

I am trying to build a quick prototype game for learning purposes without the need for tutorials to encourage free based thinking, however, I have been stuck the last few days trying to find a solution to checking multiple units being selected and following mouse input commands.

Basically, I have a parent object named (obj_unit) and the only child at the moment named (obj_boxy). I am attempting to have a single (obj_mouse) object handle all the code to issue instructions to individual instances of the child objects by using the parent object for reference. ie "with(obj_unit)"

I have been able to detect collisions using the collision_rectangle function to turn on a variable named "selected" within the individual instances, but I am unable to get them to move independently of one another. All I have been able to accomplish with this is to get all instances to move when one particular instance was selected(I think the instance with the lowest number id_number 100001).

I have looked into this and have seen posts about using arrays or ds_lists to keep track of selected instances, but have no idea how to implement this. Which option would be most efficient and how can I separate commanding the id of instances rather than all instances of the parent?

Code is provided below within the "Draw Event" of obj_mouse. Thank you for reading my post and for any guidance.
------------------------------------------------------------------------------------------------------------------------

Code:
/// @description Insert description here
// You can write your code in this editor

var mx = mouse_x
var my = mouse_y


var c = c_black

if mouse_check_button_pressed(mb_left) {
    mxt = mx;
    myt = my;
  
}

if (mouse_check_button(mb_left) and mouseCommand = 0) {
    draw_rectangle_color(mxt,myt,mx,my,c,c,c,c,1)
}   


if (mouse_check_button_released(mb_left) and mouseCommand = 0) {
    var sa_mxt = mxt
    var sa_myt = myt
    var sa_mx = mx
    var sa_my = my
  
    var instcount = instance_number(obj_unit)
    show_debug_message("instcount = " + string(instcount))
  
  
    with (obj_unit) {
        var inst = collision_rectangle(sa_mxt,sa_myt,sa_mx,sa_my,id,0,0)
      
        if (inst = id) {
          
          
            selected = 1
        }
        else selected = 0
          
    }
      
    if (inst != noone) mouseCommand = 1
}

NVM! I was able to resolve using arrays to assign instances within.
 
Last edited by a moderator:
T

Taddio

Guest
NVM! I was able to resolve using arrays to assign instances within.
Hey, I think your code with rectangle collisions would work if you replaced collision_rectangle() with collision_rectangle_list().
The latter will give you a list of ALL instances within the rectangle, wheras collision_rectangle will stop checking after it found a single collision.
In this case, ds_lists functions could be useful to have available (sorting for the GUI, to show icons of selected units and their health, like Starcraft, for example), while arrays, while fast and easy to access, have more limited built-in functions
 
M

mtski

Guest
Thanks for the feedback.

I forced myself to get a handle on arrays and was able to have each instance of "obj_unit" store their I'd into an array. I will consider looking into your suggestion as well.

My next challenge has been a custom move command and collision handling between move commanded instances. I hear it's a bad idea to use the built in speed variable and iv been testing with motion_add and standard place_merting. I'd like to redesign movement differently though. Any suggestions on this?

Unfortunately I have not played the starcraft games. My favorite rts was the original tiberium sun. I want to try creating a game that is a cross of civ5 or 6 and that lol. But I also want it to feel like every unit created can be meaningful and upgraded with an experience type system. But I dream lol.
 
T

Taddio

Guest
First thing, how can you even thibk about creatting a RTS if you never played Starcraft? This game defined RTS mechanics that are still in use today. If only for the research, do yourself a favor, really!

You mentionned CIV. This is my favorite franchise of all time, Sid is my game dev hero, and I know pretty much everythibg there is to know about it.
This game is HEAVILY based on accessing data within data, within data, within...well you get the picture.
And you need a good system to implement and tweak unit stats and stuff without too much hassle. Arrays are good, and in this case they're even better when combined with enums. And once youget the hang of arrays, you'll be able to use ds_ as well, as I said, they are 95% the same, only the built in functions are different. When you'll be comfortable with both, you'll often decide which one to pick based on what functions you'll need.
Good to know you got it working. RTS are hard to get right. You need some sort of flocking for units, pathfinding, a LOT of data for units, buildings, terrain, etc, a rather smooth game fps-wise, minimap (probably), keyboard hotkeys, etc... It's not impossible, but there's a lot of moving parts.
Good luck with it!
 
M

mtski

Guest
I will look into starcraft. Iv seen it mentioned more than once lol.

My strategy this far is to take micro steps into each part of the development of such a game. Who knows if I'll actually finish it or make it playable lol. But the sense of accomplishing something in gamemaker is better than you get from most games these days imo lol.

I plan on getting into pathfinding as well. But that will probably be pushed off until I can get something of a prototype working. At this time I'm not actually creating a game to create a game. It's all for the learning experience. Eventually though, that's my goal.
 
Top