Jump to content

Police Gates bug


CallumD

Recommended Posts

I made this Police Gates script but it has a bug; if you enter, leave, and then enter the colShape before it has finished moving or if 2 cops enter/leave at once it goes past the turning point. Does anybody know how to prevent it going past the point? The server-side script below (the part that moves the gate):

function makePoliceGate ()
barrier = createObject ( 968, 1544.7355957031, -1623.9633789063, 12.950497627258, 0, -90, 90 )
end
addEventHandler ( "onResourceStart", getRootElement(), makePoliceGate )
 
function openPoliceGate ()
moveObject ( barrier, 1000, 1544.7355957031, -1623.9633789063, 12.950497627258, 0, 90, 0 )
end
addEvent ( "openPoliceGate", true )
addEventHandler ( "openPoliceGate", getRootElement(), openPoliceGate )
 
function closePoliceGate ()
moveObject ( barrier, 1000, 1544.7355957031, -1623.9633789063, 12.950497627258, 0, -90, 0 )
end
addEvent ( "closePoliceGate", true )
addEventHandler ( "closePoliceGate", getRootElement(), closePoliceGate )

Link to comment

Sounds like you need to look at some basic Lua tutorials, but here's a quick and dirty variables 101:

This is how you define a variable for example:

someIntegerVariable = 42
someFloatVaraible = 3.14159
someStringVariable = "I am a string"
aPlayerElementVariable = getLocalPlayer()

and they are variables because you can change them at any time:

setMyVar = 5  --Variable now contains 5
setMyVar = 7  --now contains 7, 5 is forgotten
setMyVar = nil   --destroys the variable, 7 is forgotten and setMyVar no longer uses memory

Slightly more advanced, but I'd consider worth learning, is the local variable.

Setting a variable "Local" restricts it's access to only the current block. (and children of) (a block is usually a function, or file if defined outside a function, or a if...then/end section - look at "scope" below)

An example explains this easiest:

(ignore load and execution order here, it's just for demonstration - assume all functions have run once)

FileA.lua:

myGlobalVar = true
local myLocalVar = "a"
 
function myFunction(var)
   functionVar = "b"
local functionLocalVar = "c"
--here, myGlobalVar == true,  mylocalVar == "a",  var == "e" (called from below) functionVar == "b",  functionLocalVar = "c", 
--andAnother == nil  (even if it had been executed, it's local to myOtherFunction only),  var2 = nil (passed variables are local to their called function)
end
 
function myOtherFunction(var2)
local andAnother = "d"
--here, myGlobalVar == true,  mylocalVar == "a",  var == nil, functionVar == "b" (it wasn't local, so it's accessible),  functionLocalVar = nil,
--andAnother == "d",  var2 = "f"
end
 
myFunction("e")
myOtherFunction("f")
 
--here, myGlobalVar == true,  mylocalVar == "a",  var == nil, functionVar == "b",  functionLocalVar = nil,
--andAnother == nil,  var2 = nil

FileB.lua:

... (code here)
--here, myGlobalVar == true,  mylocalVar == nil (local defined outside a function in the other file, local to that specific file),  var == nil, 
--functionVar == "b",  (assuming myFunction has been called before arriving here)
--functionLocalVar = nil, andAnother == nil,  var2 = nil

Local variable names can use the same name in different scopes and they won't interfere with each other - e.g. you can have a local variable named 'local myVar' in 2 seperate functions and they won't overwrite each other.

Variable scope extends further, you can only have one "Global" version of a variable, but multiple "local" variables defined by scope (nesting: functions, then - else/elseif/end, do - end,repeat - until) - a quick demonstration from the Reference manual:

x = 10                -- global variable
do                    -- new block
local x = x         -- new 'x', with value 10
print(x)            --> 10
      x = x+1
do                  -- another block
local x = x+1     -- another 'x'
print(x)          --> 12
end
print(x)            --> 11
end
print(x)              --> 10  (the global one)

You're not likely to run into a variable name clash though so I won't explain it further.

Functions can be local too, and follow the same rules.

Not sure how much all of that will be of use to you, but I tried to make it easier to understand than the lua manual. :)

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