Getting data from entity selection

Hi! I am making a grid map consisting of tiles.
What is the best way to get the data from each tile(entity) when clicking on them?
This is needed to update a tile. E.g. click on a tile to change it from a grass tile to a water tile.

I currently have it set up so each tile is an entity with a script called tile that contains the info about the tile such as coordinates and tile type. They get spawned at runtime and their tile data is assigned when spawned. E.g initial tile data: coords: [0, 0] and type: ‘default’ is then updated when spawned to e.g coords: [1,2] and type: ‘grass’

Thanks in advance

I managed to get the data by using attributes instead of creating the variables instead of the initialize function. Not sure if it’s an ideal solution so I am open for any tips or input :slight_smile:

Ex:

// Getting this data from raycast works
MyEntity.attributes.add('myVar', {
    type: 'number',
});

// Getting this data from raycast does not work
MyEntity.prototype.initialize = function () {
     this.myVar = 0;
};

Hi @Sebastian_H and welcome! You should be able to access the variable without using an attribute like the way below.

// entity > component > name > variable
entity.script.tile.myVar
1 Like

Thank you for the reply.
After some debugging and making a new project to test this I realized the problem was that I was updating the variables before adding the entity to the root. So maybe it’s not initialized before being added to the scene?

Anyways, here is a test project to see what I was trying to do. I explained in the comments of the file Raycaster.js what and where the issue was.
https://playcanvas.com/project/950781/overview/getting-data-from-raycast-result

1 Like

Ah yes. Scripts are initialised in hierarchy order on scene load. I would do all data initialisation, set up event listeners in the initialize function and then use postInitialize to read from other entities, fire events etc

1 Like