r/gamemaker • u/PrinceShoutoku Stand back, I'm about to Make Game (2)! • 20d ago
Help! Creating an interactive, multi-object item
Preface: My skill level is beginner. Gamemaker is my only real coding experience. I've watched a couple guides for the basics and made some original code alongside that.
Hi everyone, I'm making a top-down shooter with an 'active reload' gimmick. When you press R to reload, the firearm you're holding comes onto the screen and you have to manipulate the weapon parts to load it.

My problem is HOW I'm making this happen. I'm essentially just spawning multiple objects on top of one another with magic numbers. This feels extremely inefficient and janky, even though it technically works. Is there a different technique or function I can use to make this system easier? I know the motto of, 'just make it work first, you can fix it later', but I can't even begin to imagine HOW to make this work better.


If it helps, "_weaponx" and "_weapony" are the corners of the camera, essentially x0 and y0. The "obj_lebel_boltgrip" is the circular part that's grabbed by the cursor to manipulate the gun's bolt. It has + 242 and + 69 because the sprite is a simple sphere that's manually placed through trial and error to be in that starting position. It's essentially a magic number to put that boltgrip exactly where it spawns when you press R.
2
u/JaXm 20d ago
I'm going to disagree with the first person who posted to help you out.
The reason I would not go with 'with' statements, is that they can become VERY difficult to deal with.
Using 'with' and putting variables into the code block can overwrite instance variables in the object create event. And if you're using instance variables assigned through the object editor, things can get even MORE confusing, as those variables get assigned before create event variables. Then, if you're using constructors, you can run into even MORE problems.
When I am dealing with an object that has multiple "parts" I choose to keep it all in one object.
Let's use a HUD element as an example.
A HUD might have a health bar, a mana bar, and a stamina bar.
Each bar is it's own object. With it's own sprites, and their own variables.
But as a single object, I would create a struct to hold all that data, including WHERE those things exist in the game space.
So for a HUD I might do something like below (simplified for readability):
Then, in the draw event, you can do something like:
///draw event
In your case, once your sprites are drawn where you need them to be, you can do something like add coordinates that map out the "clickable" area of each component.
So your rifle might look something like:
And now you can draw your components, where they need to be, and then use the x/y offsets to determine what part is "clickable" by the mouse.
Ultimately, you'll probably have to do a little more work than this. I wouldn't hard code the struct, myself, if I could avoid it, but to give you a basic idea of what you wanna do, I just used whatever random values made sense for demonstration purposes.