GML [SOLVED] Emulate mouse event

O

Oz Locke

Guest
I have a parent object called piece, and two children, white and black.
In the room, we just have the white and black pieces.
When a player clicks on a piece, it should change its value and sprite.

If I use the mouse event in the parent, with the following code, it works fine:
Code:
value++;
image_index = value -1;
I'd prefer it all be done in the step event though, and after searching the forum this seemed to be the right way to do it:
Code:
var instance = instance_position(mouse_x, mouse_y, all);
if (instance != noone) && (mouse_check_button_pressed(mb_left)){
    with(instance){
        value++;
        image_index = value -1;
    }
}
But it doesn't work (literally, nothing happens on click)

I've tried putting it in the step events of the children and also targeting the child objects directly instead of using all
 

Micah_DS

Member
I did a test myself and it didn't work until I put the parent object in the room with the children. After that, it worked fine. That appears to be the problem here.

But that really doesn't seem right at all, especially when the manual says this:
GMS2 manual - 'The Object Editor' page said:
It is generally considered good practice in most cases to create one base parent object and have this base object contain all the default behavior but never use an instance of it in the game.
The parent shouldn't need to be there. We may be overlooking something, but I'm not seeing the issue. Seems to be a bug? But I'm using GMS2. Not sure if it's different in GMS1.

EDIT:
And I did exactly your code, but in the parent object, I put this in the create event:
Code:
image_speed=0;
value=0;
image_index=value;
In the children objects, I literally put nothing.

The sprite I used for the children consisted of 2 images, white square and black square.

When doing it this way, without the parent in the room, I was able to make the image change only once for each child instance. With the parent in the room, it continues to alternate properly. Definitely a bug.

EDIT (again) - SOLUTION?
BUT, if you change the instance position code from checking "all" to checking "self", it will work for each individual instance, even when the parent isn't there.
 
Last edited:
O

Oz Locke

Guest
Thanks, K-BitCRUSH :D

Your assumptions about the layout are correct, and it's crazy, but yeah, the "self" thing works perfectly. (I'm still on 1.4)

Clearly there's something I'm not getting, but at least it's working now.

Thank you again :)
 
Top