In RTS especially you might want to be able to select a number of instances. Get example here:
http://gmtutorials.com/files/selectex.gm6
We need two objects; the controller and the object that should be selectable. Actually all we need is the selectable object itself, but for visual drawings we need another since it would be a stupid thing to let several instances draw the same lines.
<div style="clear: both;">The non-selectable object, controller as I will call it, needs to initialize three variables:
global.start_x = 0;
global.start_y = 0;
draw = false;</div>
This is for the square which the object is meant to draw, but it will also be used be the selectable objects and that’s why we make the position variables global. In draw event goes this:
draw_set_color(c_red);
if (draw==true) {
draw_rectangle(global.start_x,global.start_y,mouse_x,mouse_y,true);
}
The rectangle is a visualization of the area that will be selected. It starts where the mouse was first pressed and ends where it was released. It disappears immediately thereafter, by the way. Draw is the variable that fixes that, because when the left button is released it is set to false. In the global left pressed we got this code:
global.start_x = mouse_x;
global.start_y = mouse_y;
draw = true;
That’s actually everything that’s in the controller. The rest is done from the selectable object. It got a sprite with two sub images, one when the object is selected and one when it is unselected. So I simply set it in the step event:
if (selected == true) {
image_single = 1;
}
else {
image_single = 0;
}
The variable selected is local as you can see, which enables me to treat each instance as an individual. I initialize it, and image_single in create event:
image_single = 0;
selected = false;
And now the only event yet not described is the far most important: global left released. We had that event in the controller as well, but in this object is done the entire selection system; it is the best solution that each individual should figure out itself whether or not it is selected.
if ((x<mouse_x) && (y<mouse_y) && (x>global.start_x)
&& (y>global.start_y)) {
selected = true;
}
else {
selected = false;
}
We are now using the global positions that was set as the mouse button was pressed, and compare it to the current mouse position. This is exactly what we do when we draw, so if you really want to understand what area we create you should check it out visually in the example.
The only problem with the code is that is assumes certain situations if value is larger than and that value is small than; the truth is that there are more possibilities. This code alone would work when the user marked a square from the left-top corner and down-right, but not if the user did the reverse. To cover up all of the four possibilities I duplicated and altered:
if ((x<mouse_x) && (y<mouse_y) && (x>global.start_x)
&& (y>global.start_y)) {
selected = true;
}
else {
selected = false;
}
if (selected == false) {
if ((x>mouse_x) && (y>mouse_y) && (x<global.start_x)
&& (y<global.start_y)) {
selected = true;
}
else {
selected = false;
}
}
if (selected == false) {
if ((x>mouse_x) && (y<mouse_y) && (x<global.start_x)
&& (y>global.start_y)) {
selected = true;
}
else {
selected = false;
}
}
if (selected == false) {
if ((x<mouse_x) && (y>mouse_y) && (x>global.start_x)
&& (y<global.start_y)) {
selected = true;
}
else {
selected = false;
}
}
As you can see I added one line: if (selected == false), I had to do this or the condition else would be applied every time and selected would undoubtedly be set to false.
To make the object perform only when selected make them perform only to the condition that selected is set to true. If selected==true { ... I hope you understand.
Go ahead and check out the example, use it in anyway you wish and if you got any questions please ask them by commenting.
http://gmtutorials.com/files/selectex.gm6
.
Users logged in:
Comments
Loading comments...