Jump to content


Photo

Weapon Pick Up


  • Please log in to reply
6 replies to this topic

#1 8feet

8feet

    GMC Member

  • GMC Member
  • 104 posts

Posted 03 February 2010 - 05:28 PM

There is something really wrong with my codes...you see here is what I want it to work.

NOTE: Not really good in how to explain things clearly Lolz;D!

If the "weapon[current_weapon, gun_eqp] = true" that means I can use this weapon. If "false" then I can't use the weapon.
Now I want is this...as soon as the player pressed "Space Bar" while he is right on top of the weapon on the ground will replace my current weapon with the weapon that's on the floor,my previous weapon is now on the floor, if the two weapons are similar,the current weapon that I'm holding is the same as the weapon on the floor,nothing will happen as soon as I press the "Space Bar" but I can't get it to work,because I'm still new with GML.

Player Code...
Create Event:
execute code:

globalvar shoottime, weapon_num, weapon, current_weapon;

shoottime = 0; //If the player needs to wait before he can shoot again

//Melee Attack Seperated from weapon list
  weapon[melee, bul_spr] = spr_melee;
  weapon[melee, bul_spe] = 0;
  weapon[melee, bul_acc] = 0;
  weapon[melee, bul_num] = 1;
  weapon[melee, bul_dam] = 50;
  weapon[melee, bul_dly] = 15;
  weapon[melee, bul_amo] = 1;
  weapon[melee, gun_eqp] = true

//Weapon List
weapon_num = 0;
  weapon[flamethrower, bul_spr] = spr_fire;
  weapon[flamethrower, bul_spe] = 7;
  weapon[flamethrower, bul_acc] = 10;
  weapon[flamethrower, bul_num] = 8;
  weapon[flamethrower, bul_dam] = 1;
  weapon[flamethrower, bul_dly] = 0; 
  weapon[flamethrower, bul_amo] = 500;
  weapon[flamethrower, gun_eqp] = true
weapon_num += 1;
  weapon[grenade_launcher, bul_spr] = spr_Glauncher;
  weapon[grenade_launcher, bul_spe] = 9.5;
  weapon[grenade_launcher, bul_acc] = 2;
  weapon[grenade_launcher, bul_num] = 1;
  weapon[grenade_launcher, bul_dam] = 9;
  weapon[grenade_launcher, bul_dly] = 25;
  weapon[grenade_launcher, bul_amo] = 20;
  weapon[grenade_launcher, gun_eqp] = true
weapon_num += 1;
  weapon[shotgun, bul_spr] = spr_shotshell;
  weapon[shotgun, bul_spe] = 10;
  weapon[shotgun, bul_acc] = 20;
  weapon[shotgun, bul_num] = 5;
  weapon[shotgun, bul_dam] = 25;
  weapon[shotgun, bul_dly] = 20;
  weapon[shotgun, bul_amo] = 50;
  weapon[shotgun, gun_eqp] = true
weapon_num += 1;
  weapon[rifle, bul_spr] = spr_Lbullet;
  weapon[rifle, bul_spe] = 20;
  weapon[rifle, bul_acc] = 1;
  weapon[rifle, bul_num] = 1;
  weapon[rifle, bul_dam] = 40;
  weapon[rifle, bul_dly] = 12;
  weapon[rifle, bul_amo] = 80;
  weapon[rifle, gun_eqp] = false
weapon_num += 1;
  weapon[pistol, bul_spr] = spr_bullet;
  weapon[pistol, bul_spe] = 10;
  weapon[pistol, bul_acc] = 5;
  weapon[pistol, bul_num] = 1;
  weapon[pistol, bul_dam] = 10;
  weapon[pistol, bul_dly] = 10;
  weapon[pistol, bul_amo] = 100;
  weapon[pistol, gun_eqp] = false
weapon_num += 1;
  weapon[machine_gun, bul_spr] = spr_bullet;
  weapon[machine_gun, bul_spe] = 12;
  weapon[machine_gun, bul_acc] = 8;
  weapon[machine_gun, bul_num] = 1;
  weapon[machine_gun, bul_dam] = 5;
  weapon[machine_gun, bul_dly] = 1;
  weapon[machine_gun, bul_amo] = 300;
  weapon[machine_gun, gun_eqp] = true
weapon_num += 1;

current_weapon = flamethrower;  //weapon to start on

This is how the player swaps to different weapons,if the weapons are available.

Key Press Event for E-key Key:
execute code:

//toggle through guns forwards
current_weapon = (current_weapon+1) mod weapon_num;

if(keyboard_check_pressed(ord('E')))
{
while(weapon[current_weapon,gun_eqp]==false)current_weapon=(current_weapon+1) mod weapon_num;
};

Key Press Event for Q-key Key:
execute code:

//toggle through guns backwards
current_weapon = ((current_weapon-1) mod weapon_num + weapon_num) mod weapon_num;

