Jump to content

Scripting woes


bovine3dom

Recommended Posts

No. 1; customised handling for a high-speed pursuit police car

function resourceStarted() 
        handling = createHandling() -- create a new handling element 
        handlingSetEngineAcceleration(handling, 40)-- change its acceleration property 
        handlingSetABS(handling, 1) 
        handlingSetMass(handling, 1000) 
        handlingSetTractionMultiplier(handling, 1.0) 
        handlingSetTractionBias(handling, 0.6) 
        handlingSetDriveType(handling, awd) 
        addDefaultHandling(597, handling) -- attach it to the SFPD ID 
    end 
addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), resourceStarted) 

Gives me the error "attempt to call global 'createHandling' ( a nil value ). I don't quite understand why this doesn't work, I pretty much copy-pasted it from the wiki :S.

No. 2; a stinger that you can release from a car (I'll make it police cars only later)

function releaseStingers ( thePlayer, commandName ) 
        local theVehicle = getPlayerOccupiedVehicle ( thePlayer ) 
        if (theVehicle ~= false) then 
            x,y,z = getElementPosition ( theVehicle ) -- stores the location of the car 
            rx, ry, rz = getVehicleRotation ( theVehicle ) -- stores the rotation of the car 
            setTimer ( delayedStingerCreation, 100, 1, stingdc) -- make a timer using the delay time from above to start the function "delayedStingerCreation" 
            else 
            outputChatBox ( "You fail!" , source, 255, 0, 0 ) 
        end 
    end 
addCommandHandler ( "sting", releaseStingers ) 
  
function delayedStingerCreation ( source, stingdc) 
        local z = z - 0.55 
        stingerCol = createColCuboid ( x, y, z, 1000.0, 1000.0, 500.0 ) 
        local rz = rz + 90 
        stingerObj = createObject( 2899, x, y, z, 0, 0, rz ) -- create the stinger (2892) 
        outputChatBox ( "Stingers released!" , source, 0, 255, 0 ) 
        setTimer (delayedStingerDeletion, 5000.0, 1, stingdd) 
    end 
  
function delayedStingerDeletion (source, stingdd) 
        destroyElement( stingerCol ) 
        destroyElement( stingerObj ) 
    end 
     
-- Stinger Collision! 
function stingerHit ( thePlayer, st ) 
        -- If in a vehicle, make it spin out of control and pop all the tyres 
        local theVehicle = getPlayerOccupiedVehicle ( thePlayer ) 
        local r1 = randInt ( 1, 2 ) 
        local r2 = randInt ( 1, 2 ) 
        local r3 = randInt ( 1, 1 ) 
        local r4 = randInt ( 1, 1 ) 
        setVehicleWheelStates ( theVehicle, r1, r2, r3, r4 ) 
        local velocityx,velocityy,velocityz = getElementVelocity ( thePlayer ) 
        local speed = (velocityx^2 + velocityy^2 + velocityz^2)^(0.5) 
        local turnvelocity = speed/5 
        setVehicleTurnVelocity ( theVehicle, 0, 0, turnvelocity ) 
    end 
addEventHandler ( "onColShapeHit", getRootElement(), stingerHit ) -- When you hit a collision box, perform function "stingerHit"  

Whilst this works, what can I use other than getRootElement() because if I change it to stingerCol, nothing happens - and getRootElement() is highly impractical, because all collision boxes trigger the stinger collision.

No 3; A police job

  
-- Logging into an account 
function onLogIn() 
        local playerAccount = getClientAccount ( source ) 
        local x, y, z = getElementPosition ( source ) 
            if ( playerAccount ) then 
                local playerMoney = getAccountData ( playerAccount, "rp.money" ) 
                if ( playerMoney ) then 
                    setPlayerMoney ( source, playerMoney ) 
                end 
                local job = getAccountData ( playerAccount, "job" ) 
                if ( job == "cop" ) then 
                        spawnPlayer ( thePlayer, x, y, z, 90, randInt (280, 284), 0, 0, 0, Police ) 
                        giveWeapon ( source, 22, 400 ) -- Gives them a pistol with 400 ammo 
                        giveWeapon ( source, 17, 5 ) -- Gives them 5 teargas 'nades 
                        giveWeapon ( source, 33, 40 ) -- Gives the a rifle with 40 ammo 
                end 
            end 
    end 
addEventHandler ( "onClientLogin", getRootElement(), onLogIn ) 
  
-- stop noobs stealing cop cars 
function policeLock ( player, seat, jacked ) --when a player enters a vehicle 
    local policeVehicles = { [598]=true,[596]=true,[597]=true,[599]=true } 
    local playerAccount = getClientAccount ( source ) 
    local job = getAccountData ( playerAccount, job ) 
    if ( policeVehicles[getVehicleID(source)] ) and ( job ~= cop ) then -- if the car is a police car, and the player isn't a cop, don't let 'em in. 
        cancelEvent() 
        outputChatBox ( "Only policemen can enter police cars!", player ) --and tell the player why 
    end 
end 
addEventHandler ( "onVehicleStartEnter", getRootElement(), policeLock ) 
  
function setJob ( thePlayer, commandName, targetPlayerName, job) 
        local playerAccount = getClientAccount ( targetPlayerName ) 
        if ( playerAccount ) then 
            setAccountData (playerAccount, "job", job) 
        end 
    end 
addCommandHandler( "setjob", setJob) 
  
  

The /setjob [playername] [job] command doesn't work, and even if I go into the account data and set it through there, when I log in, my skin doesn't change and I don't get any guns - and I still can't enter cop cars.

Thanks in advance and I apologise for my indoubtedly buggy and poorly written code, but you must forgive me; I only started coding on Saturday.

I'm using DP 2.3, couldn't figure out how to get nightly builds to work.

Edited by Guest
Link to comment

Are you testing these scripts using DP2.3 or the nightly builds?

1. Handling functions have been taken out for some reason so they don't work yet.

2.You need to add the eventhandler AFTER you created the Colshape (before makes no sense as you can't attach a handler to a variable that doesn't exist) so you need to place it in the delayedStingerCreation function after line 15

3. the function getClientAccount needs a player/client element as argument but you are passing the string targetPlayerName

Just use getPlayerFromNick:

local playerAccount = getClientAccount ( getPlayerFromNick(targetPlayerName) ) 

Link to comment

Thanks!

I'm using DP2.3, my stingers work great now - and ranged chat doesn't set them off ;)

