Search Unity

Converting from int or float to a FixedString in a Bursted Job

Discussion in 'Burst' started by Neil-Corre, May 19, 2021.

  1. Neil-Corre

    Neil-Corre

    Joined:
    Nov 29, 2015
    Posts:
    44
    I am making a localization system from components that look like this:

    Code (CSharp):
    1.     public readonly struct LocalizerComponent : IComponentData {
    2.         public readonly FixedString64 localizationId;
    3.         public readonly FixedString64 parameter;
    4.  
    5.         public LocalizerComponent(FixedString64 localizationId, FixedString64 parameter = default) {
    6.             this.localizationId = localizationId;
    7.             this.parameter = parameter;
    8.         }
    9.     }
    And a usage that looks something like this:

    Code (CSharp):
    1. LocalizerComponent component = new LocalizerComponent(localizationId,  someInteger.ToString());
    The system would then get the appropriate localized term using the ID, and apply whatever parameter the client code wants to be applied to the localized term. But, this doesn't work for bursted jobs.

    For more information, the idea here is that we can use the "parameter" field for different parameters that can be used for localization, for example for the localization entry "I am Agent {0}" where {0} is a number, with the usage above, the code would look something like:

    Code (CSharp):
    1. LocalizerComponent component = new LocalizerComponent(agentLocalizationId,  agentNumber.ToString());
    But, burst throws a warning that says: "Burst: Accessing managed method "ToString" from type "Int32" is not supported". Is there a way to convert different data types to FixedString that's supports Burst?
     
  2. Neil-Corre

    Neil-Corre

    Joined:
    Nov 29, 2015
    Posts:
    44
    Found a workaround - thank you zayshaa (from Twitch) for pointing me in the correct direction through FixedString.Format. Reading through FixedString's code, there's FixedString.Append that accepts different data types that can be used for this purpose.
     
    sheredom likes this.
  3. Heptagram064

    Heptagram064

    Joined:
    Feb 22, 2022
    Posts:
    99
    Hello, I came across a similar issue. However i cant seem to find FixedString.Append anywhere. Could you perhaps share what exactly you did to convert a int or float to a string format that doesn't break burst?
     
  4. Heptagram064

    Heptagram064

    Joined:
    Feb 22, 2022
    Posts:
    99
    Nevermind, Apparently one can just do
    Code (CSharp):
    1. myString.Append(myInt)
    For some reason my mind went the
    Code (CSharp):
    1. FixedString.Append(myString, myInt)
    way
     
    ashton_dev and tim_jones like this.