• 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 How to get instance coordinates

V

VansiusProductions

Guest
Hi guys,
I want to get the coordinates of an instance when it is clicked by mouse. How do I do that?
 
Code:
var inst = instance_position(mouse_x, mouse_y, obj_instance);
if (inst != noone) {
    inst_x = inst.x;
    inst_y = inst.y;
}
Checks for a certain instance at mouse coordinates, then gets the x and y values.
 
V

VansiusProductions

Guest
Code:
var inst = instance_position(mouse_x, mouse_y, obj_instance);
if (inst != noone) {
    inst_x = inst.x;
    inst_y = inst.y;
}
Checks for a certain instance at mouse coordinates, then gets the x and y values.
Didn't work, nothing showed up. Still trying
 
V

VansiusProductions

Guest
@shadowspear1 's code should work, you just need to add some draw_text code to your draw event in order to display the inst_x and inst_y variables.
Im using the inst.x and inst.y in his code to create an instance:
Code:
instance_create(inst.x,inst.y,obj_selection)
but it's not showing up
 

Jezla

Member
Post more of your code, please. It's hard to tell what the problem is unless we can see how you're using the code that was suggested.
 
V

VansiusProductions

Guest
Where are you running that code?
In a script. called by:
Code:
with (obj_selection) {scr_selection()}
Post more of your code, please. It's hard to tell what the problem is unless we can see how you're using the code that was suggested.
Code:
if (global.elementSelectedA != 0) {
    var inst = instance_position(mouse_x, mouse_y, obj_element);
    if (inst != noone) {
        inst_x = inst.x;
        inst_y = inst.y;
        visible = 1;
        instance_create(inst.x,inst.y,obj_selection)
    }
}
That's basically it nothing more. I tested the script, it does get executed
 
W

whale_cancer

Guest
In a script. called by:
...and where is that script called? I am going to assume in a mouse press or release event.

What is your obj_selection like? Does it have a sprite?

Check obj_element's collision mask and bounding box.

Is it even running this code? Does it get past "global.elementSelectedA != 0"? Use show_debug_message to see if it does.
 
V

VansiusProductions

Guest
In a script. called by:
Code:
with (obj_selection) {scr_selection()}
Code:
if (global.elementSelectedA != 0) {
    var inst = instance_position(mouse_x, mouse_y, obj_element);
    if (inst != noone) {
        inst_x = inst.x;
        inst_y = inst.y;
        visible = 1;
        instance_create(inst.x,inst.y,obj_selection)
    }
}
That's basically it nothing more.
...and where is that script called? I am going to assume in a mouse press or release event.

What is your obj_selection like? Does it have a sprite?

Check obj_element's collision mask and bounding box.

Is it even running this code? Does it get past "global.elementSelectedA != 0"? Use show_debug_message to see if it does.
It's called in a mouse left button released event. It has a 70x70 pixel sprite, full image bounding box. Yes, I mentioned that i tested it. Yes I used show_debug_message and it does
 
W

whale_cancer

Guest
Are you using a global mouse event? Because if you are calling it in an object other than obj_element, it won't execute unless you are in the calling object's bounding box (i.e. never in this case).

EDIT: With just two objects and this code in obj_selector's global left pressed code, I get the coordinates properly displayed.

Code:
    var inst = instance_position(mouse_x, mouse_y, obj_element);
    if (inst != noone) {
        show_debug_message('coords are '+string(inst.x)+','+string(inst.y))
    }
 
V

VansiusProductions

Guest
Are you using a global mouse event? Because if you are calling it in an object other than obj_element, it won't execute unless you are in the calling object's bounding box (i.e. never in this case).

EDIT: With just two objects and this code in obj_selector's global left pressed code, I get the coordinates properly displayed.

Code:
    var inst = instance_position(mouse_x, mouse_y, obj_element);
    if (inst != noone) {
        show_debug_message('coords are '+string(inst.x)+','+string(inst.y))
    }
I called the script from obj_element. obj_selection Still not showing up. The coords are showing up for me
 

Jezla

Member
You're calling it from a with statement correct? Does an instance of obj_selection already exist? The new instance could be overlaying the old one, so you don't see it perhaps? Is there any other code you haven't posted from the mouse left button released event?
 
V

VansiusProductions

Guest
You're calling it from a with statement correct? Does an instance of obj_selection already exist? The new instance could be overlaying the old one, so you don't see it perhaps? Is there any other code you haven't posted from the mouse left button released event?
No I am not, since @whale_cancer said that obj_element should call it, since the code is already in obj_element, I didn't use with statement. yes, I placed an instance in the room so it would exist in the room. I don't think it will overlay cause their positions are different. The code for left button released event:
Code:
if (global.elementHover != 0) {
    if (global.elementSelectedA == 0) {
        global.elementSelectedA = global.elementHover;
        global.elementHover = 0
    }
    else {
        if (global.elementSelectedB == 0) {
            global.elementSelectedB = global.elementHover;
            global.elementHover = 0
        }
    }
    scr_selection()
}
scr_comboElements()
scr_selection is the script name
 

Jezla

Member
Sorry, you posted above that it was called by with(obj_selection){scr_sleection()}, so I guess that confused me.
I guess I'll ask an obvious question: Is obj_selection flagged as visible?

Do your global variables hold true or false values? If so, I'd suggest using the keywords 'true' and 'false,' rather than 1 and 0, as it makes your code a little more readable (in my opinion).
 
V

VansiusProductions

