Because of my memory issues, I eventually wrote a template when needing to use states (of course that is most all the time). Believe me, it took a long time for me to sort out something workable. I hope this is useful for some of you guys trying to use lua (especially lua in game development).
-- LUA Script - precede every function and global member with lowercase name of script + '_main'
gamedata = {}
local state = 0
local start = 0
function new_init(e)
start = 1
state = 1
end
function new_main(e)
if start == 1 then
start = 0
-- start code goes here that runs only once
else
if state == 1 then
--code for state 1 goes here
elseif state == 2 then
--code for state 2 goes here
elseif state == 3 then
--code for state 3 goes here
elseif state == 4 then
--code for state 4 goes here
end
end
end
Here is an example of a short script based on amenmoses' "playerfreeze" script.
-- LUA Script - precede every function and global member with lowercase name of script + '_main'
local U = require "scriptbank\\utillib"
gamedata = {}
local state = 0
local start = 0
local ename = ""
function plrfreeze_init_name(e,name)
ename = name
Include( "utillib.lua" )
dist = 100
delay = 15000
start = 1
state = 1
end
function plrfreeze_main(e)
if start == 1 then
start = 0
-- start code goes here that runs only once
else
if state == 1 then
--code for state 1 goes here
if U.PlayerLookingNear( e, dist, 30 ) then
StartTimer(e)
state = 2
end
elseif state == 2 then
--code for state 2 goes here
state = 3
timer = g_Time + delay
SetFreezePosition( g_PlayerPosX, g_PlayerPosY, g_PlayerPosZ )
elseif state == 3 then
--code for state 3 goes here
Prompt( "You have found " .. ename )
TransportToFreezePositionOnly()
if g_Time > timer then state = 4 end
elseif state == 4 then
--code for state 4 goes here
end
end
end
This also has an example of using one of his other fabulous scripts called "utillib.lua" as well as extracting the name of the entity.
Alienware Aurora R7 with SSD 256GB boot drive ( C: ) and a secondary drive ( D: ) that is 2TB
Intel(R) Core(TM) i7-8700 CPU @ 3.20GHz 3.19 with Intel Turbo-burst
Installed RAM 16.0 GB
64-bit operating system, x64-based processor
Windows 10 Home
NVIDIA GeForce GTX 1070 with 8192 MB GDDR5 and 8095 MB shared system memory