How to convert message type to string type?

I am using a old function that adapts a string to a maximun number of char for a single line, so I add line breaks when the line is too long.

I need to convert the message to a string to use my old code or at least know how I can adapt my message to add line break

Hey @davidpkami how are you?

You can use a code similar to this one to localize (not convert directly) you message to a string if the message contains variables inside:


using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

message_to_string := class(creative_device):

    # Localizable message function that takes a player parameter and returns a formatted message
    MyMessage<localizes>(Player:player) : message = "Player: {Player}"
    
    # Editable button device that can be configured in the UEFN editor
    @editable
    MyButton : button_device = button_device{}

    # Override function that runs when the device starts in a running game
    OnBegin<override>()<suspends>:void=
        # Subscribe the OnButtonPressed function to the button's interaction event
        MyButton.InteractedWithEvent.Subscribe(OnButtonPressed)

    # Function called when the button is pressed by an agent
    OnButtonPressed(Agent : agent) : void =
        # Get all players currently in the playspace
        AllPlayers := GetPlayspace().GetPlayers()
        # Check if there is at least one player and assign it to FirstPlayer
        if (FirstPlayer := AllPlayers[0]):
            # Call MyMessage function with the first player to generate a localized message
            MessageWithPlayer := MyMessage(FirstPlayer)
            # Convert the message to a string using Localize()
            MyString : string = Localize(MessageWithPlayer)
            # Print the converted string to the UEFN log
            Print("Converted message to string: {MyString}")

Keep in mind that if you are using player names, for instance, you won’t get the real player name but something like this:

This happens becasue player names are not available as a string in UEFN.

If the message is only text, you can use this code instead:


using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

message_to_string := class(creative_device):

    # Localizable message function that takes a player parameter and returns a formatted message
    MyMessage<localizes> : message = "Hello World!"
    
    # Editable button device that can be configured in the UEFN editor
    @editable
    MyButton : button_device = button_device{}

    # Override function that runs when the device starts in a running game
    OnBegin<override>()<suspends>:void=
        # Subscribe the OnButtonPressed function to the button's interaction event
        MyButton.InteractedWithEvent.Subscribe(OnButtonPressed)

    # Function called when the button is pressed by an agent
    OnButtonPressed(Agent : agent) : void =
        MyString : string = Localize(MyMessage)
        Print("Converted message to string: {MyString}")

Hope this helps you!

I have used this in my project, but it seems that it does not work and the messages are not localized

if(ValidMessage := MaybeNextMessage?):
                    DescriptionString_Final:=AdaptStringToLineV2(Localize(ValidMessage),40)
                    CharacterMessage_TextBlock.SetText(BasicMessage(DescriptionString_Final))

I localice the Message and after creating the string I use a custom function to slice the phrase if it is too long in to lines.

The message does not get localized

That’s correct, Localize() uses the language configured on the application (and since verse runs on server, it is always english)

There is no Localize(Message, "en-US"), Localize(Message, "pt-BR") and so on.

Localized messages are not meant to be post-processed (edited, trimmed or so on), it is supposed to be static.

For line breaks you should enable Text Wrap on the Text Block Widget. In verse it has not this option but you can workaround by making a single text widget with verse field for that… That approach is also way better than manipulating character sizes in verse from scratch (since it has lots of bugs, characters are not all same sizes and so on)

Oh I did not know that possibility. Is there anywhere where it explain how to do it?

It is very simple like any other widget setup:




How I use this on code?