Author Topic: Switching weapons  (Read 3049 times)

gwal

  • Regular
  • **
  • Posts: 69
  • Game Progress: Outer Space / Items
    • View Profile
Switching weapons
« on: 2006-07-19, 01:41:36 PM »
I'm using a script to switch the player's weapon, but its not working and i don't know why.

Code: [Select]
Sub Player_OnControllerMove(OldActions, NewActions)
   If (Not OldActions) And NewActions And ACTION_BUTTON1 Then Shoot
   If (Not OldActions) And NewActions And ACTION_BUTTON2 Then SelectShot
End Sub
Sub Shoot 'Determines which type of sprite to create'
Dim Weapon
Weapon = ProjectObj.GamePlayer.InvQuantityOwned(0)
  If Weapon = 1 Then DoFireButton0
  If Weapon = 2 Then DoFireButton1
  If Weapon = 3 Then DoFireButton2
End Sub

Sub SelectShot 'Switches between weapons'
Dim Weapon
Weapon = ProjectObj.GamePlayer.InvQuantityOwned(0)
Weapon = Weapon +1
If Weapon = 4 Then Weapon = 1
End Sub


EDIT: I looked at the code again and I fixed the problem by doing this:
Code: [Select]
Sub SelectShot 'Switches between weapons'
ProjectObj.GamePlayer.InvQuantityOwned(0) = ProjectObj.GamePlayer.InvQuantityOwned(0) + 1
If ProjectObj.GamePlayer.InvQuantityOwned(0) = 4 Then ProjectObj.GamePlayer.InvQuantityOwned(0) = 1 'ProjectObj.GamePlayer.InvQuantityOwned(0)
End Sub
« Last Edit: 2006-07-19, 02:15:29 PM by gwal »

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Switching weapons
« Reply #1 on: 2006-07-19, 03:14:09 PM »
Are you using this variable outside the script?  i.e. do you check the quantity of inventory item 0 during a special function in the game at all?  If not, there is really no reason to even use an in-game inventory item.  Just use a variable:

Code: [Select]
Dim weapon 'Defined in global scope so that it's available to all subs.
Sub Player_OnControllerMove(OldActions, NewActions)
   If (Not OldActions) And NewActions And ACTION_BUTTON1 Then Shoot
   If (Not OldActions) And NewActions And ACTION_BUTTON2 Then SelectShot
End Sub

Sub Shoot 'Determines which type of sprite to create'
  If Weapon = 1 Then DoFireButton0
  If Weapon = 2 Then DoFireButton1
  If Weapon = 3 Then DoFireButton2
End Sub

Sub SelectShot 'Switches between weapons'
   Weapon = (Weapon + 1) Mod 4   'Using Modulo Arithmetic reduces this to one line
End Sub
Edward Dassmesser

gwal

  • Regular
  • **
  • Posts: 69
  • Game Progress: Outer Space / Items
    • View Profile
Re: Switching weapons
« Reply #2 on: 2006-07-20, 02:12:38 PM »
I'm using weapon as an item so it can display what weapon the player is using at the bottom of the screen. This is what my new script looks like:
Code: [Select]
Dim Weapon
Weapon = 1

Sub Player_OnControllerMove(OldActions, NewActions)
   If (Not OldActions) And NewActions And ACTION_BUTTON1 Then Shoot
   If (Not OldActions) And NewActions And ACTION_BUTTON2 Then SelectShot
End Sub

Sub Shoot 'Determines which type of sprite to create'
  If Weapon = 1 Then DoFireButton0
  If Weapon = 2 Then DoFireButton1
  If Weapon = 3 Then DoFireButton2
End Sub

Sub SelectShot 'Switches between weapons'
Weapon = (Weapon + 1) Mod 4
ProjectObj.GamePlayer.InvQuantityOwned(0) = Weapon
End Sub
Anyways, I started making this game after playing Half-Life 2. I'm trying to recreate the gravity gun in gamedev. Right now, when the player shoots certain objects with the gun, a cursor appears on the screen thats lets you drag and reposition the objects with the mouse. Except for the mouse part, it's mostly done with special functions, so I'm transfering the functions into the script. (That way I won't have to redo 15 special functions for every map).