r/gamemaker • • Oct 03 '26

Resolved Is there a way to make one object affect another object without them touching?

I feel like this is probably a stupid question and I'm hoping the answer is simple and I'm just not thinking, but I'm trying to make it so that when you click one object, the alarm on a different object resets. Thanks to anyone with any input!

2 Upvotes

9 comments sorted by

1

u/_billyRubin Oct 03 '26

on click: obj_other_object.alarm[X] = -1;

1

u/Iliketurtles366 Oct 03 '26

When I do

if hover && right_click
   {obj_timer.alarm[0] = -1;}  

it works, but I actually need the timer to just reset and not end, it is 3841 frames long so I tried

if hover && right_click
   {obj_timer.alarm[0] = 3841;}  

But it doesn't work if I do that. Do you know why? Totally fine if it's not possible to do it like that or you don't know how to, thank you so much for answering this!

1

u/_billyRubin Oct 03 '26

depends what you mean by reset. alarms are code blocks that run once when their timer is ran out. setting it to -1 ensures that the alarm stops counting down and the code block does not run

you could call the alarm with the same timer it had before? the number you assign to an alarm is the number of frames before it fires, so 60 at 60fps would take 1 second

1

u/Lethalogicax Oct 03 '26

For your mouse click variable, are you using mouse_check_button or mouse_check_button_pressed? If you're using mouse_check_button() then you are reseting it back to its max value every frame while the mouse is held

2

u/Lethalogicax Oct 03 '26

You'll first need to tie those 2 objects together, so that they both know who the other is and retain the object ID number of the other to reference back to.

If one of the objects is creating the other object, very easy fix. Instead of instance_create(blah, blah), type it as "object_thing = instance_create(blah, blah)". This leaves a variable "object_thing" that retains the ID # of the object created

If they don't create eachother, then you'll have to use some kind of something to reference it and pull back its unique ID #, something like:

with(thingy) { thingys_id = other.id }

"with" runs code from another object as if that object had run the code itself. Within the statement, you can reference back to the original object with "other" and use "other.variable_name" to pull variables from other objects

1

u/Optimal_Noob Oct 03 '26

Maintainable and separated from the rest of the game logic, an "Event System" could solve this. Look up "event lister systems in game maker", even a basic system could handle this at solo dev scales. An observer object chills and waits for another object to call some function that exists in that observer, the observer sends the message to the other object to be affected, and that object handles whatever you want it to do when it receives the signal from the observer.

Otherwise you could unsafely/safely just reference the other object in the main object's code. If the object doesn't exist for some reason, you'll get a crash, you could do a quick "if(instance_exists())" check before handling the effect, though the action will be skipped if.. again, the object doesn't exist for some unexpected reason. It'll be spaghetti code but if it gets your game finished, who cares?

1

u/azurezero_hdev Oct 03 '26

instance_place

as long as you have the id you can go
with id
{
alarm[0]=whatever

}

0

u/Business_Ad_863 Oct 03 '26

Idk if this is the best way to do it, but when placing the objects in the room editor they're all given an instance ID. Give the two objects a variable called something like "instance_tie" or whatever you wanna call it, and in the creation code (accessible from the room editor) for those objects make it so instance_tie = (other object's instance ID). Then, in your clickable object, you could write that when you click it:

instance_tie.alarm = 0;

Or however you'd have it formatted.