Search Unity

[Help] I can't creat Array Field in a Struct !

Discussion in 'Project Tiny' started by alamac123, Jan 4, 2019.

  1. alamac123

    alamac123

    Joined:
    Mar 15, 2017
    Posts:
    21
    How to i can do it ?
     
  2. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    4,129
  3. alamac123

    alamac123

    Joined:
    Mar 15, 2017
    Posts:
    21
    this is forum "Project Tiny" .
    Thank bro, but i need TinyScript.
     
  4. ER_Dolleman

    ER_Dolleman

    Joined:
    Dec 10, 2018
    Posts:
    19
    There is a "Change value to array" button if that is what you mean.

    upload_2019-1-4_11-38-7.png
     
    alamac123 likes this.
  5. alamac123

    alamac123

    Joined:
    Mar 15, 2017
    Posts:
    21
    Thankyou ! :D
     
  6. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    23
    Yes I see this button but then, how can I set the size and fill it with the values I want?
     
  7. reallyhexln

    reallyhexln

    Joined:
    Jun 18, 2018
    Posts:
    69
    gamayun, it's just a structure definition, you can't add data to it.
    Instead, you should add such structure to some component, then add such component to scene object, and finally, you will get way to add items to array.
     
    gamayun likes this.
  8. gamayun

    gamayun

    Joined:
    Nov 20, 2012
    Posts:
    23
    Yes reallyhexln, you are definitely right. It takes time for me to get used to the ECS but I find it really great, I just dont have yet all the good reflexes :)
     
    reallyhexln likes this.
  9. Zionisias

    Zionisias

    Joined:
    Apr 23, 2019
    Posts:
    40
    I wanted to add a small warning for when you are working with an array of a struct:
    There currently is an issue when using an array of a struct which has an EntityReference-variable. This issue is only problematic when you are assigning a new entity as an EntityReference to this struct.

    If you didn't know, when you create a new entity, this entity has an index which is below zero. The very next frame the index of the entity is updated to its actual index. Normally when you create a new entity and put is as a reference in a normal struct (Thus not in a list) the reference will be updated to have the correct index of the new entity automatically. This works the same for putting the entity directly into an EntityReference-variable in the component, and also works for a list of EntityReferences in the component.

    However
    , this is not the case when you have a combination of a struct and an array. The EntityReference-variable within the struct will not automatically update, and thus will remain to be the index which is below zero. There are a few workarounds for this, but the best way is to avoid using a structure like this at all for the time being.

    This problem also occurs when dragging entities into the EntityReference-variables via the inspector, because these entities are also being created and set into the struct in the same frame.
     
    reallyhexln likes this.
  10. reallyhexln

    reallyhexln

    Joined:
    Jun 18, 2018
    Posts:
    69
    Wow, it's a good note.

    I encountered a similar issue while using arrays in the component.

    So, the following code works perfect:
    Code (JavaScript):
    1.  
    2. this.world.forEach([game.MyComp, ut.Entity], (comp, entity) => {
    3.     comp.id = ...;
    4.     this.world.setComponentData(entity, comp);
    5. });
    6.  
    But, the following code do not work:

    Code (JavaScript):
    1. this.world.forEach([game.MyComp, ut.Entity], (comp, entity) => {
    2.     comp.ids.push(...);
    3.     this.world.setComponentData(entity, comp);
    4. });
    You must explicitly update reference to the array to update the component data:

    Code (JavaScript):
    1. this.world.forEach([game.MyComp, ut.Entity], (comp, entity) => {
    2.     comp.ids.push(...);
    3.     comp.ids = comp.ids.slice(); // this line is important
    4.     this.world.setComponentData(entity, comp);
    5. });
    I'm not sure why it can be, but it seems, Tiny have problems with determining updates in the deep of component data hierarchy.
     
  11. Zionisias

    Zionisias

    Joined:
    Apr 23, 2019
    Posts:
    40
    Yup, I also encountered this issue. It indeed seems that Tiny has a problem with checking changes for lists, but to make things even more strange, the following code does work:

    Code (CSharp):
    1. this.world.forEach([ut.Entity, game.ListComponent], (entity) => {
    2.     let listComp = this.world.getComponentData(entity, game.ListComponent);
    3.     listComp.Ids.push(Math.round(Math.random() * 9) + 1);
    4.     this.world.setComponentData(entity, listComp);
    5. });
    So the update-checking only seems to be bugged for when you do it on a component which is passed via the anonymous function's parameters.

    Another way of fixing this issue could be:
    Code (CSharp):
    1. this.world.forEach([ut.Entity, game.ListComponent], (entity, listComp) => {
    2.     let ids = listComp.Ids;
    3.     ids.push(...);
    4.     listComp.Ids = ids;
    5. });
    This way, you do not even need the
    this.world.setComponentData(entity, listcomp);
     
    reallyhexln likes this.