Jump to content


Photo

Weapon Switch


  • Please log in to reply
5 replies to this topic

#1 Titus94

Titus94

    GMC Member

  • GMC Member
  • 75 posts

Posted 08 November 2009 - 02:54 PM

I've got this awsome TDS example from sodahertz with 4 different weapons (changing with 1-4) and I need help with one tricky thing

I want the player to start without weapons and being able to pickup weapons on the ground by pressing a button (max 2 weapons)
and can then switch between these two and replace them with other weapons he's standing on.

In create event
[codebox]weapon=1[/codebox]

In End Step Event

[codebox]//Weapon Changing
if keyboard_check_pressed(ord('1')){weapon=1}
if keyboard_check_pressed(ord('2')){weapon=2}
if keyboard_check_pressed(ord('3')){weapon=3}
if keyboard_check_pressed(ord('3')){weapon=4}
//Shooting
guns()[/codebox]

In the script called "guns"
[codebox]//HANDGUN
if weapon=1{
current_maxammo=hg_maxammo
current_ammo=hg_ammo
current_clips=hg_clips
if mouse_check_button_pressed(mb_left){
if hg_ammo>0{
hg_ammo-=1
instance_create(x,y,objBullet)}}}

//RIFLE
if weapon=2{
current_maxammo=rf_maxammo
current_ammo=rf_ammo
current_clips=rf_clips
if mouse_check_button(mb_left){
if rf_ammo>0 and canshoot=true{
rf_ammo-=1
b1=instance_create(x,y,objBullet)
b1.direction=b1.direction+-(random(10))
canshoot=false
alarm[0]=10}}}

//SHOTGUN
if weapon=3{
current_maxammo=sg_maxammo
current_ammo=sg_ammo
current_clips=sg_clips
if mouse_check_button(mb_left){
if sg_ammo>0 and canshoot=true{
sg_ammo-=1
b1=instance_create(x,y,objBullet)
b2=instance_create(x,y,objBullet)
b3=instance_create(x,y,objBullet)
b4=instance_create(x,y,objBullet)
b5=instance_create(x,y,objBullet)
b6=instance_create(x,y,objBullet)
b1.direction=b1.direction+(random(20))
b2.direction=b2.direction+(random(20))
b3.direction=b3.direction+(random(20))
b4.direction=b4.direction-(random(20))
b5.direction=b5.direction-(random(20))
b6.direction=b6.direction-(random(20))
canshoot=false
alarm[0]=75}}}

//GRENADE
if weapon=4{
current_ammo=grenades
current_clips=""
if mouse_check_button(mb_left){
if grenades>0 and canshoot=true{
grenades-=1
instance_create(x,y,objGrenade)
canshoot=false
alarm[0]=100}}}[/codebox]


It's kind of a big request but i have no idea where to start

-Titus

Edited by Titus94, 08 November 2009 - 02:55 PM.

  • 0

#2 The Kousoru

The Kousoru

    GMC Member

  • GMC Member
  • 75 posts

Posted 08 November 2009 - 04:01 PM

If you want him to start with no weapons, then in create event, make weapons = 0

If he needs to pick up a weapon with a max of 2, make 2 variables called global.gun1 = "first gun name" and
global.gun2 = "second gun name". Then you put:
if global.gun1 = "rifle" && weapon = 3 ...use.

to get a weapon, make the pick up object, then when you stand on it, press a button to pick it up, to change the gun1 or gun2 varibles.

I hoped it helped. Sorry if its alittle sloppy
  • 0

#3 2DLuis

2DLuis

    Graphic Designer

  • GMC Member
  • 2504 posts
  • Version:GM8

Posted 08 November 2009 - 04:21 PM

I would recommend changing all of these to a Switch Statement, the manual does a great job for beginners :)
also, set the weapon var to 0, to begin with
  • 0

#4 ramses12

ramses12

    6

  • GMC Member
  • 5769 posts
  • Version:GM8.1

Posted 08 November 2009 - 04:30 PM

Yeah. And also you can use an array for guns - guns[0]=main weapon guns[1]=second weapon.
  • 0

#5 Titus94

Titus94

    GMC Member

  • GMC Member
  • 75 posts

Posted 10 November 2009 - 08:00 PM

Okey, arrays and such are new to me as i'm a very novice programmer

could you give me some code examples on how to do the weapon pickup, the 2 weapon slots and switching between these two with 1 and 2

thanks!

/titus
  • 0

#6 ramses12

ramses12

    6

  • GMC Member
  • 5769 posts
  • Version:GM8.1

Posted 11 November 2009 - 08:13 AM

When you want to select a main gun you write
guns[0]=<selected main gun here>
and a secondary gun:
guns[1]=<selected second gun here>
For example:
//selecting the main weapon
if keyboard_check_pressed(ord('1')){guns[0]=1};
if keyboard_check_pressed(ord('2')){guns[0]=2};
if keyboard_check_pressed(ord('3')){guns[0]=3};
if keyboard_check_pressed(ord('3')){guns[0]=4};

//shooting
switch(guns[0]){

case 1:
//handgun
current_maxammo=hg_maxammo;
current_ammo=hg_ammo;
current_clips=hg_clips;
if mouse_check_button_pressed(mb_left){
if hg_ammo>0{
hg_ammo-=1;
instance_create(x,y,objBullet);
}};
break;

case 2:
//rifle
current_maxammo=rf_maxammo;
current_ammo=rf_ammo;
current_clips=rf_clips;
if mouse_check_button(mb_left){
if rf_ammo>0&&canshoot==true{
rf_ammo-=1;
b1=instance_create(x,y,objBullet);	   //You create the same bullet object?
b1.direction-=random(10);
canshoot=false;
alarm[0]=10;
}};
break;

case 3:
//shotgun
current_maxammo=sg_maxammo;
current_ammo=sg_ammo;
current_clips=sg_clips;
if mouse_check_button(mb_left){
if sg_ammo>0&&canshoot=true{
sg_ammo-=1;
for(i=0;i<=6;i+=1){
b=instance_create(x,y,objBullet);	   //Same bullet again??
b.direction+=random(100);
};
canshoot=false;
alarm[0]=75;
}};
break;

case 4:
//grenade
current_ammo=grenades
current_clips=""
if mouse_check_button(mb_left){
if grenades>0&&canshoot=true{
grenades-=1;
instance_create(x,y,objGrenade);
canshoot=false;
alarm[0]=100;
}};
break;
};
Switching between main and second weapon? Well you can easily do that:
var buffer;
buffer=guns[1];
guns[1]=guns[0];
guns[0]=buffer;

~Ramses12
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users