• 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!

Legacy GM drawing a menu for each unit..

T

theGRIM

Guest
I'm making a tower defense. I'm trying to make it so that when you click on a tower, a menu is created, and when you click on another tower, the first menu is deleted and a new one is created on the tower that is clicked on. I'm trying to do it in the parent object for the towers. Here is my code:

object "objtowerparent" event "mouse global left pressed"
Code:
with (objtowermenu) { instance_destroy(); }
if canselect == 1 then
{
    instance_create(x,y,objtowermenu);
}
"canselect" is if the mouse cursor is hovering above the tower or not. It changes with the [mouse enter] and [mouse leave] events.
It works fine when you just have 1 tower created, but when i create more towers, the menu only works for the last one I've created. Any ideas for why this is happening? Is there a better way to do this? Thanks in advance.
 
D

DevNorway

Guest
How do you make it so that the objtowermenu is referring to each tower?
 
T

theGRIM

Guest
objtowermenu doesn't refer to anything yet. It's just an empty object with a sprite right now
 
F

frumple

Guest
I would guess the problem lies in how you handle the variable canselect. Elsewise i can't think why it would work for one instance as opposed to all or none. What is the Mouse Enter & Leave code?
 
T

theGRIM

Guest
Sure
objtowerparent:
[create]:
Code:
event_inherited ();
canselect = 0;
[Mouse Enter]:
Code:
canselect = 1;
[Mouse Leave]
Code:
canselect = 0;
[Global Mouse Left Pressed]
Code:
with (objtowermenu) { instance_destroy(); }
if canselect == 1 then
{
    instance_create(x,y,objtowermenu);
}
The towers that are created are called objtower1. It's parent is set to objtowerparent.​
 
Last edited by a moderator:
F

frumple

Guest
Well, i'm not 100% sure, but it *may* be that when a mouse enters and leaves ALL children react (in the order they were created most likely) and all set canselect to 0 or 1. So later when you do:
Code:
with (objtowermenu) { instance_destroy(); }
if canselect == 1 then
{
   instance_create(x,y,objtowermenu);
}
Guess what? You will have created menus for each one, and destroyed the menu (as per your instruction) for all but the last one.

Run in debug mode and look at the instances. I'm pretty sure canselect is not right for them. Check their variables.

You could avoid this by using only Mouse Enter and doing:
Code:
with ( objtowerparent )
	{
	canselect = 0;
	}
canselect = 1;
That should set the variable correctly for all children. 0 for all, then 1 for the one that the mouse entered.

I could be way off though. Just sayin.
 
Top