• 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!

Problem with weapons and ammunition in the next room and when the hero dies

skofrant

Member
hi all,


I have a simple system for changing weapons, for now there are two basic weapons and a more powerful one, but there will be about 5 more of them.

And now this is how I start the game with a basic weapon ;-) to have this more powerful weapon I have to find it on the board ..

The player finds this weapon and it is automatically added to him together with the amount of ammunition, in this case 20 rounds, and on the hud bar the icon of this weapon is displayed immediately and the number of rounds under it.

I assigned the change of weapon to buttons 1 and 2 ... where 1 corresponds to the standard unlimited weapon, and 2 corresponds to a more powerful weapon with limited ammunition up to 20

The problem is that when I have this more powerful weapon and I go to the next board (room), I don't have it ... I still have an ordinary weapon, even though I still had ammunition in the previous board (room) ... and in order to have the additional one, I have to find the object again, hover over it and then the icon will also appear again with the additional weapon and the amount of ammunition ...

the second problem, when I start the game, I have a value of 20 assigned to the draw gui event. This should not be the case with the primary weapon, because it has unlimited ammunition, it should only appear with the second weapon, which has a limited number of bullets

Maybe someone will be able to change my attack code in the hero's steppe, because if there are a few more types of weapons, I will have a lot of it in the hero's code

Ok
Here below is my code


obj_player
create

GML:
global.primaryweapon=1
global.shotgun=0
global.shotgunammo=0


obj_player
step


GML:
 if keyboard_check(kAction) && attClock == false
{
if global.primaryweapon=1 or global.shotgun=0
{

attClock = 6;  
curAttack = 1;
with instance_create( x+(image_xscale*12), y-26, obj_primaryweapon)
{
hspd = other.image_xscale*15;
vspd = 0;
g = .0;
owner = par_player;
strength = 0;
explode = true;
explosionRadius = 20;
explosionStrength = 2+(sign(other.pwrupStr)*8);


}

}

}



if keyboard_check(kAction) && attClock == false
{
if global.shotgun=1 and global.shotgunammo>0
{

attClock = 6;  
curAttack = 1;
with instance_create( x+(image_xscale*12), y-26, obj_shotgun)
{
global.shotgunammo-=1
hspd = other.image_xscale*15;
vspd = 0;
g = .0;
speed=4;
owner = par_player;
strength = 0;
explode = true;
explosionRadius = 20;
explosionStrength = 2+(sign(other.pwrupStr)*8);


}

}

}
obj_weapon_chest

here a collision with the player (when he touch over the crates, he gets this weapon and ammunition)

GML:
global.shotgun=1
global.shotgunammo=1
global.primaryweapon=0

obj_player

pressing the button 1


GML:
global.primaryweapon=1

if global.shotgun=1
global.shotgun=0

obj_player

pressing the button 2


GML:
global.primaryweapon=0

if global.shotgun=0 and global.shotgunammo=1
global.shotgun=1

Now this is obj_game_controler perssistent who always accompanies the game ..

obj_game_controler

create


GML:
global.shotgunammo=20


obj_game_controler

Step

GML:
if global.shotgunammo>20
global.shotgunammo=20


obj_game_controler


Draw GUI

GML:
draw_text(x+76, 46,string(" " + string(global.shotgunammo)))

if global.shotgun=1
draw_sprite(spr_shotgun,0,x,y)

if global.shotgun=0
draw_sprite(spr_primaryweapon,0,x,y)
 

woods

Member
is your obj_player persistent?
in the create event you have..
global.shotgun=0

when you goto the next room, the variable is reset back to zero?




as far as your second problem, you are drawing the shotgun ammo no matter what.. try moving that line down?

obj_game_controler
Draw GUI

GML:
if global.shotgun=1

draw_sprite(spr_shotgun,0,x,y)

draw_text(x+76, 46,string(" " + string(global.shotgunammo)))



if global.shotgun=0

draw_sprite(spr_primaryweapon,0,x,y)
 

skofrant

Member
Yes, obj_player is persistant because he is a player
what value should I set so that after finding the shotgun, it does not reset it to zero in subsequent boards???

