Hi, I have a question. I’m creating mods for Czech railcars, and I had the idea of creating a realistic information system for them, similar to the one used on railcars in the Czech Republic. Here’s what I’m thinking: a script that would display this information. So far, though, I haven’t quite figured out how to create the script—what to do and how to do it. I’ve tried some code with the help of AI, but it doesn’t really work. The display on the railcar should show the train number, car number, departure station, stations the train passes through, and the destination station. Some displays also show the line number, but I understand that this is done using LabelList (LINE_NAME). At the same time, it should be able to scroll the text continuously in a loop if the words are too long. See the image of the information panel from the Bdmpee233 car.
[Blockierte Grafik: https://www.vagonweb.cz/fotogalerie/foto/201301/IMG_7952.jpg]
I'm also attaching the script that the AI generated for me; unfortunately, I've been tinkering with it for a while now, and nothing works.
if not cdWagonNumbers then
cdWagonNumbers = {}
end
local cdWagonModels = {
["CD_Amz138.mdl"] = "KLASIKA_1",
["CD_ABmz346.mdl"] = "KLASIKA_1",
["CD_Ampz146-005.mdl"] = "KLASIKA_1",
["CD_Bdmpee233-009.mdl"] = "KLASIKA_2",
["cd_Bbdgmee236.mdl"] = "KLASIKA_2",
["CD_ARmpee832.mdl"] = "KLASIKA_2",
["CD_Bmz225.mdl"] = "KLASIKA_2",
["CD_Bmz226.mdl"] = "KLASIKA_2",
["CD_Bmz230.mdl"] = "KLASIKA_2",
["CD_Bmz232_020.mdl"] = "KLASIKA_2",
["CD_Bmz232_022.mdl"] = "KLASIKA_2",
["CD_Bmz232_023.mdl"] = "KLASIKA_2",
["CD_Bmz234.mdl"] = "KLASIKA_2",
["CD_Bmz235.mdl"] = "KLASIKA_2",
["CD_Bmz241_033.mdl"] = "KLASIKA_2",
["CD_Bmz245_002.mdl"] = "KLASIKA_2",
["CD_Bmz245_020.mdl"] = "KLASIKA_2",
["CD_Bmz245_020M.mdl"] = "KLASIKA_2",
["CD_Bcmz834.mdl"] = "KLASIKA_2",
["cd_rj_afmpz.mdl"] = "RAILJET",
["cd_rj_bmpz1.mdl"] = "RAILJET",
["cd_rj_armbz.mdl"] = "RAILJET",
["cd_rj_bmpz2.mdl"] = "RAILJET",
}
if not cdWagonNumbers then
cdWagonNumbers = {}
end
local function formatViaStation(rawName)
if not rawName or rawName == "" then return "" end
local cleanName = rawName:gsub("%b()", ""):match("([^-]+)") or rawName
cleanName = cleanName:gsub("^%s*(.-)%s*$", "%1")
return string.upper(cleanName:gsub("Nádraží", "n."):gsub("Hlavní", "hl."))
end
local function processTrainData()
local payload = {}
local trains = game.interface.getEntities({type = "TRAIN"})
for _, trainId in ipairs(trains) do
local trainState = game.interface.getEntity(trainId)
if trainState and trainState.line and trainState.vehicles then
local lineState = game.interface.getEntity(trainState.line)
local targetStationName = ""
if lineState and lineState.stops and #lineState.stops > 0 then
local lastStop = game.interface.getEntity(lineState.stops[#lineState.stops].station)
if lastStop then targetStationName = formatViaStation(lastStop.name) end
end
local originStationName = ""
if lineState and lineState.stops and #lineState.stops > 0 then
local firstStop = game.interface.getEntity(lineState.stops[1].station)
if firstStop then originStationName = formatViaStation(firstStop.name) end
end
local viaStationsTable = {}
if lineState and lineState.stops then
for i = 1, #lineState.stops do
local stopEntity = game.interface.getEntity(lineState.stops[i].station)
if stopEntity then
local currentStopName = formatViaStation(stopEntity.name)
if currentStopName ~= targetStationName and currentStopName ~= originStationName then
table.insert(viaStationsTable, currentStopName)
end
end
end
end
local routeViaText = table.concat(viaStationsTable, " - ")
local vlakName = ""
local linkaName = ""
if trainState.name and string.find(trainState.name, "#") then
vlakName, linkaName = string.match(trainState.name, "([^#]+)#([^#]+)")
vlakName = vlakName:gsub("^%s*(.-)%s*$", "%1"):upper()
linkaName = linkaName:gsub("^%s*(.-)%s*$", "%1"):upper()
elseif trainState.name then
vlakName = trainState.name:upper()
end
local startKlasika2, startKlasika1, startRailjet = 365, 372, 21
if lineState and lineState.name and (string.find(lineState.name, "Polsko") or string.find(lineState.name, "Ex32")) then
startKlasika2, startKlasika1 = 344, 352
end
local offset2nd = 0
local offset1st = 0
local offsetRj = 0
for _, vehicleId in ipairs(trainState.vehicles) do
local vehicleData = game.interface.getEntity(vehicleId)
if vehicleData and vehicleData.fileName then
local mdlName = vehicleData.fileName:match("([^/]+)$")
local wagonType = cdWagonModels[mdlName]
if wagonType then
local finalNumber = ""
if wagonType == "KLASIKA_2" then
finalNumber = tostring(startKlasika2 + offset2nd)
offset2nd = offset2nd + 1
elseif wagonType == "KLASIKA_1" then
finalNumber = tostring(startKlasika1 + offset1st)
offset1st = offset1st + 1
elseif wagonType == "RAILJET" then
finalNumber = tostring(startRailjet + offsetRj)
offsetRj = offsetRj + 1
end
if not cdWagonNumbers[vehicleId] then
cdWagonNumbers[vehicleId] = finalNumber
end
payload[tostring(vehicleId)] = {
vName = vlakName,
oName = originStationName,
rText = routeViaText,
tName = targetStationName,
wNum = cdWagonNumbers[vehicleId]
}
end
end
end
end
end
return payload
end
function data()
return {
updateFn = function()
local trainData = processTrainData()
game.interface.sendScriptEvent("cd_is_mod", "syncLabels", trainData)
end
}
end
Alles anzeigen
So I'd like to ask for some help with the script instead of endlessly testing it with AI. Thank you