Jump to content

debugging error


Recommended Posts

I've been posting a lot here lately, and I am sorry if I'm getting annoying. But after a whole afternoon of trying to get my script to work I'd like to get some feedback.

After I learned of the existence of debugging I immediatly went ahead and tried it. I almost fixed my script, but there's one problem keeping it from working

Context:

  1. Player enters vehicle and an uzi is spawned and attached to the vehicle
  2. When the player presses left mouse button the uzi starts firing
  3. When the left mouse button is released the uzi should stop firing.
function hydraFunctions() 
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
        if getElementModel(vehicle) == 520 then
		    outputChatBox ("Hydra guns installed")
		    toggleControl ( "accelerate", true )
	        toggleControl ( "brake_reverse", true )
            toggleControl ( "vehicle_secondary_fire", false )
		    toggleControl ( "vehicle_fire", false )
		    setControlState ( "special_control_up", true)
		    toggleControl ( "special_control_down", false)
		    local x, y, z = getElementPosition(vehicle)
            local weapon = createWeapon("uzi", x, y, z)
			local weaponHydraR = setElementData(vehicle, "weaponHydraR", weapon, true)
		    attachElements ( weapon, vehicle, 0, 4, 1, 0, 0, 90)
			setWeaponClipAmmo(weapon, 500)
		    setWeaponFiringRate(weapon, 90)
        end
    end
end
addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), hydraFunctions )

function hydraFiring() 
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
        if getElementModel(vehicle) == 520 then
		    if getElementData(vehicle, "weaponHydraR", true ) then
		        setWeaponState(weapon, "firing") -- Can't seem to find the weapon
		        outputChatBox ("guns activated")
			end	
		end
	end
end	
  

function hydraStopping()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
	    if getElementModel(vehicle) == 520 then	
		    if getElementData(vehicle, "weaponHydraR", true ) then			
		        setWeaponState(weapon, "ready") -- Can't seem to find the weapon
		        outputChatBox ("guns deactivated")
			end	
		end	 
	end
end	

bindKey("mouse1", "down", hydraFiring)
bindKey("mouse1", "up", hydraStopping)
bindKey("lctrl", "down", hydraFiring)
bindKey("lctrl", "up", hydraStopping)
WARNING: 1947weapons\hydraweaponC.lua:41: Bad argument @ 'setWeaponState' [Expected weapon at argument 1, got nil]
WARNING: 1947weapons\hydraweaponC.lua:28: Bad argument @ 'setWeaponState' [Expected weapon at argument 1, got nil]

What I think is happening:

  1. The uzi spawns perfectly fine, but for some reason the following functions (hydraFiring & hydraStopping) cannot "detect" the uzi, hence the [Expected weapon at argument 1, got nil] error message. The uzi is simply not found.

Also, I read on the forums that the setElementData and getElementData should only be used with more important data (which this isn't really I guess.) Is there an alternative that I can use?

Thanks for your time,

 

-Noah

Edited by Noah_Antilles
Link to comment
  • Moderators

Using elementdata is fine, just make sure you disable the syncronization when you don't need it. ElementData
 is absolute not for important information. It is very foolish to save passwords or similar data in it.

bool setElementData ( element theElement, string key, var value [, bool synchronize = true ] )

 

You could also use:

bool setElementParent ( element theElement, element parent )
element getElementParent ( element theElement )  
table getElementChildren ( element parent [, string theType = nil ] ) 
element getElementChild ( element parent, int index ) 

 

Or learn tables.

Edited by IIYAMA
  • Like 1
Link to comment

The scope of the variable "weapon" is in the function hydraFunction as it's set to be a local variable, so hydraFiring and hydraStopping is outside the scope. You're are already saving the weapon element to an elementData, why not use that instead of the "weapon" variable? Try this:


function hydraFunctions() 
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
        if getElementModel(vehicle) == 520 then
		    outputChatBox ("Hydra guns installed")
		    toggleControl ( "accelerate", true )
	        toggleControl ( "brake_reverse", true )
            toggleControl ( "vehicle_secondary_fire", false )
		    toggleControl ( "vehicle_fire", false )
		    setControlState ( "special_control_up", true)
		    toggleControl ( "special_control_down", false)
		    local x, y, z = getElementPosition(vehicle)
            local weapon = createWeapon("uzi", x, y, z)
			local weaponHydraR = setElementData(vehicle, "weaponHydraR", weapon, true)
		    attachElements ( weapon, vehicle, 0, 4, 1, 0, 0, 90)
			setWeaponClipAmmo(weapon, 500)
		    setWeaponFiringRate(weapon, 90)
        end
    end
end
addEventHandler ( "onClientPlayerVehicleEnter", getLocalPlayer(), hydraFunctions )

function hydraFiring() 
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
        if getElementModel(vehicle) == 520 then
		    if getElementData(vehicle, "weaponHydraR", true ) then
		        setWeaponState(getElementData(vehicle, "weaponHydraR"), "firing") -- Can't seem to find the weapon
		        outputChatBox ("guns activated")
			end	
		end
	end
end	
  

function hydraStopping()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
	    if getElementModel(vehicle) == 520 then	
		    if getElementData(vehicle, "weaponHydraR", true ) then			
		        setWeaponState(getElementData(vehicle, "weaponHydraR"), "ready") -- Can't seem to find the weapon
		        outputChatBox ("guns deactivated")
			end	
		end	 
	end
end	

bindKey("mouse1", "down", hydraFiring)
bindKey("mouse1", "up", hydraStopping)
bindKey("lctrl", "down", hydraFiring)
bindKey("lctrl", "up", hydraStopping)

 

Edited by pa3ck
  • Like 1
Link to comment

Thank you for your tips and feedback. I was struggling so hard to find why the setWeaponState function didn't work ^^

Now I see the local weapon created in hydraFunctions was outside the scope and thus not recognized by the other functions.

I learned some great tips and tricks thanks to you all. Thanks again!

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...