The F table is useful for defining functions in the init code which you can then use in other components. I'll often put commonly used snippets in as quick reference functions instead of typing out the same few lines of code each time.56independent_actual wrote: ↑Sun Mar 20, 2022 09:45Do i have to use F.x for every component or is F neglected in LuaATC tracks?
Eg: some of my functions require a string to be returned, regardless of if get_rc() actually returns a string or nil
Spoiler
Code: Select all
F.get_rc_safe = function()
return get_rc() or ""
end
Spoiler
Code: Select all
F.has_rc = function(query,rc_list)
if rc_list == "" or query == nil or query=="" then return false end
if not rc_list then rc_list = F.get_rc_safe() end
for word in rc_list:gmatch("[^%s]+") do
if word == query then return true end
end
return false
end
Spoiler
Code: Select all
F.remove_rc = function(rc_list,arrow_mode)
-- rc_list MUST be a table of rc codes to remove
-- eg: {"rc1","rc2"}
-- Arrow Modes:
-- true: with arrow direction
-- false: against arrow direction
-- nil: ignores arrow direction
if not event.train then return end
if (arrow_mode == nil) or (atc_arrow == arrow_mode) then
local rc = F.get_rc_safe()
rc_list = rc_list or {}
-- ensure rc-remove table can be read
local rc_remove = {}
for _,v in pairs(rc_list) do
rc_remove[v] = true
end
-- remove codes from train's rc
local reinsert = {}
for token in rc:gmatch("[^%s]+") do
if not rc_remove[token] then
table.insert(reinsert,token)
end
end
-- insert new string to train's rc
set_rc(table.concat(reinsert," "))
end
return reinsert
end
Spoiler
Code: Select all
--F.arrive and F.depart would be defined before this of course
F.station = function(stn_code,dir,line,delay)
if event.train then
F.arrive(stn_code,dir,line)
interrupt(delay,"depart")
elseif event.int and event.msg=="depart" then
F.depart(stn_code,dir,line)
end
end
-- you'd then just need to put variations of this at each lua-track for each platform:
F.station("Main Station","South","Express",10)