1. Home
  2. Docs
  3. LEd documentation
  4. Haxe API
  5. Usage

Usage

Create the project class

Create a HX file for each L-Ed project JSON. The filename isn’t important, pick whatever you like.

This HX will host all the the typed data extracted from the JSON:

MyProject.hx:

private typedef _Tmp =
    haxe.macro.MacroType<[ led.Project.build("../path/to/myProject.json") ]>;

Notes:

  • The “_Tmp” typedef isn’t actually directly used. But for syntax validity purpose, we need an identifier here. Anything will do the job.
  • This magic line will call the led.Project.build macro which will parse the project JSON file, then use its content to dynamically construct types & classes definitions at compilation-time.

Create a project instance

Somewhere else, in your game code, add the following to instanciate the project data.

MyGame.hx:

class MyGame {
    public function new() {
        var p = new MyProject();
        trace( p.all_levels ); // Well done!
    }
}

You can move the project HX class to a sub-package, just add the corresponding package line at the beginning:

package assets;

private typedef _Tmp =
    haxe.macro.MacroType<[ led.Project.build("../path/to/myProject.json") ]>;

Accessing your project content

Layers, entities, fields etc.

The project content is easily accessed using various methods, as seen below:

var p = new MyProject();

// Access to a specific level
var level = p.all_levels.MyFirstLevel;
trace( level.pxWid );

// Access to a layer in this level
var someLayer = level.l_myIntGridLayer; // return a properly typed IntGrid layer
var entityLayer = level.l_myEntityLayer; // return a properly typed Entity layer

// Access to some entities in an Entity-layer
for( treasure in entityLayer.all_Treasure )
    trace( treasure.pixelX );

// Access to entity fields
var someTreasure = entityLayer.all_Treasure[0];
trace( someTreasure.f_isTreasureHidden ); // boolean custom field
trace( someTreasure.f_customColor_hex ); // color code in Hex format (#rrggbb)

Project types

If your project class is MyProject, you can access all its related types by using:

MyProject.Entity_Mob;
MyProject.Layer_MyIntGridLayer;
MyProject.Level;

This can be useful to hint function parameters:

function something( level : MyProject.Level ) {
  trace( level.identifier+" => "+level.pxWid+"px" );
}