I need clarification on placeing objects in a room.

flyinian

Member
Correct me if I am wrong:
If I have an object and I place it into a room, that object in the room becomes an instance of said object. That instance will be at the x and y position that I placed it at in the room. All is fine, however it is recommended to code your x and y positions within that object to prevent issues coming up later. What code(s) do I use to place the object(instance) in the room. Do I use instance _create_layer? or do I use draw_sprite_ext? or is there another code to use?

here are questions:
1. If I use the draw_sprite_ext(or another similar code), will the surface(the interact-able surface for mouse click and such) of that object be the size of the sprite(a.k.a the collision mask of said sprite)?
2. If I place an object into a room and have code within the said object that creates it in the room, will it create two of the said object, since one is in the room and a line of code is also making it?

For clarification of what I want:
I want to place an object into the room with a given x and y position within the room. I then want to be able to use that object for mouse clicks, mouse hover reactions and so forth...What code(s) do I use to achieve this.

I am currently using a master object I place into a room with no x or y position or sprites w/ the "visible" check box unchecked This master object creates the instance of the object I want in the room using the "instance_create_layer_" code within it's "create" event. That object that is created by the master object, creates all of the other instances of the other objects. Master object > which spawns a menu background > that menu background spawns in all of the buttons.


Thank you.
 
Last edited:

AnotherHero

Member
To place an instance of an object, you use instance_create_layer or instance_create_depth. If you use draw_sprite/draw_sprite_ext, you are only drawing an image. It is not an object, it is a sprite. For your x and y positions, it depends: If you know the exact x and y it should be, you can definitely set x and y in the create event or just drag it into the room at those coordinates. I'm not sure what issues you're anticipating: you can always move your objects around. Even if you place it in the room at (0,0), if you set x and y to (32,128) in the create event, as soon as your game starts the object will be at (32,128).

I got confused reading your first question because surfaces are an actual thing in GMS. But your understanding is correct: the "surface" of htat object will be the size of the collision mask.

And for your second question: yes. It will create two. Because you are telling your game to create two! By putting one in the room and creating one with code, it will do just that. As with any programming: your game does exactly what you tell it to, nothing more, nothing less. If you don't want it to create two, you have a few options: just don't create two (don't place one in the room or don't make one with code), or in the code where it gets created, put it within an instance_exists check.

So, to achieve what you want: Create the object however works best: either directly placed in the room or with code, within that object's code, use Mouse (Pressed/Down: pressed = only on the initial press, down = as long as it's down...) and Mouse Enter (when the cursor enters the sprite's mask) events, and code what you want to happen within those events.
 

flyinian

Member
To place an instance of an object, you use instance_create_layer or instance_create_depth. If you use draw_sprite/draw_sprite_ext, you are only drawing an image. It is not an object, it is a sprite. For your x and y positions, it depends: If you know the exact x and y it should be, you can definitely set x and y in the create event or just drag it into the room at those coordinates. I'm not sure what issues you're anticipating: you can always move your objects around. Even if you place it in the room at (0,0), if you set x and y to (32,128) in the create event, as soon as your game starts the object will be at (32,128).

I got confused reading your first question because surfaces are an actual thing in GMS. But your understanding is correct: the "surface" of htat object will be the size of the collision mask.

And for your second question: yes. It will create two. Because you are telling your game to create two! By putting one in the room and creating one with code, it will do just that. As with any programming: your game does exactly what you tell it to, nothing more, nothing less. If you don't want it to create two, you have a few options: just don't create two (don't place one in the room or don't make one with code), or in the code where it gets created, put it within an instance_exists check.

So, to achieve what you want: Create the object however works best: either directly placed in the room or with code, within that object's code, use Mouse (Pressed/Down: pressed = only on the initial press, down = as long as it's down...) and Mouse Enter (when the cursor enters the sprite's mask) events, and code what you want to happen within those events.
Thanks for the clarification.
 
Top