Guest
Sorry, you posted above that it was called by with(obj_selection){scr_sleection()}, so I guess that confused me.
I guess I'll ask an obvious question: Is obj_selection flagged as visible?

Do your global variables hold true or false values? If so, I'd suggest using the keywords 'true' and 'false,' rather than 1 and 0, as it makes your code a little more readable (in my opinion).
Yes it's visible. No they hold integer values, 0 as nothing, other numbers are like IDs for elements. I removed the with statement after @whale_cancer said I should be calling it from obj_element. I changed that and it still doesn't work.
 

Jezla

Member
Sorry for asking so many questions, but I'm trying to narrow down the problem, as it seems like it should work.

So, next question is why the mouse released and not mouse pressed? Are you dragging an element to a position and dropping it there?
 
V

VansiusProductions

Guest
Sorry for asking so many questions, but I'm trying to narrow down the problem, as it seems like it should work.

So, next question is why the mouse released and not mouse pressed? Are you dragging an element to a position and dropping it there?
That was actually an alternate attempt of making this kind of game using Drag and drop. But I eventually settled with a click and click approach, similar to Doodle God, Doodle Alchemy. Idk why I prefer left released though, I changed it to left pressed, still not working, :(
 

Jezla

Member
Hmm, I'm really kind of stumped now. the only thing I can think to suggest is to change instance_create(inst.x, inst.y, obj_selection); to instance_create(inst_x, inst_y, obj_selection);

All that's really doing though is using the variables that hold the same values as you have it now. Other than that, I'm really not sure.

It's getting late here, so that's it for me tonight. If I think of anything in the morning, I'll let you know. Sorry I couldn't be more help. :(
 
V

VansiusProductions

Guest
Hmm, I'm really kind of stumped now. the only thing I can think to suggest is to change instance_create(inst.x, inst.y, obj_selection); to instance_create(inst_x, inst_y, obj_selection);

All that's really doing though is using the variables that hold the same values as you have it now. Other than that, I'm really not sure.

It's getting late here, so that's it for me tonight. If I think of anything in the morning, I'll let you know. Sorry I couldn't be more help. :(
Nope it didn't work. Probably because it's treating inst_x and inst_y as variables. But I didn't get the error message like inst_x or inst_y not defined. Yeah same for me it was night but now it's in the morning, glad you'd still like to help today.
 

Jezla

Member
Okay, let's see if we can figure it out. First, can you describe what your logic is supposed to do, and what the global represent? Also, what is the code in scr_comboElements()? Could there be anything in it that is causing the new instance to be destroyed or deactivated?
 

TheouAegis

Member
He's, as he said, making a game based on Doodle God. You have a list of elements. When you click on an element, it opens a list of all the subelements in that element . Then you click on an element again and it opens another list. Then you click on one subelement from each element set, it checks if those elements form a valid pair, and then create a new element(s) if they are a valid pair.
 

Jezla

Member
Okay, I understand. I had to look up Doodle God and try it to see how the mechanics work. So, does obj_element represent the element group, or the sub-element to be combined? Which does obj_selection represent?
 

Jezla

Member
Okay, I made a little test project to test the code you've given. Note it may not work the way yours does, as you may have code I don't know about, but the issue is that an instance is not being created that should be, so here's what I did:

I have 4 objects, oParent, oA, oE, oSelect. oA and oE are children of oParent, and oSelect is the instance to be created. Here's the code I used (a variation on what you provided):

Code:
///Object oA create event
event_inherited();
str = "air"; //oE is the same, except str is "earth"

Code:
///oParent Create event
str = 0;
global.selectedA = 0;
global.selectedB = 0;

Code:
///oParent Mouse Enter event
global.hover = str;
show_debug_message(global.hover);

///oParent Mouse Leave Event
global.hover = 0;
show_debug_message(global.hover);

Code:
///oParent Mouse Left Click event
var inst = noone;
var inst2 = noone;

if (global.hover != 0)
    {
        if (global.selectedA == 0)
            {
                global.selectedA = global.hover;
            }
        else if (global.selectedB == 0)
            {
                global.selectedB = global.hover;
            }
        global.hover = 0;
     
        if (global.selectedA != 0)
            {
           
            inst = instance_position(mouse_x, mouse_y, oParent);
         
            if (inst != noone)
                {
                    inst2 =instance_create(inst.x+40, inst.y, oSelect);
                }
            }
    }
show_debug_message(string(inst));
show_debug_message(global.hover);
show_debug_message(global.selectedA);
show_debug_message(global.selectedB);
show_debug_message(string(inst2));

Here's the output from the debug messages:

air
0
air
100000
0
air
0
100003
0

The new instance of oSelect did create (twice, because I clicked twice). You'll notice that above I created it to the right of the calling object, and it showed up fine in the room. I also tried it by creating it at the x/y position of the calling instance, and it created underneath oA (I couldn't see it, but the debug message printed the instance id).

Long story short, the logic of the code you've provided seems sound. the only thing I can think of is that there is something is causing your global.elementSelectedA to equal 0 or the var inst to equal noone so that the obj_selection is not created, OR something is causing the instance to destroy as soon as it's created.

EDIT: mistake, it didn't create twice, the first id is the var inst id. I was able to get it to work multiple clicks, though.

EDIT 2: had to adjust the left clicked event, see above. It was returning an error. Further testing shows that the second click returned noone for inst. I had to move the mouse out of the object and back in to create a second oSelect.

Here's my suggestion: move your global.elementHover = 0; line outside the if statement brackets and right before scr_selection, and see what that does.
 
Last edited:
Top