Changing the name of an object

  • #1, by elwanTuesday, 03. March 2020, 10:46 5 years ago
    Is it possible with Lua to change, anytime during the game, the name included in the properties tab of an object ?
    Thanks for helping...

    Newbie

    26 Posts


  • #2, by afrlmeTuesday, 03. March 2020, 11:23 5 years ago
    Yes it is, but unfortunately the new name doesn't get saved into the save game data, meaning that when someone loads a save game that the original name will be displayed instead. However you work around this by creating a value & saving the name to the string section of the value & then on mouse over the character you can use Lua to overwrite the name with the one stored in the value & that way it will always display the correct name.

    I've just woken up, but I'll dig out the script/function to do that for you later on. wink

    Imperator

    7285 Posts

  • #3, by elwanTuesday, 03. March 2020, 13:56 5 years ago
    Thank you for this quick answer!
    I'll wait for your script, but I have to mention that I am a beginner in Lua...

    Newbie

    26 Posts

  • #4, by afrlmeTuesday, 03. March 2020, 14:58 5 years ago
    Ok, so here's what you can do...

    1. add this as a definition type script in the script section of the editor.

    function changeName(txt)
    
    
    
        local obj = game.CurrentObject.Name.TextLanguages
    
        -- + --
    
        for i = 1, #obj do
    
          obj[i].text = txt
    
        end
    
        -- + --
    
        game.CurrentObject.Name.TextLanguages = obj
    
    
    
    end


    Now what you need to do is create some mouse over actions for the objects/characters that you want to change the name for.

    & inside of these you need to add something like this...

    if value "example" is 1
    execute a script >


    local t = {
    
     English = "original name",
    
     German = "ursprüngliche name"
    
    }
    
    
    
    changeName( t[ game.StandardLanguage:getName() ] )

    else if value "example" is 2

    local t = {
    
    English = "alternative name",
    
    German = "alternativer name"
    
    }
    
    
    
    changeName( t[ game.StandardLanguage:getName() ] )


    end if

    ----

    Hopefully what I've just written makes sense.
    Anyway, good luck. wink

    * edit: I should have probably explained how it works. The definition script is a function that allows us to change the name of whatever the cursor is currently hovering over for each available game language. The mouse actions you create expect you to use a value that you can query to check which name it should use & then inside of some execute a script action parts you create some temporary tables containing the name for each available language & then the engine picks the relevant name from the table based on the current active game language & updates the name to that.

    Imperator

    7285 Posts

  • #5, by elwanTuesday, 03. March 2020, 16:26 5 years ago
    Thank you again for the quality of the answer.
    It will take me some time to implement this code in my game as I already said I am not an expert...

    Newbie

    26 Posts

  • #6, by afrlmeTuesday, 03. March 2020, 17:35 5 years ago
    No problem.

    What I posted is more or less plug & play. Just need to edit the table values & maybe the name of the languages if they don't match up - everything is case sensitive when it comes to Lua script.

    Imperator

    7285 Posts