Making a popup window

FiftyFive

Member
I do not have much experience in Gamemaker, I am trying to make a new game (tower defense). When I click on an object in the game, I want a menu to open and I want to select a tower from there, which code can I use for this
 

Attachments

otterZ

Member
I am not that experienced in GML yet, but I can suggest:

In your object in the game create a Left Pressed event which creates an instance of your menu containing something like:

GML:
instance_create_depth (room_width/2,160,1,obj_menu);
In obj_menu you could have a nice sprite acting as the red rectangle mock up and position it with x and y coordinates in the Create event. Then over the top of this you could have the tower choice to select in the form of buttons.

The button sprites can be drawn on top of the red rectangle with instance_create_depth again, as in something like:

GML:
instance_create_depth (room_width/2 + 40,160,1,obj_first_tower);
For the tower 'buttons' you could make these really nice by having them change shade/colour whilst hovering your mouse over them and being in a highlighted state once you have clicked on them, with something like this in obj_first_tower, imagining that you created 3 images in one sprite, the fist image 0 as normal, image 1 as being 50 percent lighter in shading and image 3 being let's say a highlight yellow colour.

GML:
//Left Pressed

global.tower = towerOfSecrets; //selects your tower choice and stores it in a global variable
image_index = 3; //highlights the button

//Mouse Enter

image_index = 2; //50 percent lighter in shade

//Mouse Leave

if (image_index != 3)
{
image_index = 1; //sets back the tower 'button' to normal once the mouse leaves
}
In obj_menu you could then just repeat the process for as many tower buttons as you like, just adjusting the x, y coordinates as to where you want them, with each one assigning your global.tower variable with a new value of whatever you like.

The only other thing that would be left to do is code into each button that if selected, all the other buttons' image indexes must be set back to 1.

For your object in the game, if the player doesn't wish to select anything and wants to get rid of the pop up menu you could use a Right Pressed event containing something like:

GML:
instance_destroy(obj_menu);
You could then just destroy the buttons in obj_menu's Clean Up event.

Anyway, hope this helps in some way and wishing you the best of success in your project.
 
Last edited:

FoxyOfJungle

Kazan Games
I have a better solution for you!

You will use arrays.


When clicking on the desired object, you create a new object, I will name it obj_towers_menu:
instance_create_depth(0,0,1,obj_towers_menu);


Create Event:

GML:
// Here you create how much towers you want
array[0] = "Tower\n1";
array[1] = "Tower\n2";
array[2] = "Tower\n3";
array[3] = "Tower\n4";
Tip: \n = New line.


Draw GUI Event:

GML:
// Buttons position
var _xx = display_get_gui_width()/2;
var _yy = 160;
var _spacing = 70;

// Loop the buttons and draw it
for (var i = 0; i < array_length(array); i+=1)
{
    // draw back button
    draw_rectangle_color(_xx+i*_spacing, _yy, _xx+i*_spacing + 50, _yy + 50, 0,0,0,0,false);

    // draw tower text
    draw_set_halign(fa_center);
    draw_set_valign(fa_middle);
    draw_text(25+_xx+i*_spacing, 25+_yy ,array[i]);
    draw_set_halign(fa_left);
    draw_set_valign(fa_top);

    // draw mouse select color
    if point_in_rectangle(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), _xx+i*_spacing, _yy, _xx+i*_spacing + 50, _yy + 50)
    {
        // white outline button rectangle
        draw_rectangle_color(_xx+i*_spacing, _yy, _xx+i*_spacing + 50, _yy + 50, c_white,c_white,c_white,c_white, true);
    
    
        // mouse press action
        if mouse_check_button_pressed(mb_left)
        {
            switch (array[i])
            {
                case array[0]:
                    show_message("Tower 1 Created.");
                    break;
            
                case array[1]:
                    show_message("Tower 2 Created.");
                    break;
                
                case array[2]:
                    show_message("Tower 3 Created.");
                    break;
                
                case array[3]:
                    show_message("Tower 4 Created.");
                    break;
            }
        }
    }
}
You will create the towers where show_message ("Tower X Created.") Is;


Result:



I hope I helped! :)
 
Top