Legacy GM RESOLVED Instance not creating

V

VansiusProductions

Guest
Hi guys

When you click on each one of them, it will set a value to the variable groupHover.
However when I click on the air group (the top most one, groupHover = 1) , earth group(2nd one) SHOWS UP! It doesn't make sense!

This is my code under the mouse enter event:
Code:
if (place_meeting(mouse_x,mouse_y,obj_group_air)) {global.groupHover = 1}
if (place_meeting(mouse_x,mouse_y,obj_group_earth)) {global.groupHover = 2}
if (place_meeting(mouse_x,mouse_y,obj_group_fire)) {global.groupHover = 3}
if (place_meeting(mouse_x,mouse_y,obj_group_water)) {global.groupHover = 4}
it should set groupHover to 1 when i click air group. I have the game to display a debug message of groupHover's value and it's always 2 when I touch air group. Any idea what's wrong?
 
T

TheMatrixHasMe

Guest
@VansiusProductions Sometimes when something doesn't make sense it's an error where we don't think. Like the create event of the object or something wasn't parented correctly.

Secondly, I notice your hover code but where is your click code? Can you show us your click code?

Edit: nevermind this is probably in the left_mouse_pressed event, duh!
Edit2: Where is your code that creates the earth that is supposed to be air? Can you show us that?
 
V

VansiusProductions

Guest
@VansiusProductions Sometimes when something doesn't make sense it's an error where we don't think. Like the create event of the object or something wasn't parented correctly.

Secondly, I notice your hover code but where is your click code? Can you show us your click code?

Edit: nevermind this is probably in the left_mouse_pressed event, duh!
Edit2: Where is your code that creates the earth that is supposed to be air? Can you show us that?
Sure, it is in the left mouse released event:
Code:
//obj_group_air
if (global.groupHover == 1) {
    if (global.groupSelectedA == 0) {
        global.groupSelectedA = 1;
        instance_create(352, 62, obj_group_air);
        scr_displayElements()
    }
    else {
        if (global.groupSelectedB == 0) {
            global.groupSelectedB = 1;
            instance_create(352, 222, obj_group_air);
            scr_displayElements()
        }
    }
}
//obj_group_earth
if (global.groupHover == 2) {
    if (global.groupSelectedA == 0) {
        global.groupSelectedA = 2;
        instance_create(352, 62, obj_group_earth);
        scr_displayElements()
    }
    else {
        if (global.groupSelectedB == 0) {
            global.groupSelectedB = 2;
            instance_create(352, 222, obj_group_earth);
            scr_displayElements()
        }
    }
}
It should be fine (I think)
Here's scr_displayElements if it's relevant:
Code:
//obj_group_air
if (global.groupSelectedA == 1) {
instance_create(512,62,obj_air)
}
if (global.groupSelectedB == 1) {
instance_create(512,222,obj_air)
}
//obj_group_earth
if (global.groupSelectedA == 2) {
instance_create(512,62,obj_earth)
}
if (global.groupSelectedB == 2) {
instance_create(512,222,obj_earth)
}
 
V

VansiusProductions

Guest
@VansiusProductions
And how do you reset your global variables to 0?
Under the mouse leave event:
Code:
global.mouseHover = 0
And I also realized that I didn't reset them while it's creating the group instances so I did that but it still doesnt work!
Revised code:
Code:
//obj_group_air
if (global.groupHover == 1) {
    if (global.groupSelectedA == 0) {
        global.groupSelectedA = 1;
        instance_create(352, 62, obj_group_air);
        scr_displayElements()
        global.groupHover = 0
    }
    else {
        if (global.groupSelectedB == 0) {
            global.groupSelectedB = 1;
            instance_create(352, 222, obj_group_air);
            scr_displayElements()
            global.groupHover = 0
        }
    }
}
//obj_group_earth
if (global.groupHover == 2) {
    if (global.groupSelectedA == 0) {
        global.groupSelectedA = 2;
        instance_create(352, 62, obj_group_earth);
        scr_displayElements()
        global.groupHover = 0
    }
    else {
        if (global.groupSelectedB == 0) {
            global.groupSelectedB = 2;
            instance_create(352, 222, obj_group_earth);
            scr_displayElements()
            global.groupHover = 0
        }
    }
}
 
