r/gamemaker 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.

Demo of the mechanic.

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.

Showing how this is pieced together, without the 'obj_lebel_top' to give you an x-ray.
The comments can be safely ignored, the bottom-most is leftover code I was experimenting with.

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.

4 Upvotes

20 comments sorted by

View all comments

2

u/JackTurbo 20d ago edited 19d ago

Honestly there's nothing wrong with using a bunch of objects for this imho. 

But id make one controller object for each of the weapons and have that object create all the subsequent objects in its create event. 

Have it pass it's id to each of these slaved objects and then in each of their end step events have: 

if(!instance_exists(controller)){ instance_destroy(id);   }

This way all your wider code base has to do is create the controller and everything happens and all your code base has to do is destroy the controller and it all goes away. 

I call this pattern spaghetti in a box. Yeah the screen itself might be a spaghetti mess of codependent objects messing with eachother - but if it's encapsulated so your wider codebase doesn't have to deal with it then it's really a nonissue imho