However, although your suggestion for 3. was definitely part of the problem - /setjob now works - it still doesn't make me a cop when I log in and for some reason, my accounts.xml gets wiped every now and then.

EDIT: I get bad "client" pointer @ line 4 and bad "element" pointer @ line 5

Link to comment

it's a bug in DP2.3 that the accounts.xml gets wiped out if you use setAccountData for a guest account (people who are not logged in have guest accounts)

Just make a simply if check like this to prevent it:

if isGuestAccount(getClientAccount(getPlayerFromNick(targetPlayerName))) == false then 
    --do blah and blubb 
else 
    outputChatBox ("ERROR: Player is not logged in!") 
end 

Link to comment

Thanks Mr.Hankey, I was wondering why the accounts were dissappearing!

Unfortunately, not all is well - on occasion, the stingers decide to sting the person who released them when someone else runs over the stingers - and sometimes the time is set to 0:00 when someone runs over the stingers. Also, the "Stingers Released!" is global i.e. everyone can see it. - Why?

-- Stinger Release 
function releaseStingers ( thePlayer, commandName ) 
        local theVehicle = getPlayerOccupiedVehicle ( thePlayer ) 
        if (theVehicle ~= false) then 
            x,y,z = getElementPosition ( theVehicle ) -- stores the location of the car 
            rx, ry, rz = getVehicleRotation ( theVehicle ) -- stores the rotation of the car 
            setTimer ( delayedStingerCreation, 100, 1, stingdc) -- make a timer using the delay time from above to start the function "delayedStingerCreation" 
            else 
            outputChatBox ( "You fail!" , source, 255, 0, 0 ) 
        end 
    end 
addCommandHandler ( "sting", releaseStingers ) 
  
function delayedStingerCreation ( source, stingdc) 
        local z = z - 0.55 
        stingerCol = createColCuboid ( x, y, z, 1000.0, 1000.0, 500.0 ) 
        addEventHandler ( "onColShapeHit", stingerCol, stingerHit ) -- When you hit a collision box, perform function "stingerHit" 
        local rz = rz + 90 
        stingerObj = createObject( 2899, x, y, z, 0, 0, rz ) -- create the stinger (2892) 
        outputChatBox ( "Stingers released!" , source, 0, 255, 0 ) 
        setTimer (delayedStingerDeletion, 5000.0, 1, stingdd) 
    end 
  
function delayedStingerDeletion (source, stingdd) 
        destroyElement( stingerCol ) 
        destroyElement( stingerObj ) 
    end 
     
-- Stinger Collision! 
function stingerHit ( thePlayer, st ) 
        -- If in a vehicle, make it spin out of control and pop all the tyres 
        local theVehicle = getPlayerOccupiedVehicle ( thePlayer ) 
        local r1 = randInt ( 1, 2 ) 
        local r2 = randInt ( 1, 2 ) 
        local r3 = randInt ( 1, 1 ) 
        local r4 = randInt ( 1, 1 ) 
        setVehicleWheelStates ( theVehicle, r1, r2, r3, r4 ) 
        local velocityx,velocityy,velocityz = getElementVelocity ( thePlayer ) 
        local speed = (velocityx^2 + velocityy^2 + velocityz^2)^(0.5) 
        local turnvelocity = speed/5 
        local spin = randInt ( 0, 1 ) 
        if (spin == 1 ) then 
        setVehicleTurnVelocity ( theVehicle, 0, 0, turnvelocity ) 
        else  
        local turnvelocity = turnvelocity * -1 
        setVehicleTurnVelocity ( theVehicle, 0, 0, turnvelocity ) 
        end 
    end 

Link to comment

Ok let me see:

- Line 9: Source doesn't exist here, only in the case of an event. (or well, it does exist, but it's not really usable) Solution: Replace with thePlayer

- Line 14: Advice: Don't use source in the function parameters. Might help fixing the message in line 20.

- Line 32: onColShapeHit is triggered whenever an element comes into the colshape, not only when a player enters. This means it's triggered whenever a vehicle drives over, and when a player enters the shape. Don't know if it solves anything here though.

The problem in which it pops the creator's tires, are you sure it happens whenever someone else drives over? And not when the creator drives over it himself? :P

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...