mte.getObject error

Hi

whenever I try to use the function getObject the console return me an error. (I have the same error in mte v918 and 927).

i use:

local objects = mte.getObject({layer = 4})  

and I get this error:

mte.lua:2475: attempt to get the length of field "objects" (a nil value)  

My map loads correctly, and I have a layer set to objectLayer=true. (if i set {layer = 2} which is not a layer object I get the warning “layer not an objectLayer”… so…that part work ok.

I’m pretty sure I’m missing i little something… but what is it?

Hello jerars,

It sounds to me like you’ve manually created an objectLayer property in a Tile Layer or Image Layer and set it to “true.” This will trick MTE into thinking that the layer is an Object Layer even though it is not; the map will load, but you’ll get errors if you try to retrieve objects or perform other actions related to objects. I was able to reproduce the error message you posted by doing just that.

What are you trying to do, exactly? If you want to retrieve sprites you can use getSprites(). You can get a reference to a layer’s group object- the container holding all the layer’s sprites and tiles- with getLayerObj(). The “objects” retrieved with getObject() are created in Tiled, and only on Object Layers created in Tiled. They are their own layer type distinct from Tile Layers and Image Layers.

Hi dyson!

you are correct, I was trying to get object from a Tile layer instead of a real Object layer. thanks for the solution.

I’m working on a sidescroller game Mario bros type, What I would like to do is to apply an event listener (pre collision) to my coin objects in my coin layer. I’m still pretty green with MTE so I’m not sure this is the way I should do it.

I was also thinking of doing a getObject(or getSprite) in my gameLoop function on the enterframe event. I could get the sprite at the player location and see if there is a coin. Any other way you can think of?

I’m must admit I have to play with MTE more, I’m not 100% sure if I should use physics. Right now I’m using moveSprite to move my player left/right and physics to make him jump.,

thanks for your help.

How you go about detecting collisions with coins depends on 1) whether each coin is placed neatly in one map location and 2) whether you use physics. 

I generally recommend anyone working on a platforming game to use the Physics API if only because it offers so much power and flexibility. The overhead is a little higher, but you gain a lot in return. For example, without physics you would have to manually detect slopes in a level and move the player sprite accordingly, and these are not trivial tasks. With the physics system, the sprite reaches the slope and goes up it automatically. The moveSprite() function moves a sprite a certain number of pixels, but if the sprite is a physics object it will still react with other physics objects.

One thing to keep in mind is that physics objects are only active as long as they remain within the active map region; they go to sleep and stop responding to gravity and other physics objects when they leave this area. By default the active map region is just larger than the visible screen area and is calculated when you call goto(). You can increase the size of the area with the cullingMargin parameter of goto() in order to allow physics objects to remain active further from the outside edges of the screen. 

Hello jerars,

It sounds to me like you’ve manually created an objectLayer property in a Tile Layer or Image Layer and set it to “true.” This will trick MTE into thinking that the layer is an Object Layer even though it is not; the map will load, but you’ll get errors if you try to retrieve objects or perform other actions related to objects. I was able to reproduce the error message you posted by doing just that.

What are you trying to do, exactly? If you want to retrieve sprites you can use getSprites(). You can get a reference to a layer’s group object- the container holding all the layer’s sprites and tiles- with getLayerObj(). The “objects” retrieved with getObject() are created in Tiled, and only on Object Layers created in Tiled. They are their own layer type distinct from Tile Layers and Image Layers.

Hi dyson!

you are correct, I was trying to get object from a Tile layer instead of a real Object layer. thanks for the solution.

I’m working on a sidescroller game Mario bros type, What I would like to do is to apply an event listener (pre collision) to my coin objects in my coin layer. I’m still pretty green with MTE so I’m not sure this is the way I should do it.

I was also thinking of doing a getObject(or getSprite) in my gameLoop function on the enterframe event. I could get the sprite at the player location and see if there is a coin. Any other way you can think of?

I’m must admit I have to play with MTE more, I’m not 100% sure if I should use physics. Right now I’m using moveSprite to move my player left/right and physics to make him jump.,

thanks for your help.

How you go about detecting collisions with coins depends on 1) whether each coin is placed neatly in one map location and 2) whether you use physics. 

I generally recommend anyone working on a platforming game to use the Physics API if only because it offers so much power and flexibility. The overhead is a little higher, but you gain a lot in return. For example, without physics you would have to manually detect slopes in a level and move the player sprite accordingly, and these are not trivial tasks. With the physics system, the sprite reaches the slope and goes up it automatically. The moveSprite() function moves a sprite a certain number of pixels, but if the sprite is a physics object it will still react with other physics objects.

One thing to keep in mind is that physics objects are only active as long as they remain within the active map region; they go to sleep and stop responding to gravity and other physics objects when they leave this area. By default the active map region is just larger than the visible screen area and is calculated when you call goto(). You can increase the size of the area with the cullingMargin parameter of goto() in order to allow physics objects to remain active further from the outside edges of the screen.