if(keyboard_check_pressed(ord('Q')))
{
while(weapon[current_weapon,gun_eqp]==false)current_weapon =((current_weapon-1) mod weapon_num + weapon_num) mod weapon_num;
};

the "wpn_drop" checks what weapon i'm holding so as soon as the player's current weapon has been replaced the "wpn_drop" now tells the "wpn_pickup" what weapon it should be next,wpn_drop(weapon drop: the weapon that was once held by the player) wpn_pickup(weapon pickup: the weapon that will soon be used by the player)

it also changes the "weapon[current_weapon,gun_eqp]=true" to "weapon[current_weapon,gun_eqp]=false" so that I won't be able to use it,because I don't have that weapon.

Weapon On the ground...
Create Event:
execute code:

wpn_pickup=random(6)

image_angle = random(360)
image_index = wpn_pickup
image_speed = 0

Collision Event with object obj_player_soldier:
execute code:

var wpn_drop;

if(keyboard_check_pressed(vk_space))
{
if wpn_pickup != current_weapon
{
image_angle = random(360);
wpn_drop = current_weapon
weapon[current_weapon,gun_eqp]=false;
current_weapon = wpn_pickup
weapon[current_weapon,gun_eqp]=true;
wpn_pickup=wpn_drop;
image_index = wpn_pickup;
}
};

Edited by 8feet, 04 February 2010 - 10:00 AM.

  • 0

#2 snowyowl

snowyowl

    GMC Member

  • New Member
  • 615 posts

Posted 03 February 2010 - 07:19 PM

You say "it doesn't work", but could you be more specific? What actually happens?

As far as I can tell, it should work fine, except that the player when picking up a new weapon doesn't switch guns to the new weapon. But it does appear in his inventory.
Wait... is that the problem? I don't know!
  • 0

#3 8feet

8feet

    GMC Member

  • GMC Member
  • 104 posts

Posted 04 February 2010 - 12:28 AM

You say "it doesn't work", but could you be more specific? What actually happens?

As far as I can tell, it should work fine, except that the player when picking up a new weapon doesn't switch guns to the new weapon. But it does appear in his inventory.
Wait... is that the problem? I don't know!


Ok here are the errors that I'm talking about...first no error message will appear....here is the bug if you go on top of a weapon and press space bar instead of getting that weapon you get a different weapon(example: my current_weapon is a flamethrower and my 2 other available weapons is a grenade launcher and a shotgun, the weapon on the floor is a rifle and I'll replace my flamethrower with the weapon on the floor BUT instead a get a machine gun instead!? BUT there are some times that I receive the right weapon...I get the feeling that this code only gives me RANDOM weapons!)

NOTE: I sometimes get the melee(knife) it must NOT because the melee is for the (right mouse button ONLY) but it sometimes go straight to the (left mouse button)
  • 0

#4 noobletherder

noobletherder

    GMC Member

  • New Member
  • 105 posts

Posted 04 February 2010 - 02:15 AM

Oh wow, this is very similar to a technique for gun switching I used for one of my games. I came to a quick conclusion when you said

my current_weapon is a flamethrower and my 2 other available weapons is a grenade launcher and a shotgun, the weapon on the floor is a rifle and I'll replace my flamethrower with the weapon on the floor BUT instead a get a machine gun instead!?


Now I can almost for sure say that it is an array location issue. What I mean is...

weapon[rifle,...] in the array rifle might equal 3. You may have a problem with the variable that controls the x location in that array; when trying to pickup the flamethrower (for the sake of example flamethrower = 4), instead of accessing weapon[4,...] where the flamethrower is, it accesses weapon[3,...] where the rifle is. So again what I am saying that the variable for the x location in the array is not being set equal to what you want it to. In this case I suggest looking around your code for something that could be setting the x location variable to something unwanted





OR
(the less likely case)

Since you did say you were new to game maker, I am going to assume you made a novice mistake such as having
weapon[melee, bul_spr] = spr_melee;
weapon[flamethrower, bul_spr] = spr_fire;
that as your actual code.

well in this case I am going to tell you now that when using arrays, it should look something like this array[x,y] where x and y are variables (having any name you want; which hold numerical values) or numerical values (1,2,3,4...).

So when you use the above code, game maker thinks "flamethrower" and "melee" are variables, and since I see no code setting them to any value game maker will think you are saying something like this.
weapon[0, bul_spr] = spr_melee;
weapon[0, bul_spr] = spr_fire;
This is because, since melee and flamethrower are not set equal to anything, they both hold a value of 0. And when game maker tries to access weapon[rifle, ...] it is really accessing weapon[0, ...] which explains the random gun thing (since you set all of the guns and things to weapon[0,...]).

If you need more help with arrays, I suggest you look here http://gmc.yoyogames...p?showtopic=105. It's a great tutorial.

Edited by noobletherder, 04 February 2010 - 02:16 AM.

  • 0

#5 8feet

8feet

    GMC Member

  • GMC Member
  • 104 posts

Posted 04 February 2010 - 02:28 AM

