how do i skip a certain weapon in this selection scroll wheel script when the weapon has no more ammo for it?

if event.is_action_pressed("mouseDOWN") && canswitchweapon:
		
		if !inventoryOpen:
			open_Inventory()
			hideweapon()
			inventoryOpen = true
		
		var now = Time.get_unix_time_from_system()
		if (now > nextselect) :
			var lastindex = weaponIndex
			nextselect = now + .1
			weaponIndex += 1
			
			if weaponIndex > weapons.size()-1:
				weaponIndex = 0
			var asp = AudioStreamPlayer.new()
			self.add_child(asp)
			asp.stream = hideweaponSound
			asp.play()
			if !asp.is_playing():
				asp.queue_free()
			weaponInventory.get_child(weaponIndex).modulate = selectColor
			weaponInventory.get_child(lastindex).modulate = normalColor
			currentWeapon = weaponIndex
		#hidecurrentweapon
		
	if event.is_action_pressed("mouseUP") && canswitchweapon:
		
		if !inventoryOpen:
			open_Inventory()
			hideweapon()
			inventoryOpen = true
		
		var now = Time.get_unix_time_from_system()
		if (now > nextselect) :
			var lastindex = weaponIndex
			nextselect = now + .1
			
			weaponIndex -= 1
			
			if weaponIndex < 0:
				weaponIndex = weapons.size()-1
			
			var asp = AudioStreamPlayer.new()
			self.add_child(asp)
			asp.stream = hideweaponSound
			asp.play()
			if !asp.is_playing():
				asp.queue_free()
			
			weaponInventory.get_child(weaponIndex).modulate = selectColor
			weaponInventory.get_child(lastindex).modulate = normalColor
			currentWeapon = weaponIndex

You haven't said how you're storing ammunition, but it's basically a matter of incrementing the selection twice if the ammunition is zero. You'll want to check if the index is out of range each time you increment, of course.

  • DJM replied to this.

    theres this line in your code (in both the mouseUp and mouseDown sections) where you check the size of the "weapons" array. In stead of just checking it to see whether you should overflow or not; also add a check there to see if the weapon still contains ammo, so replace that with something like this:

    var weaponFound = false
    while(!weaponFound):
        if weaponIndex == weapons.size():
            weaponIndex = 0
        if weapons[i].ammo == 0:
            weaponIndex += 1
        else:
            weaponFound = true

    And for mouseUp we search in the opposite direction ofcourse:

    var weaponFound = false
    while(!weaponFound):
        if weaponIndex == 0:
            weaponIndex = weapons.size()
        if weapons[i].ammo == 0:
            weaponIndex -= 1
        else:
            weaponFound = true

    This will fail if all your weapons are out of ammo as it will be stuck in the while-loop, forever. You can fix this by having one weapon have infinite ammo.

    • DJM replied to this.

      Feivisto ive tried it, that code crashes my game.

      duane
      im setting them as variables
      and i also have a reload func to deplete them

      @export var weapons : Array[NodePath]
      @export var weaponsammo : Array[int]
      var currentweaponammosize : int
      @export var weaponIcons : Array[CompressedTexture2D]
      @export var currentWeapon = 0
      @export var dynamitesticks = 5
      @export var selectColor : Color
      @export var normalColor : Color
      @export var unavailableColor : Color
      var weaponIndex = 0

      and in ready i setup the inventory

      #setup weaponinventory
      	for slot in weaponIcons.size():
      		var weaponslot = TextureRect.new()
      		weaponInventory.add_child(weaponslot)
      	for icon in weaponInventory.get_children():
      		var iconindex = icon.get_index()
      		icon.set_texture(weaponIcons[iconindex])
      		icon.modulate = normalColor
      	
      	weaponIndex = currentWeapon
      	weaponIcon.set_texture(weaponIcons[currentWeapon])
      	get_node(weapons[currentWeapon]).start()
      	get_node(weapons[currentWeapon]).totalAmmo = weaponsammo[currentWeapon]
      	weaponInventory.visible = false
      	inventoryOpen = false

      ideally i would like to make the inventory dynamic, so u start with basic the first 2 weapons (knife and pistol)and pickup others along the way. so i would need code to manipulate the inventory and add them. once a weapon is added it should stay in the inventory, but when its ammo is depleted it should be unavailable to select.

        DJM ive tried it, that code crashes my game.

        So what error did you get and what changes did you make? You obviously have to change the name of the ammunition variable.

        • DJM replied to this.

          duane
          i dont get an error, my computer freezes and i need to force reboot

          if event.is_action_pressed("mouseDOWN") && canswitchweapon:
          		
          		if !inventoryOpen:
          			open_Inventory()
          			hideweapon()
          			inventoryOpen = true
          		
          		var now = Time.get_unix_time_from_system()
          		if (now > nextselect) :
          			var lastindex = weaponIndex
          			nextselect = now + .1
          			
          			var weaponFound = false
          			while(!weaponFound):
          				if weaponIndex == weapons.size():
          					weaponIndex = 0
          				if get_node(weapons[currentWeapon]).totalAmmo == 0:
          					weaponIndex += 1
          				else:
          					weaponFound = true

          probably cause weapon 1 is a 'knife' and has a variable of totalAmmo of zero

          Godot shouldn't be freezing your operating system. If you enter an endless loop, just click the stop button on your godot application to stop the program. If you're having to reboot, you have serious issues with something else in your system.

          You're going to have to distinguish between weapons that use ammunition and those that don't. You might put -1 in melee weapons, to show that. Checking for zero should then select them as well. You can check for >0 in your functions that decrement ammunition.

          • DJM replied to this.

            duane

            ive changed knife ammo to -1
            godot doesnt freeze my os , i had to press F8 to stop the scene

            
            var weaponFound = false
            	while(!weaponFound):
            				if weaponIndex == weapons.size():
            					weaponIndex = 0
            				if get_node(weapons[currentWeapon]).totalAmmo == 0:
            					weaponIndex += 1
            				else:
            					weaponFound = true

            this line gives error>
            if get_node(weapons[currentWeapon]).totalAmmo == 0:
            (array and int)
            and the scrolling isn't working anymore

              That error message doesn't look complete. You can right-click on the error line in the errors tab and select "copy error" to copy the whole message, then you can paste it here. I don't see anything obviously wrong with the code you've posted.

              Make sure that weapons[] contains the right information. This is a good time to learn about godot's debugging tools. The documentation isn't very good for that, but GDQuest can help. A breakpoint in the scroll loop will let you step through the process (and prevent the game from looping infinitely).

              If you have a way of posting the project (or a subset with the problem) somewhere, that would also help. Github is a good option if your project isn't proprietary.

              • DJM replied to this.

                DJM
                I would've changed the knife ammo to 1 and add a variable to the knife to see whether or not it has infinite ammo. That way you can make it so ammo is never subtracted when "firing" the knife. And the script that checks for ammo in guns will always at least find the knife when switching weapons.
                Looking at your script it seems that if get_node(weapons[currentWeapon]).totalAmmo == 0 throws an error because you want to use weaponIndex in stead of currentWeapon. If you step through the code you should be able to see that weaponIndex is an integer and currentWeapon is probably a weapon object/script.

                • DJM replied to this.

                  Feivisto ive tried setting ammo to 1, scrolling isnt working anymore, even if i use weaponIndex

                  duane all assets where made by me so i can share my project.
                  i dont really have much experience with how github works , but i was thinking of making my fps a community project where everyone can contribute to the game,
                  all i want is to be the projects' art lead.