Search Unity

Clearing Tilemap

Discussion in 'Project Tiny' started by vincismurf, Dec 17, 2018.

  1. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I am doing some procedural tilemap stuff but I can't seem to clear out the existing tile.

    Code (JavaScript):
    1.  
    2.         static ClearTileMap(tilemap:ut.Tilemap2D.Tilemap)
    3.         {
    4.             tilemap.tiles.forEach(function(tile:ut.Tilemap2D.TileData, index:Number,array: ut.Tilemap2D.TileData[]){
    5.               if(!tile.tile.isNone())
    6.                this.world.destroyEntity(tile.tile);
    7.             } );
    8.        
    9.         }
    I get the error

    tsc-emit.js:55 Uncaught TypeError: Cannot read property 'destroyEntity' of undefined
    at tsc-emit.js:55
    at Array.forEach (<anonymous>)
    at Function.MapGenerator.ClearTileMap (tsc-emit.js:51)
    at tsc-emit.js:72
    at World.forEach (runtime.js:168689)
    at MapSystem.OnUpdate (tsc-emit.js:71)
    at Object.<anonymous> (bindings.js:1157)
    at Array.ASM_CONSTS (runtime.js:1683)
    at _emscripten_asm_const_iiii (runtime.js:1726)
    at _ut_SystemFn_call (runtime.js:164413)


    Am I missing something?
     
  2. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I noticed the this.world isn't really accessible within the loop so I tried this trick

    Code (CSharp):
    1.  
    2. namespace game {
    3.  
    4.     export class MapBehaviorFilter extends ut.EntityFilter {
    5.         tilemap: ut.Tilemap2D.Tilemap;
    6.  
    7.     }
    8.  
    9.     export class MapBehavior extends ut.ComponentBehaviour {
    10.  
    11.         data: MapBehaviorFilter;
    12.  
    13.         // ComponentBehaviour lifecycle events
    14.         // uncomment any method you need
    15.        
    16.         // this method is called for each entity matching the MapBehaviorFilter signature, once when enabled
    17.         OnEntityEnable():void
    18.         {
    19.             let world = this.world;
    20.             this.data.tilemap.tiles.forEach(function(value:ut.Tilemap2D.TileData, index:Number,array: ut.Tilemap2D.TileData[]){
    21.                
    22.                 world.destroyEntity(value.tile);
    23.            
    24.             } );
    25.  
    26.         }
    27.  
    28.         // this method is called for each entity matching the MapBehaviorFilter signature, every frame it's enabled
    29.         //OnEntityUpdate():void { }
    30.  
    31.         // this method is called for each entity matching the MapBehaviorFilter signature, once when disabled
    32.         //OnEntityDisable():void { }
    33.  
    34.     }
    35. }

    And I got 2 new Error

    runtime.js:1684 ut.World.destroyEntities(): Attempted to delete invalid entity [131:1] (NONEXISTENT) (passed at index 0 in the array to destroyEntities() function)
    Callstack:
    World.ut.WorldBase.destroyEntity (http://localhost:19050/runtime.js:167985:10)
    (anonymous) http://localhost:19050/~project/TinyExport/DungeonZ/html5/debug/ScriptAssemblies/tsc-emit.js:35:23
    Array.forEach (<anonymous>)
    ASM_CONSTS @ runtime.js:1684
    runtime.js:167075 Assertion failed: exists(entities), at: /Users/etienne/repos/packages/tiny/Runtime/modules/core/src/arch/EntityManagerAssertions.cpp,43,
    runtime.js:167076 Assertion failed: exists(entities), at: /Users/etienne/repos/packages/tiny/Runtime/modules/core/src/arch/EntityManagerAssertions.cpp,43,
    abort @ runtime.js:167076
    runtime.js:167085 Uncaught abort("Assertion failed: exists(entities), at: /Users/etienne/repos/packages/tiny/Runtime/modules/core/src/arch/EntityManagerAssertions.cpp,43,"). Build with -s ASSERTIONS=1 for more info.
     
  3. vincismurf

    vincismurf

    Joined:
    Feb 28, 2013
    Posts:
    200
    I seriously hate Javascript

    solution to clear is

    Code (JavaScript):
    1.  
    2. this.data.tilemap.tiles = [];
     
    facundo_unity961 likes this.
unityunity