T

TheMatrixHasMe

Guest
and the groupSelected variables, when and where do you reset those to 0?
also I noticed the one says mouseHover and the others say groupHover.
Also, I don't think it's the problem but you can use a lot of else if statements everywhere if you don't have to check both. Like this
Code:
if{

}
else if{

}
So your code should and could look like this
Code:
//obj_group_air
if (global.groupSelectedA == 1) {
instance_create(512,62,obj_air)
}
else if (global.groupSelectedB == 1) {
instance_create(512,222,obj_air)
}
//obj_group_earth
else if (global.groupSelectedA == 2) {
instance_create(512,62,obj_earth)
}
else if (global.groupSelectedB == 2) {
instance_create(512,222,obj_earth)
}
In this way, once you enter one of the code blocks it will skip the other ones it doesn't need. Go ahead and see if you can identify the other areas of your code that you've shown us that can be done this way.
 
V

VansiusProductions

Guest
and the groupSelected variables, when and where do you reset those to 0?
also I noticed the one says mouseHover and the others say groupHover.
Also, I don't think it's the problem but you can use a lot of else if statements everywhere if you don't have to check both. Like this
Code:
if{

}
else if{

}
So your code should and could look like this
Code:
//obj_group_air
if (global.groupSelectedA == 1) {
instance_create(512,62,obj_air)
}
else if (global.groupSelectedB == 1) {
instance_create(512,222,obj_air)
}
//obj_group_earth
else if (global.groupSelectedA == 2) {
instance_create(512,62,obj_earth)
}
else if (global.groupSelectedB == 2) {
instance_create(512,222,obj_earth)
}
In this way, once you enter one of the code blocks it will skip the other ones it doesn't need. Go ahead and see if you can identify the other areas of your code that you've shown us that can be done this way.
Whoops I typed that wrong I meant groupHover. I will reset those values when the objects are clicked (on the right). But anyways i have an event called room start where it has a code that sets groupSelectedA and B and groupHover to 0.
 
T

TheMatrixHasMe

Guest
@VansiusProductions
Okay, make sure you are resetting variables and everything or else it could lead to behavior such as the one you're dealing with. Another thing you can do for testing is to give the different object different creation coordinates even if only by a little, just for testing, to see if they are both being created and one is overlapping the other.

edit: for example
Code:
//obj_group_air
if (global.groupSelectedA == 1) {
instance_create(512,62,obj_air)
}
else if (global.groupSelectedB == 1) {
instance_create(512,222,obj_air)
}
//obj_group_earth
else if (global.groupSelectedA == 2) {
instance_create(512,72,obj_earth)
}
else if (global.groupSelectedB == 2) {
instance_create(512,232,obj_earth)
}
this way they don't have perfect overlap and you can test it.
do it with your other coordinates as well.
 

TheouAegis

Member
Good, now I will tell you why it works.

The object you are calling that code in apparently has a sprite assigned to it. The collision mask of that sprite is large enough such that when some of the times you are clicking on one of the buttons, the mask overlaps two buttons. In this case, obj_Earth was being prioritized over obj_Air. Using position_meeting, the game is ignoring the mask of the sprite and just checking where the mouse is; there is only one object under the mouse.
 

GMWolf

aka fel666
Recommended reading: What's the difference: collision functions

This is the third time I've cited that article today, something is seriously wrong with the way novices are taught nowadays.
I would say this time, the problem lies with GM's unintuitive naming: place_meeting and position_meeting? easy to get them confused as positions and places are, kinda the same thing.
it would be more usefull to have point_meeting and mask_meeting, wouldnt it?
 
Top