Page 1 of 1

create_entity Key "movement" not found in property tree

Posted: Mon May 15, 2017 3:36 pm
by sparr
I'm calling LuaSurface::create_entity with just a few parameters and getting a weird error about a missing property table key.

Here's the block of code:

Code: Select all

  -- clone entities
  local master_entities = surface.find_entities({master_pos, {master_pos.x+32, master_pos.y+32}})
  for _, entity in ipairs(master_entities) do
    if entity.position.x ~= master_pos.x+32 and entity.position.y ~= master_pos.y+32 then
      if entity.type ~= "character" and entity.type ~= "player" then
        local amount = entity.type == "resource" and entity.amount or nil
        surface.create_entity{
          name= entity.name,
          position= {
            x= (entity.position.x - master_pos.x) * slave_dx + slave_pos.x + (master_pos.x > slave_pos.x and 1 or 0),
            y= (entity.position.y - master_pos.y) * slave_dy + slave_pos.y + (master_pos.y > slave_pos.y and 1 or 0)
          },
          direction= entity.direction,
          force= entity.force,
          -- TODO: more thorough cloning
          -- type-specific parameters
          amount= amount,
        }
      end
    end
  end
And here's the error:

Code: Select all

Error while running event world-mirror::on_chunk_generated (ID 12)
Key "movement" not found in property tree at ROOT
stack traceback:
__world-mirror__/control.lua:97: in function mirror_chunk
__world-mirror__/control.lua:130: in function <__world-mirror__/control.lua:116>
Line 97 is the call to surface.create_entity

Re: create_entity Key "movement" not found in property tree

Posted: Mon May 15, 2017 3:52 pm
by bobingabout
Basically, what it's telling you is that you're not accounting for all possible entity types, you're trying to clone an entity that has a movement key, but aren't setting one.

In such situations, it's more common to use a table.deepcopy (I think that's the command) to clone one table onto another, then edit the values you want changed afterwards (Like position in this instance), Though I'm not sure how that would work in this specific case either.

Perhaps something like this? (untested code)

Code: Select all

        new_entity = surface.create_entity(entity)
          new_entity.position= {
            x= (entity.position.x - master_pos.x) * slave_dx + slave_pos.x + (master_pos.x > slave_pos.x and 1 or 0),
            y= (entity.position.y - master_pos.y) * slave_dy + slave_pos.y + (master_pos.y > slave_pos.y and 1 or 0)
          }

Re: create_entity Key "movement" not found in property tree

Posted: Mon May 15, 2017 5:32 pm
by sparr
Thanks. It seems I was trying to clone a particle entity, which has some required properties.

Sadly those properties don't seem to be readable at all through the api.

I'll add an exception and not try to clone particles. And I'll cover more of the types.