Legacy GM Need help with menu's.

D

DenHead

Guest
I'm trying my best to find easy ways to do an upgrade menu in my game. I have a sprite that looks like a battery and when you click it, it charges up indicating that you have upgraded that skill. The main issue I'm having is that I either have to make different objects for each individual part of the menu that I want to be interactive or I have to do it all in the draw event of my game controller. I have this code already in my game controller but I feel as though this isn't the best way to do it as it takes up a ton of space and I have to do it repeatedly for each individual upgrade button.


height = sprite_get_height(spr_upgradeBattery);
width = sprite_get_width(spr_upgradeBattery);

//Damage Upgrade
upgrade1x = room_width/3
upgrade1y = room_height/3

if (upgrade1 = 0) draw_sprite(spr_upgradeBattery,0,upgrade1x,upgrade1y)
if (upgrade1 = 1) draw_sprite(spr_upgradeBattery,1,upgrade1x,upgrade1y)
if (upgrade1 = 2) draw_sprite(spr_upgradeBattery,2,upgrade1x,upgrade1y)
if (upgrade1 = 3) draw_sprite(spr_upgradeBattery,3,upgrade1x,upgrade1y)
if (upgrade1 = 4) draw_sprite(spr_upgradeBattery,4,upgrade1x,upgrade1y)

if point_in_rectangle(mouse_x,mouse_y,upgrade1x-width/2,upgrade1y-height/2,upgrade1x+width/2,upgrade1y+height/2) && device_mouse_check_button_released(0,mb_left)
{
if upgrade1 < 4
{
upgrade1 += 1;
global.playerdmg += 1
}
else
{
audio_play_sound(snd_error,1,false)
}
}


Is there an easier way to make this work without all these unnecessary variables like upgrade1,upgrade2,upgrade3, etc.? How do you guys do it?
 

johnwo

Member
Just for improving the code you provided:

Use a for-loop.

This code should be ready to go:
Code:
var height = sprite_get_height(spr_upgradeBattery);
var width = sprite_get_width(spr_upgradeBattery);

//Damage Upgrade
var upgrade1x = room_width/3
var upgrade1y = room_height/3

for (var i=0;i<5;i++) {
    if upgrade1 == i then draw_sprite(spr_upgradeBattery,i,upgrade1x,upgrade1y); else break;
}

if point_in_rectangle(mouse_x,mouse_y,upgrade1x-width/2,upgrade1y-height/2,upgrade1x+width/2,upgrade1y+height/2) && device_mouse_check_button_released(0,mb_left)
{
    if upgrade1 < 4 {
        upgrade1 += 1;
        global.playerdmg += 1
    } else {
        audio_play_sound(snd_error,1,false)
    }
}
If you have any questions, don't hesitate to ask.

Cheers!
 
D

DenHead

Guest
Just for improving the code you provided:

Use a for-loop.

This code should be ready to go:
Code:
var height = sprite_get_height(spr_upgradeBattery);
var width = sprite_get_width(spr_upgradeBattery);

//Damage Upgrade
var upgrade1x = room_width/3
var upgrade1y = room_height/3

for (var i=0;i<5;i++) {
    if upgrade1 == i then draw_sprite(spr_upgradeBattery,i,upgrade1x,upgrade1y); else break;
}

if point_in_rectangle(mouse_x,mouse_y,upgrade1x-width/2,upgrade1y-height/2,upgrade1x+width/2,upgrade1y+height/2) && device_mouse_check_button_released(0,mb_left)
{
    if upgrade1 < 4 {
        upgrade1 += 1;
        global.playerdmg += 1
    } else {
        audio_play_sound(snd_error,1,false)
    }
}
If you have any questions, don't hesitate to ask.

Cheers!
Thanks, I'm fairly new to coding in general but I had seen that sort of thing before but couldn't figure out how to use it.
 

Bukmand

Member
You don't need a lot of objects, just use one object.
- Steps
Create one object, and assign all the variables you need.
you can do it using for loop to create multiple objects from just one object, you don't need to create object1, object2

try this
for(i=0; i<4; i++) {
iD=instance_create(x, y, obObject)
iD.Variable=i;
//You can play here with variables as you like. or use arrays.
}

and here is a tip to shorten your code
draw_sprite(spr_upgradeBattery, upgrade1, upgrade1x, upgrade1y)
instead of image_index use your variables since they are same.
 
Top