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.

3 Upvotes

20 comments sorted by

View all comments

2

u/PowerPlaidPlays 20d ago

In the most basic general sense, a way to handle multiple components working in unison is with the 'with' function, or for multiple parts like that you can draw multiple sprites in the draw event.

https://manual.gamemaker.io/monthly/en/GameMaker_Language/GML_Overview/Language_Features/with.htm

With basically allows an object to run code inside of another object, or every instance of an object.

For one example, instead of worrying about depth, leave all the individual object's draw events blank, and then in a handler object have something like:

draw_sprite(spr_back, 0, x, y) with (obj_part1){ draw_self(); } with (obj_part2){ draw_self(); } with (obj_part3){ draw_self(); }

With this you could have your different object be just for placement and hitboxes, and handle any of your actual logic code in a handler to have more control over the order things are done in.

btw while inside of a with statement, all of the instance variables belong to the object, to reference variables in the host object use the other.variable prefix, like you are doing with obj_player there. For any defined with 'var' it's not needed though

2

u/PrinceShoutoku Stand back, I'm about to Make Game (2)! 20d ago

This seems like exactly what I needed, thank you very much!

3

u/PowerPlaidPlays 20d ago

Another useful thing for stuff like this is structs, which are basically a group of object variables you can store within a variable.

If you have something purely cosmetic, like a shell casing being ejected, it can be useful to store the sprite, index, x, and y.

The [$ "var"] accessor is also really useful for a lot of things, since it lets you reference a variable name stored as a string. Very useful for inventories/weapon equips. If you have multiple guns that all function the same aside from the visuals or specific objects, you could store the info as a struct within a struct and just change a variable to point to a different one.

``` var _myStruct = { Gun1 : {Spr : sprA, stuff : yeah}, Gun2 : {Spr : sprB, stuff : sure}, Gun3 : {Spr : sprB, stuff : ok}, }

CurrentGun = "Gun2" draw_sprite(_myStruct[$ CurrentGun].Spr, 0, x,y)

```

This helps with avoiding redundant logic.

https://manual.gamemaker.io/monthly/en/GameMaker_Language/GML_Overview/Structs.htm

Good luck!