How to properly print a train stop's name containing symbols

Place to get help with not working mods / modding interface.
Post Reply
boernsen2
Inserter
Inserter
Posts: 21
Joined: Thu Dec 02, 2021 5:36 pm
Contact:

How to properly print a train stop's name containing symbols

Post by boernsen2 »

Hi everyone,

if I use a copper-plat symbol for a train stop name:
Screenshot_20220427_133355.png
Screenshot_20220427_133355.png (3.19 KiB) Viewed 1095 times

then print its name:

Code: Select all

game.print(train_stop.backer_name)
it prints the symbol plus an item description next to it:
Screenshot_20220427_133613.png
Screenshot_20220427_133613.png (5.64 KiB) Viewed 1095 times

How can I print the symbol only as it is displayed for the train stop name?

Thank you :)

boernsen2
Inserter
Inserter
Posts: 21
Joined: Thu Dec 02, 2021 5:36 pm
Contact:

Re: How to properly print a train stop's name containing symbols

Post by boernsen2 »

I just found this page and I guess it has something to do with it:
https://wiki.factorio.com/Rich_text

boernsen2
Inserter
Inserter
Posts: 21
Joined: Thu Dec 02, 2021 5:36 pm
Contact:

Re: How to properly print a train stop's name containing symbols

Post by boernsen2 »

I found this solution, which seems to work for what I have tested:

Code: Select all

function corrected_train_stop_name(str)
    if not string.find(str,"=")
    then
        return str
    else
        return string.gsub(string.gsub(str, "=", "/"), "%[", "[img=")
    end
end
It converts

Code: Select all

[item=copper-plate]
into

Code: Select all

[img=item/copper-plate]

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: How to properly print a train stop's name containing symbols

Post by Optera »

The "correct" way is to use proper formating in stop names.
Here's a short code you could run from console to search and replace item and fluid icons in all train stop names on all surfaces:

Code: Select all

for _, surface in pairs(game.surfaces) do
  local stops = surface.find_entities_filtered{type="train-stop"}
  for _, stop in pairs(stops) do
    stop.backer_name = string.gsub(stop.backer_name, "item=", "img=item/")
    stop.backer_name = string.gsub(stop.backer_name, "fluid=", "img=fluid/")
  end
end

Post Reply

Return to “Modding help”