Oh wow, this is very similar to a technique for gun switching I used for one of my games. I came to a quick conclusion when you said

my current_weapon is a flamethrower and my 2 other available weapons is a grenade launcher and a shotgun, the weapon on the floor is a rifle and I'll replace my flamethrower with the weapon on the floor BUT instead a get a machine gun instead!?


Now I can almost for sure say that it is an array location issue. What I mean is...

weapon[rifle,...] in the array rifle might equal 3. You may have a problem with the variable that controls the x location in that array; when trying to pickup the flamethrower (for the sake of example flamethrower = 4), instead of accessing weapon[4,...] where the flamethrower is, it accesses weapon[3,...] where the rifle is. So again what I am saying that the variable for the x location in the array is not being set equal to what you want it to. In this case I suggest looking around your code for something that could be setting the x location variable to something unwanted





OR
(the less likely case)

Since you did say you were new to game maker, I am going to assume you made a novice mistake such as having
weapon[melee, bul_spr] = spr_melee;
weapon[flamethrower, bul_spr] = spr_fire;
that as your actual code.

well in this case I am going to tell you now that when using arrays, it should look something like this array[x,y] where x and y are variables (having any name you want; which hold numerical values) or numerical values (1,2,3,4...).

So when you use the above code, game maker thinks "flamethrower" and "melee" are variables, and since I see no code setting them to any value game maker will think you are saying something like this.
weapon[0, bul_spr] = spr_melee;
weapon[0, bul_spr] = spr_fire;
This is because, since melee and flamethrower are not set equal to anything, they both hold a value of 0. And when game maker tries to access weapon[rifle, ...] it is really accessing weapon[0, ...] which explains the random gun thing (since you set all of the guns and things to weapon[0,...]).

If you need more help with arrays, I suggest you look here http://gmc.yoyogames...p?showtopic=105. It's a great tutorial.


I use constants on my codes...
Resources/Change Global Game Settings/Constants

[Name = Value]

flamethrower = 0
grenade_launcher = 1
pistol = 2
shotgun = 3
rifle = 4
machine_gun = 5
melee = 6

I still can't figure it out.
  • 0

#6 torigara

torigara

    GMC Member

  • GMC Member
  • 6483 posts

Posted 04 February 2010 - 09:41 AM

wpn_pickup=random(6)

That will give you a decimal number like 4.68249713. Have you tried to make it a whole number?
wpn_pickup = floor(random(6));

When you assign a decimal number to image_index, it is rounded down on drawing the sprite. However, when you use it as an array index, it will get rounded to the nearest whole number. So, it can happen that you get a different weapon than it appears:

wpn_pickup=random(6) // Suppose that this yields 4.8
...
image_index = wpn_pickup // Then, this will show the sub-image 4
...
current_weapon = wpn_pickup
weapon[current_weapon,gun_eqp]=true; // However, you set weapon[5,gun_eqp]


By the way, there seems to be a few redundancy in your code.

Key Press Event for E-key Key:
(snip)
if(keyboard_check_pressed(ord('E')))

As you're in the E-key press event, there is no need to check if E key is pressed, isn't it?

current_weapon =((current_weapon-1) mod weapon_num + weapon_num) mod weapon_num

You don't need two mod's (as current_weapon never can go below -weapon_num.) It can be:
current_weapon = (current_weapon-1 + weapon_num) mod weapon_num

  • 0

#7 8feet

8feet

    GMC Member

  • GMC Member
  • 104 posts

Posted 04 February 2010 - 09:53 AM

EDIT: THANK YOU VERY MUCH LOZL:D!!! it works I tell you! It.....works....CREDITS for "torigara" Thank you again and GOD Bless!!!

wpn_pickup=random(6)

That will give you a decimal number like 4.68249713. Have you tried to make it a whole number?
wpn_pickup = floor(random(6));

When you assign a decimal number to image_index, it is rounded down on drawing the sprite. However, when you use it as an array index, it will get rounded to the nearest whole number. So, it can happen that you get a different weapon than it appears:

wpn_pickup=random(6) // Suppose that this yields 4.8
...
image_index = wpn_pickup // Then, this will show the sub-image 4
...
current_weapon = wpn_pickup
weapon[current_weapon,gun_eqp]=true; // However, you set weapon[5,gun_eqp]


By the way, there seems to be a few redundancy in your code.

Key Press Event for E-key Key:
(snip)
if(keyboard_check_pressed(ord('E')))

As you're in the E-key press event, there is no need to check if E key is pressed, isn't it?

current_weapon =((current_weapon-1) mod weapon_num + weapon_num) mod weapon_num

You don't need two mod's (as current_weapon never can go below -weapon_num.) It can be:
current_weapon = (current_weapon-1 + weapon_num) mod weapon_num


I never had thought of making it as a whole number before? thank you very much I'll try it out and thanks for the making the 2 mod to 1 Lolz;D! GOD Bless!!! I'll tell ya if the codes work.

Edited by 8feet, 04 February 2010 - 09:59 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users