User Tools

Site Tools


lgscript:third_script

Third Script

------ CountedRelay.lua ------
-- script services
local propsrv = lgs.PropertySrv
local linksrv = lgs.LinkSrv
-- some variables used by our script
-- there's no need for global variables, so use local as much 
-- as possible, your script will perform better
local limit = 3
local count = 0
 
-- Setup the script when the mission begins
function Sim(msg)
  if msg.fStarting then
    -- if the property isn't set, the default of 3 will be used
    if propsrv.Possessed(script.objid, "ScriptTiming") then
      limit = propsrv.Get(script.objid, "ScriptTiming")
    end
    count = 0
  end
  return true
end
 
-- Each TurnOn will increment the counter
function TurnOn(msg)
  count = count + 1
  if count >= 3 then
    count = 0
    linksrv.BroadcastOnAllLinks(script.objid, "TurnOn", "ControlDevice")
  end
  return true
end
 
-- A TurnOff decrements the counter
function TurnOff(msg)
  count = count - 1
  if count < 0 then count = 0 end
  return true
end

This script is a relay with a counter. A property is used so it can be configured in Dromed. Every property has a name to be used by scripts, but this isn’t the name you see in Dromed. Many properties are made up of multiple fields, so you also need to know the field name. Be aware that many property fields have spaces in the names; you must include these spaces when accessing the fields. The ScriptTiming property is seen in Dromed as Script→Timing. It’s a simple property, so no field name is necessary.

You may notice that all the examples end a handler with return true. If you use SendMessage to send a script message, the return value from the method call will be the value returned by the message handler. The type of value returned can be any of the basic types: number, string, boolean, vector, or nil. Basic types are also used as message data, property and link values, and script variables.

This example will not work as intended. If the game is saved then restored, the script will be loaded from scratch. The value of the count variable is lost. When you need a value to be preserved with a saved game, you must use a script variable. This is the TurnOn handler rewritten to use a script variable.

-- Each TurnOn will increment the counter
function TurnOn(msg)
  local count = script:GetScriptData("count") + 1
  if count >= 3 then
    count = 0
    linksrv.BroadcastOnAllLinks(script.objid, "TurnOn", "ControlDevice")
  end
  script:SetScriptData("count",count)
  return true
end

The name of a script variable can be anything you like. The variable names from other scripts are kept seperate so you don’t have to worry about conflicting names. This example uses a temporary local variable to be the most efficient. Reading and writing script variables can be slow.

Using method names can be cumbersome, so there is another way of accessing script variables. If you attempt to access an index of the script object that doesn’t exist, then a script variable with that name will be looked for. So the handler could also be written this way.

-- Each TurnOn will increment the counter
function TurnOn(msg)
  local count = script.count + 1
  if count >= 3 then
    count = 0
    linksrv.BroadcastOnAllLinks(script.objid, "TurnOn", "ControlDevice")
  end
  script.count = count
  return true
end

Even though Lua is a case-sensitive language, the Dark Engine ignores case for most names. This means the value of script:GetScriptData("COUNT") is the same as script:GetScriptData("Count"). (Which is the same as script.CouNt even.) This goes for the names of messages as well. LgScript will ignore the case of function names when looking for a message handler function, or the name of a script variable. In all other respects, case is significant. Such as local variables or library functions.

lgscript/third_script.txt · Last modified: 2011/05/15 19:22 by telliamed