Page 1 of 1

[0.12.6] Bushes & ghosts — LuaEntity API call … invalid

Posted: Fri Sep 04, 2015 11:28 pm
by erisco
Reproduction
1) Install QuickPrints mod v0.1.3 https://github.com/erisco/quickprints/releases.
2) Start a new game.
3) Place a ghost object on a bush (you can use QuickPrints to unlock the necessary tech for this).
4) Click on the "QuickPrints" button at the top and in the expanded GUI click "On".
5) Walk near the ghost object (the intent here is to have QuickPrints place the object, so you need one in your inventory).

This is the exception you'll get:
LueEntity_API_call.png
LueEntity_API_call.png (242.02 KiB) Viewed 6843 times
Relevant line is 232 https://github.com/erisco/quickprints/b ... l.lua#L232

Code: Select all

    local nearbyEntities = game.get_surface(1)
      .find_entities{minCorner, maxCorner}
    for _, entity in pairs(nearbyEntities) do 
      if entity and entity.name == "entity-ghost" then     -- line 232
      -- snip
      end
    end
You can see that I added a check that "entity" is truthy. I was not sure what was causing the exception at first and since it was rare I just threw that in (isn't any help). The exception is thrown when "entity.name" is read.

This is looping over all nearby entities, and somehow it is picking up something the game engine is not happy with, and for some reason bushes are relevant.

Starting on line 110 I create the entity to replace the ghost entity. https://github.com/erisco/quickprints/b ... l.lua#L110

Code: Select all

  local surface = game.get_surface(1)
  if surface.can_place_entity(properties) then
    surface.create_entity(properties)
    return true
  else
    return false
  end

Re: [0.12.6] Bushes & ghosts — LuaEntity API call … invalid

Posted: Fri Sep 04, 2015 11:41 pm
by Rseding91
Creating the entity destroys the bush the ghost is on thus invalidating the bush entity. This is normal and running as expected - not a bug.

Check for entity.valid before trying to read the name off the entity.

Re: [0.12.6] Bushes & ghosts — LuaEntity API call … invalid

Posted: Fri Sep 04, 2015 11:55 pm
by erisco
Thanks, this resolved the issue.

Should be documented on https://forums.factorio.com/wiki/inde ... Lua/Entity else I don't see how anyone would know about this.

Re: [0.12.6] Bushes & ghosts — LuaEntity API call … invalid

Posted: Sat Sep 05, 2015 12:01 am
by Rseding91
erisco wrote:Thanks, this resolved the issue.

Should be documented on https://forums.factorio.com/wiki/inde ... Lua/Entity else I don't see how anyone would know about this.
https://forums.factorio.com/wiki/inde ... d_property

Re: [0.12.6] Bushes & ghosts — LuaEntity API call … invalid

Posted: Sat Sep 05, 2015 12:11 am
by erisco
Okay cool. When should you check if an object has become invalid? Also, if there was a list of exceptions and probable causes that'd seem like a great place to mention this.