the solution below does not help ... the value of 20 is still displayed with the primary weapon :-(


GML:
if global.shotgun=1

draw_sprite(spr_shotgun,0,x,y)

draw_text(x+76, 46,string(" " + string(global.shotgunammo)))



if global.shotgun=0

draw_sprite(spr_primaryweapon,0,x,y)

/*
if global.primaryweapon=1 
draw_sprite(spr_weapon_box,0,x+76,y+76)
*/
 

woods

Member
how about we put an else in there?


if global.shotgun=1
{
draw_sprite(spr_shotgun,0,x,y)
draw_text(x+76, 46,string(" " + string(global.shotgunammo)))
}
else

if global.shotgun=0
{
draw_sprite(spr_primaryweapon,0,x,y)
}
 

skofrant

Member
Thanks, it helped ;-) but the problem still exists ;-( the player object is not persistent. but I threw this variable into obj_game_controler, which is persistent and still resets the weapon on subsequent boards:-(
 

woods

Member
do you have an instance of the obj_player in the next room(in the room editor)?

(thought)
when the persistent player changes room and the new room is created the one from the room editor overwrites the one being transferred from the previous room?
 

skofrant

Member
I will correct the code because I can see one variable has disappeared for me

obj_player
create

GML:
global.primaryweapon=1
global.shotgun=0
obj_player
step



GML:
if keyboard_check(kAction) && attClock == false
{
if global.primaryweapon=1 or global.shotgun=0
{

attClock = 6;
curAttack = 1;
with instance_create( x+(image_xscale*12), y-26, obj_primaryweapon)
{
hspd = other.image_xscale*15;
vspd = 0;
g = .0;
owner = par_player;
strength = 0;
explode = true;
explosionRadius = 20;
explosionStrength = 2+(sign(other.pwrupStr)*8);


}

}

}



if keyboard_check(kAction) && attClock == false
{
if global.shotgun=1 and global.shotgunhas>0
{

attClock = 6;
curAttack = 1;
with instance_create( x+(image_xscale*12), y-26, obj_shotgun)
{
global.shotgunhas-=1
hspd = other.image_xscale*15;
vspd = 0;
g = .0;
speed=4;
owner = par_player;
strength = 0;
explode = true;
explosionRadius = 20;
explosionStrength = 2+(sign(other.pwrupStr)*8);


}

}

}

obj_weapon_chest

here a collision with the player (when he touch over the crates, he gets this weapon and ammunition)

GML:
global.shotgun=1
global.shotgunhas=1
global.primaryweapon=0

obj_player

pressing the button 1



GML:
global.primaryweapon=1

if global.shotgun=1
global.shotgun=0

obj_player

pressing the button 2


GML:
global.primaryweapon=0

if global.shotgun=0 and global.shotgunhas=1
global.shotgun=1

Now this is obj_game_controler perssistent who always accompanies the game ..

obj_game_controler (perssistent)

create



GML:
global.shotgunhas = 0 //[B]shotgun it has!!![/B]

global.shotgumana = 20 // [B]shotgun ammunition!!![/B]
obj_game_controler (perssistent)

Step


GML:
if global.shotgunmana > 20
global.shotgunmana = 20


obj_game_controler (perssistent)

draw gui


GML:
if global.shotgun=1
{
draw_sprite(spr_shotgun,0,x+76,y+76)
draw_text(x+76, y+125,string(" " + string(global.shotgunmana)))
}
else

if global.shotgun=0
{
draw_sprite(spr_primaryweapon,0,x+76,y+76)
}
 
Last edited:

woods

Member
i bet that would be your problem.. if the create event says shotgun ammo zero.. and you move to a new room.... fresh new player object is created ;o)

i would make your obj_player persistent and have it placed in the beginning of the game only.(you can set the player.x/y in the following rooms without creating a new object)

that should keep the shotgun = true variable
 

woods

Member
you could move
obj_player create event:
global.primaryweapon=1
global.shotgun=0

from the obj_player, to the controller
 

skofrant

Member
Unfortunately it doesn't help to pass these variables from obj_player to obj_game_controler:-(
ie if the player were perssistent it would be the same .. So there must be some other solution.
 

skofrant

Member
maybe you have to give some condition that if the player finds the weapon, he has it and if he has ammunition, he also has it.
in the next rooms?

and if the player has a gun and no ammunition, the gun also has it in the next rooms.???
 

woods

Member
looking over your code again..

obj_weapon_chest

here a collision with the player (when he touch over the crates, he gets this weapon and ammunition)
GML:
global.shotgun=1
global.shotgunhas=1
global.primaryweapon=0
global.shotgunhas=1;
THIS is where the player gets the shotgun.. right?
if so, then this needs to be in the persistent object... i think ;o)
 

skofrant

Member
it does not do anything .. I set a persistent for obj_weapon_chest

so this solution is also not good ...
 

woods

Member
what i meant in my last post, the variable

global.shotgunhas=1

needs to be in a persistent object for the player to keep the shotgun on room change


sorry if i was unclear
 

skofrant

Member
Yes Woods I know .I moved it from obj_player to obj_game_controler (perssistent)

look

obj_game_controler (perssistent)

GML:
global.shotgunmana=20
global.shotgunhas=0
If I set the variable global.shotgunhas in the persisstent object obj_game_controler from 0 to 1, I have a weapon all the time, and it should be only when I get it, that's what this chest is for obj_weapon_chest
 

TheouAegis

Member
The more you change your code, the more you mess it up. Revert EVERYTHING back to how it was at the start of this thread.

Change the Draw event back to what was suggested
Code:
if global.shotgun=1
{
    draw_sprite(spr_shotgun,0,x,y)
    draw_text(x+76, 46,string(" " + string(global.shotgunammo)))
}
else
{
draw_sprite(spr_primaryweapon,0,x,y)
}
The player does not have to be persistent, but your controller does. Now, once again, move the code in the player create event from the player's create event into the controller's create event. Don't set the shotgun ammo to 20, because I have a hunch you are going to forget that you did that later. We do this because each time the player's recreated, we do not want to reset the ammo, so we should not be declaring the ammo variable inside the player at all.

When player presses 2, you need to put all the code inside the conditional. You also need to make it ADD to the ammo, not set it.
Code:
if global.shotgun=0 and global.shotgunammo+=1 {
global.shotgun=1;
global.primaryweapon=0;
}
Nitpick: When the player presses 1, you do not need to check if global.shotgun==1 before setting it to 0, just set it to 0. That conditional is just slowing down the code.


Once you get this all sorted out, then we can tackle having more than two weapons, because your code is going to need a complete overhaul in order to accommodate more than just two weapons.


Update: scratch that, focus on getting more than one weapon in the game, since your other thread is going to completely negate anything in this thread.
 
Last edited:

skofrant

Member
I have already set the event draw gui, as my colleague wrote above, and this part is already working ..



ok i moved this code from create obj_player to persistent object obj_game_controler

but on the first line it turns red, so there's an error somewhere

obj_game_controler (perssistent)

presses 2

GML:
if global.shotgun=0 and global.shotgunammo+=1 {
global.shotgun=1;
global.primaryweapon=0;
}
 
Top