# [Game Maker] Weapon change system for a novice?

This topic is 1226 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey there,

so i'm fairly new to GML (although i have been messing about with game maker for a little while now) and i'm making a simple top down shooting game. The trick is that as you kill baddies you rack up a score, at certain score milestones you gain a new weapon. You keep your old weapon, so as you go along you build up a collection of weapons, you get the idea.

You press the number keys to choose weapons, it will switch object instance to the next gun with it's own sprite. You press 2 to choose your second weapon you unlock at say 400 points, 1 to go back to starting weapon, then 3 to choose the next weapon you unlock at say 400 points in the next room ect.

At the moment, i have a controller object for each weapon that acts as the ammo counter and HUD, but i just cant seem to get it to actually keep count of the ammo for each gun correctly. As it is the starting weapon works, the ammo controller draws the actual amount on screen and the gun stops firing at 0. I can change weapon too, aslong as i have the required points. However the ammo counter doesn't change on any weapon except the first so i have unlimited ammo for every other gun. It also wont go back to the starting weapon as i want, it keeps the second gun's ammo count. There are odd bugs like this that really are confusing me. This is after trying somebodies advice which changed it to a new system but made new problems.

Ive been researching these issues and how to implement weapon switching into GML but i've found few helpful results, although this forum did come up and is why i'm here

I've started another project to play with this idea and i have had some luck with only 2 weapons but i'm still struggling to get it down so i actually understand how to switch weapons in a game.

So tl:dr..

If anyone has created a weapon changing system in Game Maker before and has any advice for a learner, knows where to find information on the subject or if you know of a good tutorial for this sort of thing please post it for me, it would be greatly appreciated, i'm enjoying making my first proper game but a few issues are holding it back

##### Share on other sites

You'd probably get better answers on a forum related to game maker, but posting code usually helps diagnose the problem better along with describing the behavior. Regardless of the language the solutions are usually generally the same.

The problem is that you're describing something not working that could be caused by a billion different things.

##### Share on other sites

Yeah i was going to post the inner workings but i thought i'd see if anyone has a better system that they know works first lol, and i will probably make a topic on the gmc about it later but it cant hurt to try other places too

I'm going to try changing the sprite instead of the entire player object each time and see if that makes a difference (i've found a semi-helpful tutorial on this)

But if anyone is interested, what i've got as of now goes as follows; (ill just use 2 weapons as an example)

I reach say, 400 points.

Press key '2' the room controller object checks score and if larger than 399. obj_playerPISTOL instance change into obj_playerSMG.

"         "     '2' same as above, obj_ammoPISTOL instance change into obj_ammoSMG

(the above isnt in code it's in D&D, the below is however;)

The new ammo controller then draws (same as the last one, but obviously replacing the text and ammo variable)

if instance_exists(obj_playerSMG)
{
depth=-10 draw_text(view_xview[0]+view_wview[0]-200, view_yview[0]+view_hview[0]-64,(mytext) + string(global.ammo_smg))
}

The 'mytext' is a variable that changes based on which gun is equipped, if it's the SMG player object it'll read that as variable(gun) =1 and if gun 1 it tells it to display ''SMG ammo:'', which happens in the create event. I've tried the same with the ammo variable but i just cant seem to get it working. This was somebodies idea, it fixed an old issue but now the ammo counter never changes on any gun except the first.

Then of course if i want to switch back to the starting gun (gun =0, the pistol) i can hit key '1' and just like before, the player will change object back to the pistol object, and the ammo counter will too with this;

if instance_exists(obj_playerPISTOL)
{
instance_change(obj_ammo_PISTOL,true)
}

Except obviously, it keeps the 'SMG Ammo: ' and it's ammo count, but i can shoot and lose ammo even if there is loads more of it lol

The system is pretty much as simple as that, except theres 6 weapons im trying to impletement lol. They all fire fine and have their own sound effect and all that but i'm still fighting with bugs over this.

May edit later once i've had another go at messing around with this, if you need anymore/any different info let me know i'm not 100% sure what to put down haha, thanks

Edited by N00bGames

##### Share on other sites

I use GM extensively so I have an idea what's going on.  I don't see any code in that post that actually does the ammo decreasing.  If you have all the separate objects manage the ammo separately, then you probably have the code, but I don't see it there so I mention it.

I hate to say it, but I recommend a different approach to the problem.  You are using variables for a few things, but you could use them more efficiently, making things more organized.  I would have only one player object, and if the weapons are like totally different, maybe separate objects for them, but not separate players.  You could change the player sprite too if it needs to look different for different weapons.  So, on some single object, probably a global object, I would track all of the ammo.  I would make them global variables too just to make it easy to increase/decrease/check the values from anywhere.

So, if the weapons are about the same, but different shot speed, etc...  you could use the same object and just change the shooting.  If they are massively different, than just use different objects.  Either way, just make this object follow the player.  An easy way would be to make all the weapon objects "child" objects of a single parent weapon.  Then in the player object, any time it moves, move that parent object with it.  If you use.../

with(objParentWeapon)
{
x = other.x;
y = other.y;
}


It will go through any objects that are children of the objParentWeapon object.  The "x = other.x" makes the object the same as the player x position.  It works like this because the 'with' statement puts you into the context of the other objects, as if that code were actually on that other object, at least while in the brackets.  Then you use the 'other.x' to get back to the player temporarily, accessing the player's x, just like you would use the 'other' word to access the other object in a collision event.

So, the key is to use variables to your advantage.  Instead of seeing if some weapon object exists in your code to draw the ammo, you would instead simply check a single global variable that is a number 1 through 6 saying which weapon you have(1 = pistol, 2 = rifle).  You can also use the macro/constant system to define exactly that, where you could put WEP_PISTOL instead of the number 1, and it is easier to read, you get the automatic intellisense(as in the text pops up like functions do as you type them), and you don't have to remember which weapon is which number anymore.

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 17
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631758
• Total Posts
3002163
×