• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Weapon switching, switch or array?

Niels

Member
Hi all,
For my game I want the players to be able to switch weapons.

I might pull it off with a switch and different scripts for different weapons, and I know how to use switches, but I have the feeling a array would be more effective in the long run. Problem is that (while I watched some videos on it) I don't understand how to implemt arrays yet.

Basicly I need the following variables for every weapon:
Damage
Clipsize
Current ammo in clip
Ammo (amount)
X coordinate of end of barrel
Y coordinate of end of barrel
Sprite of guniet

So is it worth diving into arrays for this, or will a switch work just as good?
 

GMWolf

aka fel666
Arrays are a very bad way of doing multiple weapons: what if the behavior of you weapons changes? (Say you have a hand gun and a flame thrower).

A good option is to have a separate object for each weapon.
Have a look at my video on OO in GM:
YouTube link
I recommend you watch it a 1.5x speed. I was speaking quite slowly.
 

Niels

Member
Thx for the reply. I already have different objects for all weapons so might store are variables in the gun itself
 

GMWolf

aka fel666
Thx for the reply. I already have different objects for all weapons so might store are variables in the gun itself
That's a good plan.
Remember you only need to have an instance of the gun you are using. Dont keep around all your object as instances.

You can create an array that stores each of your guns, so you can easily find them.

You can create a script to manipulate the gun (fire, move, etc), without knowing what gun you are using like shown in my tutorial.
 
Top