Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

[Solved] How to change the value of an array.

Discussion in 'Project Tiny' started by boringName, May 21, 2019.

  1. boringName

    boringName

    Joined:
    Jul 20, 2016
    Posts:
    8
    I have:

    1. an enum named Pieces that has 12 values.
    2. A component named board which has a property "pieces". The property is an array of the former enum Pieces.

    I'm trying to change the value at an index using the following code.

    Code (JavaScript):
    1.  this.world.forEach([ut.Entity,game.Board,game.ToSpawn],
    2.                 (self,board,tag) =>
    3.             {
    4.                 board.pieces[4] = Pieces.Empty;
    5.                 board.pieces[28] = Pieces.BlackBishop;            
    6.                 this.world.removeComponent(self,game.ToSpawn);
    7.                 this.world.addComponent(self,game.IsSpawn);
    8.                 length = board.pieces.length;
    9.  
    10.             });
    The bug is that the lines board.pieces[4] and board.pieces[28] aren't actually changing the underlying value at all. I've not been able to change the value of the array anywhere.

    I feel like I'm making a small, fundamental mistake. Any advice on the correct way to edit the values of 4 and 28 in the array?
     
  2. Ferran_SP

    Ferran_SP

    Joined:
    Jul 9, 2018
    Posts:
    27
    Are there any other entities that conform to this query? I mean, is it possible that you are changing the values of one entity, but then checking them in another entity?

    Have you tried what happens if you comment out the removeComponent and addComponent instructions?
    Just to test it out.
     
  3. Rupture13

    Rupture13

    Joined:
    Apr 12, 2016
    Posts:
    78
    The forEach loop with the components has some unexpected behaviour with setting the component data that you change in the loop. Most component data that you change in the loop is set to the entity's component correctly after the loop, but that seems to depend on whether or not Tiny detected if a field in a component has been assigned.

    It doesn't detect field assignments when the assignment is "two (or more) levels" deep. Like, your array is level 1 deep, and the specific elements in your array are two levels deep. The same goes for a component using a struct: the fields in the structs would also be two levels deep.

    To make it detect your assignments, you can do two things:
    • Assign the entire array. In your case, after you assigned the individual pieces, you could add a line
      board.pieces = board.pieces;
      which looks stupid but works.

    • Use
      this.world.setComponentData(self, board);
      , which also seems redundant but also works.

    For more detailed information on this matter, you could take a look at this thread.
     
    reallyhexln and boringName like this.
  4. boringName

    boringName

    Joined:
    Jul 20, 2016
    Posts:
    8
    Thanks everyone, I fiddled about a bit with the code and finally got my goal actively working.

    The code that finally worked was:

    Code (JavaScript):
    1.                 let pieces = board.pieces;      
    2.                 pieces[4] = Pieces.Empty;
    3.                 pieces[28] = Pieces.BlackBishop;
    4.                 board.pieces = pieces;