GMS 2.3+ Help with the "point in rectangle" function!

face

Member
I have this code:
GML:
var a = point_in_rectangle(mouse_x, mouse_y, 215, 660, 360, 700);

var layerID = layer_get_id("instances");



if (a == true) {

 

    instance_create_layer(600, 700, layerID, obj_starttext);

 

}
It's supposed to make an object appear when I hover the mouse inside an area. I don't know if it's the syntax but it's not working.
In the GML Documentation the point in retangle example has, in the rectangle coordinates, x - a, y - b, x + c, etc. where a, b and c are numbers:

point_in_rectangle(mouse_x, mouse_y, x+215, y+660, x+360, y+700);

I've tried the above but it still doesn't work. I know I'm missing something but can't figure out what it is. Any help is appreciated. ^^
 

TsukaYuriko

☄️
Forum Staff
Moderator
Please use code tags to format your code. That's [code], not [icode]. ;)

First of all, find out which part of your code is actually running - and which part may not be. You could do this by throwing a bunch of show_debug_message statements in there and then checking which ones are actually being displayed. This will help you locate the origin of the issue.

At a glance... is your layer actually named "instances", and I mean literally "instances" with that exact capitalization (as in, not "Instances")?
 

face

Member
Please use code tags to format your code. That's [code], not [icode]. ;)

First of all, find out which part of your code is actually running - and which part may not be. You could do this by throwing a bunch of show_debug_message statements in there and then checking which ones are actually being displayed. This will help you locate the origin of the issue.

At a glance... is your layer actually named "instances", and I mean literally "instances" with that exact capitalization (as in, not "Instances")?
Yes the i is not capitalized, I changed it so I was sure that I wasn't typing it wrong. I'll do the debug message, hopefully that will help me fix it.
 

face

Member
Please use code tags to format your code. That's [code], not [icode]. ;)

First of all, find out which part of your code is actually running - and which part may not be. You could do this by throwing a bunch of show_debug_message statements in there and then checking which ones are actually being displayed. This will help you locate the origin of the issue.

At a glance... is your layer actually named "instances", and I mean literally "instances" with that exact capitalization (as in, not "Instances")?
I did a debug message inside the if statement and it didn't show up (so my variable a is false). The problem are probably the coordinates of the rectangle, I don't know how I'm supposed to define the rectangle and the gml documentation isn't very specific on that either.
 

muki

Member
point_in_rectangle(px, py, x1, y1, x2, y2);


pxThe x coordinate of the point to check.
pyThe y coordinate of the point to check.
x1The x coordinate of the left side of the rectangle to check.
y1The y coordinate of the top side of the rectangle to check.
x2The x coordinate of the right side of the rectangle to check.
y2The y coordinate of the bottom side of the rectangle to check.
 

TsukaYuriko

☄️
Forum Staff
Moderator
I'll dare to claim that the documentation is very specific about it:
ArgumentDescription
pxThe x coordinate of the point to check.
pyThe y coordinate of the point to check.
x1The x coordinate of the left side of the rectangle to check.
y1The y coordinate of the top side of the rectangle to check.
x2The x coordinate of the right side of the rectangle to check.
y2The y coordinate of the bottom side of the rectangle to check.
Source: https://manual.yoyogames.com/#t=Gam..._Collisions/Collisions/point_in_rectangle.htm

Is this the manual page you were looking at? If so, could you clarify what is unclear about it so we could try to clear it up?


On the subject of draw_rectangle - whenever I have issues with detecting anything in a rectangular area, I like to draw said rectangular area via draw_rectangle. This lets you see where your code will expect something, so you won't waste time debugging why the thing isn't working when the area to be checked is wrong in the first place.
 

face

Member
I'm sorry to everyone but I made a post on reddit and a kind soul answered.
I want to point out I've spent 2 hours reading de documentation of GML and came up with that code thinking it was correct. The problem in my code is that the x1 y1 x2 and y2 has to be RELATIVE to the object running the code. That's why, in the documentation, there's the x-a, y-b, etc. a and b being numbers and the x and y being the position of the object running the code. Thank you for everyone's response.

I've just started learning GML and know a bit of C. Still, object oriented programing is very unfamiliar to me although I took a course a few years back on GMS 1.4


I'll dare to claim that the documentation is very specific about it:


Source: https://manual.yoyogames.com/#t=GameMaker_Language/GML_Reference/Movement_And_Collisions/Collisions/point_in_rectangle.htm

Is this the manual page you were looking at? If so, could you clarify what is unclear about it so we could try to clear it up?


On the subject of draw_rectangle - whenever I have issues with detecting anything in a rectangular area, I like to draw said rectangular area via draw_rectangle. This lets you see where your code will expect something, so you won't waste time debugging why the thing isn't working when the area to be checked is wrong in the first place.
To answer you, the part that was not clear was the x and y in the rectangle position. Now I know that it's that it's the position of the object running the code.
Thank you for your help anyway. 😄

If you're familiar with draw_rectangle(), it's the exact same concept. Try drawing the rectangle to see where it actually is.

I was not familiar with it, it could have helped me a lot. 😅

EDIT: I just got home and tested the code, it still doesn't work. The draw_rectangle function doesn't draw anything on the screen and I've tested with a big rectangle. I believe it's a problem with something else but one thing that works is the debug messages. I have no idea what the problem is though.
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
I've just started learning GML and know a bit of C. Still, object oriented programing is very unfamiliar to me although I took a course a few years back on GMS 1.4
GML is not object oriented, so I'm not sure about the relevance of this statement... :)

EDIT: I just got home and tested the code, it still doesn't work. The draw_rectangle function doesn't draw anything on the screen and I've tested with a big rectangle. I believe it's a problem with something else but one thing that works is the debug messages. I have no idea what the problem is though.
Is the instance drawing this marked as visible, and is the draw code in a Draw event? Does a filled rectangle from 0,0 to room_width,room_height show up?

Could you also post the code with the debug messages added in, as well as the output log showing which of these messages are being triggered when you do what?
 
Why not? I've heard of that term, but I can't understand what makes a linguager object-oriented 🤔
A couple things makes it OOP, like inheritence and polymorphism. Type "OOP" in Google and it will probably tell you a lot more than I can in a single post.
GML borrows some of these concepts, like the parent-child and the constructors, tho it's not OOP (it's confusing because the resource asset is named "object", but these are not the objects in "OBJECT"-oriented programming)
 

TsukaYuriko

☄️
Forum Staff
Moderator
It's less of a tangible aspect of a language and more of a paradigm... a language can only be as object-oriented as the workflow you can use said language in allows you to do. GML is not object-oriented just because there's something called an "object". Keeping this in mind, GML is about as object-oriented as castoreum is vanilla. It certainly shares some aspects and may be considered similar by some... but it's not the same. Not in the former case, and definitely not in the latter case.

With that said, this is probably not the best place to discuss this subject as this topic is about something entirely different, so if you're interested in learning more about object-oriented programming, I suggest to look it up externally. My original comment that spawned the discussion was just a passing remark intended to hint at the lack of a cause for concern about GML feeling unfamiliar due to being object-oriented... because it isn't. ;)
 

face

Member
I'll have to say sorry again. I have fixed it for real now. I messed with it for about 2 hours and realized that I needed the step event and used this code:

GML:
//I used my previous a variable
//I only changed the if

if (a == true) {

    "instance_name".visible = true;

} else {

    "instance_name".visible = false;

}

//I had to set this up in the pop up object create event

visible = true;

//also set the object to visible
It was a weird way to do it, it took too many rectangles being drawn and too many debug messages but most of my error was not using the step event. Now I can say I really found a solution. Thank you to everyone who helped.

It's less of a tangible aspect of a language and more of a paradigm... a language can only be as object-oriented as the workflow you can use said language in allows you to do. GML is not object-oriented just because there's something called an "object". Keeping this in mind, GML is about as object-oriented as castoreum is vanilla. It certainly shares some aspects and may be considered similar by some... but it's not the same. Not in the former case, and definitely not in the latter case.

With that said, this is probably not the best place to discuss this subject as this topic is about something entirely different, so if you're interested in learning more about object-oriented programming, I suggest to look it up externally. My original comment that spawned the discussion was just a passing remark intended to hint at the lack of a cause for concern about GML feeling unfamiliar due to being object-oriented... because it isn't. ;)
I thought OOP was applied when you are working with graphics rather than the traditional command line programs, not because of the objects. I have friends who had to do an OOP class in college and they needed to make a video in Unity so it just made sense to me that OOP meant working with graphical content. My bad on that one.
 
Top