Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Strings are usable in component data now

Discussion in 'Entity Component System' started by siggigg, Mar 14, 2019.

  1. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Today's ECS preview release included the ability to use bool and char in component data, so I started wondering if it would be possible to implement an "ECS string" using this... But then I remembered running across the "NativeString" types a while ago, but they weren't really usable from component data at the time (only NativeStringView).

    I checked on Discord, and dzamani there pointed out there were some changes to NativeStrings.. and lo and behold, you can use strings in ECS components now :D

    Code (CSharp):
    1. [Serializable]
    2. public struct StringTests : IComponentData {
    3.     public NativeString64 TestString;
    4. }
    Code (CSharp):
    1. public class StringTestSystem : ComponentSystem
    2. {
    3.     protected override void OnUpdate() {
    4.  
    5.         Entities.ForEach((ref StringTests strings) => { Debug.LogWarning($"!! {strings.TestString}"); });
    6.     }
    7. }

    For reference, these are the types I've been playing with:
    NativeString64 (64 bytes long, 30 characters).
    NativeString512 (512 bytes, 254 chars)
    NativeString4096 (4096 bytes, 2046 chars)

    For example the 64 byte string is stored as:
    private unsafe fixed uint buffer[15]

    So its 15x4 + 4 (for storing length) = 64 bytes.

    There are also more related types I haven't dug into yet, including something called "Word".. and I see the following comments in the source code:

    Code (CSharp):
    1. // A "Words" is an integer that refers to 4,096 or fewer chars of UTF-16 text in a global storage blob.
    2. // Each should refer to *at most* about one printed page of text.
    3. // If you need more text, consider using one Words struct for each printed page's worth.
    4. // If you need to store the text of "War and Peace" in a single object, you've come to the wrong place.
     
    Last edited: Mar 14, 2019
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Small correction: char is 16 bit -> NativeString64 can only store 30 characters.
     
  3. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Thanks, that was actually a typo :) fixed it
     
  4. dzamani

    dzamani

    Joined:
    Feb 25, 2014
    Posts:
    122
    Words, as I understand, is just a way to avoid recreating strings you already had and keep them together.

    But there is one thing I don't get, how is this `private unsafe fixed uint buffer[15]` blittable.
     
    siggigg likes this.
  5. timmehhhhhhh

    timmehhhhhhh

    Joined:
    Sep 10, 2013
    Posts:
    155
    *smashes that like button*
     
  6. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    Is there anyway we can use this in non ecs projects with c# jobs? Our data structures use strings a lot and it would be really useful to jobify the work done on them.
     
  7. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    948
    What version is this? preview.27?
     
  8. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Preview 28
     
  9. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Afaik you should be able to use this in native arrays
     
  10. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Huzzah!
    It's the fixed keyword that allows that. It embeds the array as part of the struct.
     
    siggigg likes this.
  11. timmehhhhhhh

    timmehhhhhhh

    Joined:
    Sep 10, 2013
    Posts:
    155
    while i don't think there is anything stopping you from doing this, for example, from a monobehaviour or your own custom class update, don't you need to complete the job immediately, defeating the purpose?
     
  12. siggigg

    siggigg

    Joined:
    Apr 11, 2018
    Posts:
    247
    Depends on what you are doing. If you are doing work that can be done in parallel you will benefit from multiple worker threads. You could also call complete in a LateUpdate.
     
    JakHussain likes this.
  13. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    426
    Native string is not promoted in auto generated component..
     
  14. Vlasov_VV

    Vlasov_VV

    Joined:
    Mar 9, 2018
    Posts:
    6
    Did you find a solution?
     
  15. TextusGames

    TextusGames

    Joined:
    Dec 8, 2016
    Posts:
    426
    No. But I have not searched for it hard.
     
    Vlasov_VV likes this.
  16. Vlasov_VV

    Vlasov_VV

    Joined:
    Mar 9, 2018
    Posts:
    6
    HELP. Native string is not promoted in auto generated component..
     
  17. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,554
    This is correct. It's not promoted for anyone right now. (0.6.0)
     
  18. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    There are also FixedStrings
     
    GameDeveloper1111 and toomasio like this.