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’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

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

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

  1. boringName


    Jul 20, 2016
    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):
    2.                 (self,board,tag) =>
    3.             {
    4.                 board.pieces[4] = Pieces.Empty;
    5.                 board.pieces[28] = Pieces.BlackBishop;            
    6.       ,game.ToSpawn);
    7.       ,game.IsSpawn);
    8.                 length = board.pieces.length;
    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


    Jul 9, 2018
    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


    Apr 12, 2016
    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, 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


    Jul 20, 2016
    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;