AddObject Causes Error on mte.Update()

I’m trying to add some objects dynamically to an objectLayer. 

My map has 3 layers: 2 Tile and 1 with a spriteLayer=true property. Tried with more layers/fewer layers and it made no difference.

I load the map and then add a sprite and a corresponding object. The sprite loads fine and causes no problems, but the addObject causes the below error when mte.update happens.

[lua]

OBJ_LAYER = mte.getObjectLayer(1)

mte.addObject(OBJ_LAYER, objX)

[/lua]

[lua]

objX = { x = x+1, y = y, moveType=moveDir}

[/lua]

[lua]

mte.lua:12943: attempt to index field ‘?’ (a nil value)

message

stack traceback:

    ?: in function <?:218>

[/lua]

I’ve stripped everything out of my project down to just a main and error still reproduces. I also tried it using the castleDemo map/tiles and reproduced the problem.

I’m going out on a limb and saying I’m doing something wrong here, but can’t seem to figure out what.

I verified that the getObjectLayer(1) returns my object layer correctly and verified that the object is inserted into the layer correctly but the update errors. 

Using daily builds of Corona from last few days (tried a couple)

Using MTE 958 and 957

That’s strange, the line giving the error has nothing to do with objects of that type. I don’t all have my files in front of me right now, but I can say that MTE expects objects added to a map to have the bare minimum of parameters and format of an object created in Tiled. As an experiment I would create an object in Tiled and use it’s format as a template for your object, including all the fields it uses, and see if you still get an error.

I’ll look into it tomorrow as well.

Good suggestion on using tiled object. Mine wasn’t exactly like what tiled used, however that had no bearing on the error.

I ‘resolved’ the error by adding the below code to MTE at line 12943. This is in no way fully tested.

The problem is that physicsData.layer is an empty table. I suspect this isn’t the only location and it may be an issue with my map that is triggering this. I stepped back through MTE and physics is not enabled (and I wasn’t explicitly calling enableBox2D). I did try calling enableBox2D and that didn’t have any impact.

In the below case next should be a local variable (for performance)

[lua]

                                if next(physicsData.layer) ~= nil then

                                    if physicsData.layer[layer].isAwake then

                                            object.isAwake = true

                                    end

                                end

[/lua]

I recreated the situation you described in your original post and did not get any errors. The code block containing line 12943 executes each update cycle if you’ve added a physics body to a display object. The assumption has been that nobody would want to add a physics body to a sprite unless they intended to use physics and call the necessary functions for starting the physics simulation. Adding an object to an object layer should not trigger any of that code. There may some crossed wires, so to speak.

Calling met.enableBox2DPhysics() sets a flag telling the engine to fill the physicsData.layer tables when you call loadMap. You’ll have to call it before calling loadMap for it to work correctly. Again though, unless physics is in use none of that should be necessary. Could you post your stripped-down main.lua or, even better, email me the stripped-down app you created to reproduce the error? I’m just not seeing enough here to really get down to the source of the problem.

Thanks Dyson I’ll see if I can put something together.

That’s strange, the line giving the error has nothing to do with objects of that type. I don’t all have my files in front of me right now, but I can say that MTE expects objects added to a map to have the bare minimum of parameters and format of an object created in Tiled. As an experiment I would create an object in Tiled and use it’s format as a template for your object, including all the fields it uses, and see if you still get an error.

I’ll look into it tomorrow as well.

Good suggestion on using tiled object. Mine wasn’t exactly like what tiled used, however that had no bearing on the error.

I ‘resolved’ the error by adding the below code to MTE at line 12943. This is in no way fully tested.

The problem is that physicsData.layer is an empty table. I suspect this isn’t the only location and it may be an issue with my map that is triggering this. I stepped back through MTE and physics is not enabled (and I wasn’t explicitly calling enableBox2D). I did try calling enableBox2D and that didn’t have any impact.

In the below case next should be a local variable (for performance)

[lua]

                                if next(physicsData.layer) ~= nil then

                                    if physicsData.layer[layer].isAwake then

                                            object.isAwake = true

                                    end

                                end

[/lua]

I recreated the situation you described in your original post and did not get any errors. The code block containing line 12943 executes each update cycle if you’ve added a physics body to a display object. The assumption has been that nobody would want to add a physics body to a sprite unless they intended to use physics and call the necessary functions for starting the physics simulation. Adding an object to an object layer should not trigger any of that code. There may some crossed wires, so to speak.

Calling met.enableBox2DPhysics() sets a flag telling the engine to fill the physicsData.layer tables when you call loadMap. You’ll have to call it before calling loadMap for it to work correctly. Again though, unless physics is in use none of that should be necessary. Could you post your stripped-down main.lua or, even better, email me the stripped-down app you created to reproduce the error? I’m just not seeing enough here to really get down to the source of the problem.

Thanks Dyson I’ll see if I can put something together.