How to access the localised_name of an unknown entity?

Place to get help with not working mods / modding interface.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1761
Joined: Sun Oct 14, 2018 8:13 am
Contact:

How to access the localised_name of an unknown entity?

Post by Pi-C »

I've looked at the localization tutorial and elsewhere, but I can't quite figure out how to access the localized name of an unknown entity. This example
Yes, I know the example is about an item, not an entity, but the category shouldn't matter here.
from the wiki seems easy enough:
game.print({"item-name.iron-plate"})
but that is because you know you're looking for iron-plates and can use a constant in your code. But how about the following?

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
    local entity = event.created_entity

    local vehicles = entity.surface.find_entities_filtered{area = area.create(entity.position,1), type = "car"}
    for i in pairs(vehicles) do
        local found_vehicle = vehicles[i]
        game.print("Your vehicle is a " .. {found_vehicle.localised_name})
    end
end        
There are different entities of the type "car" -- even in vanilla, you have the car and the tank, and mod authors may use the car prototype to disguise whatever they want as a car. So, if you don't know what mods may be installed and what entity names they might use, you can't really use constants. There must be some simple way to get it right, but I really don't see it. Can anybody help me out, please? :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: How to access the localised_name of an unknown entity?

Post by eradicator »

You already found LuaEntity.localised_name, so either i don't understand your problem, or you don't know how to use it? So here's an example (which you shouldn't use because it uses hardcoded English instead of a localization template):

Code: Select all

/c local this = game.player.vehicle if this then game.print{"",'Your riding a brand new ',this.localised_name} end
Technically all localised names are just strings, so you could try to construct them yourself by combining the correct prefix with the internal name of the prototype. But be careful, this is only correct if the prototype doesn't specify an override for the localization in data stage (mostly procedural items like fluid-barrels). And as you can not know if it does this or not you should only do this if you absolutely have to (see below).

Code: Select all

/c 
local loc_name = {'item-name.' .. game.item_prototypes['iron-plate'].name}
game.print(loc_name)
So generally in control stage you should always use the pre-constructed name. (In data stage this is far more complicated and there are other threads about it.)

Code: Select all

/c game.print(SomeLuaObject.prototype.localised_name)
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1761
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: How to access the localised_name of an unknown entity?

Post by Pi-C »

eradicator wrote: Thu Sep 05, 2019 8:41 pm You already found LuaEntity.localised_name, so either i don't understand your problem, or you don't know how to use it?
Right, I didn't use it correctly. Thought I'd have to use the braces and forgot that entity.localised_name is already a table …
So here's an example (which you shouldn't use because it uses hardcoded English instead of a localization template):

Code: Select all

Also, I only now realize that I used the wrong concatenators: ".." instead of (commas -- or is "" the actual concatenator?). Seeing a real example was very helpful! (On re-reading, I notice that I've made exactly the mistake of using hardcoded text in my output. So, I'll better actually get to use the localization file some more …

 [quote]
Technically all localised names are just strings, so you could [i]try to[/i] construct them yourself by combining the correct prefix with the internal name of the prototype. But be careful, this is only correct if the prototype doesn't specify an override for the localization in data stage (mostly procedural items like fluid-barrels). And as you can not know if it does this or not you should only do this if you absolutely have to (see below).
[code]
/c 
local loc_name = {'item-name.' .. game.item_prototypes['iron-plate'].name}
game.print(loc_name)
So generally in control stage you should always use the pre-constructed name. (In data stage this is far more complicated and there are other threads about it.)

Code: Select all

/c game.print(SomeLuaObject.prototype.localised_name)
I'll keep that in mind. Have to get back to my mod now, trying out things and getting a knack for the new toy. :-)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Post Reply

Return to “Modding help”