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

Weapons Switching [SOLVED]

D

Dosko

Guest
Hello guys.
I want`t to ask you about shop and weapons.
I want the shop to buy weapons and ammunition.
Here, well, I am fine with the ammunition
Code:
if selected = 1 and global.p1_money >= 200
{
global.p1_money -= 200
global.uziammo += 10
}
if selected = 2 and global.p1_money >= 250
{
global.p1_money -= 250
global.akammo +=5
}
But for weapons, it becomes more complicated.
I have written a script for changing them, which is most likely wrong.
Code:
Player 1 Create
global.pistol = 1
global.uzi = 0
global.ak47 = 0
//
Player 1 Press B
if global.pistol = 1 {global.pistol = 0 global.ak47 = 1}
else if global.ak47 = 1 { global.aka47 = 0 global.uzi = 1}
else if global.uzi = 1 { global.uzi = 0 global.pistol = 1}
The problem is that whenever I change them, they become active.
That's why my code in the shop is doing nothing but selecting the appropriate weapon.
Code:
if selected = 3 and global.p1_money >= 300
{
global.p1_money -= 300
global.uzi = 1
}
Please, tell me where i`m wrong?
 
W

warbo

Guest
You could use an array and store your guns there and also anything else that you can think of that needs to be for that gun so you could do something really simple like

global.GunName[1] = "AK-47";
global.GunPrice[1] = "£5000";
global.GunSprite[1] = sprite_index = spr_AK

global.currentGun = 1;

then when you wanna use it's like (example)
draw_text(x, y, global.GunName[global.currentGun])
 
D

Dosko

Guest
Okey, maybe I did not say correct
I have a menu code, I do not want to be too complicated for doing so
Code:
//Draw
    draw_text(view_xview[0] + 220,view_yview[0] + 100 + 18,"Uzi" )
    draw_text(view_xview[0] + 220,view_yview[0] + 100 + 18 +35, "AK47" )
    draw_text(view_xview[0] + 220,view_yview[0] + 100 + 18 + 35*2, "AmmoUzi")
//release Enter
if selected = 1 and global.p1_money >= 200
{
global.p1_money -= 200
global.uziammo += 10
}
if selected = 2 and global.p1_money >= 250
{
global.p1_money -= 250
global.akammo +=5
}
if selected = 3 and global.p1_money >= 300
{
global.p1_money -= 300
global.uzi = 1
}
this is my code for the shop. I do not want anything more complicated than that.
I just want a code for changing weapons and buying them because what I've written does not work.
 
D

Dosko

Guest
I set selected with enter, and i buy ammonition with no problem, but weapon is on me, because when i use the "B" key, i change it without buy it.

Code:
Player 1 Create
global.pistol = 1
global.uzi = 0
global.ak47 = 0
//
Player 1 Press B
if global.pistol = 1 {global.pistol = 0 global.ak47 = 1}
else if global.ak47 = 1 { global.aka47 = 0 global.uzi = 1}
else if global.uzi = 1 { global.uzi = 0 global.pistol = 1}
Every thime when i press B, if i have a global.pistol = 1, their weapons are created by myself without buying them. I wan`t when press "B" to do nothing if i didn`t buy global.Uzi or global.aka47.
 
D

Dosko

Guest
Hello, I made a working shop system. But i want help to switching weapons, because my code is not good.
I have
global.weapon = 1
global.weapon2 = 0
global.weapon3 = 0
global.weapon4 = 0
global.weapon5 = 0
global.weapon6 = 0
When i buyit weapon

if shop = 1 and selected = 1 and global.p1_money >= 300
{
global.p1_money -= 200
global.weapon2 = 1
}
i have
global.weapon = 1
global.weapon2 = 1
global.weapon3 = 0
etc...
I want to change weapon with 1 key, if they buyit.
I try with this code
global.weapon+=1;
if(global.weapon==6)global.weapon=1;
But he activadet all the weapons.
 

Rob

Member
i think an array would help you out with this:

Code:
[create event]
weapons = 6;

//Create 6 slots for whether a weapon is selected or not in a global array
for (var i = 0; i < weapons; i ++){
   global.a_weapons[i] = false; //Selected or not (you could use 0/1 for this I guess
}

/*
This is just the same as typing "global.a_weapons" 6 times and typing a different number each time like this:
*/

global.a_weapons[0] = false;
global.a_weapons[1] = false;
global.a_weapons[2] = false;
global.a_weapons[3] = false;
global.a_weapons[4] = false;
global.a_weapons[5] = false;
Then when you press a key (or mouse click) / whatever:

Code:
/*
   Presuming that pressing 1 will affect global.a_weapons[0], 2 affects global.a_weapons[1] etc)
*/

if (keyboard_check_button_pressed(ord("1"))){

   //Reset other weapons to not selected
   for (var i = 0; i < weapons; i ++){
      global.a_weapons[i] = false;
   }

   //Set this weapon to true
   global.a_weapons[0] = true;
}
And you could copy/paste/modify this code for pressing keyboard numbers 2-6 for the different weapons and then just check which weapon is set to true or false.

It would work better if you moved your weapon values into arrays too (like cost/damage etc).
 
Last edited:
D

Dosko

Guest
Thank you @Rob, but i don`t understand very well array functions. So after many hour thinking i write this code
if global.weapon = 1 and global.weapon2 = 1 {global.weapon +=1; }
else if global.weapon3 = 1 {global.weapon +=1 if(global.weapon==4)global.weapon=1;}
Only for 3 weapons for the moment, but it`s easy to add more.
Is work perfect for me. Thanks again for help :) Close topic :)
 
Top