Page 1 of 1

Error when saving ruins of damaged objects in "Ruin maker Updated (Time fork)"

Posted: Fri Dec 20, 2024 4:02 am
by Keysivi
This is a continuation of the topic Strange problem with the "Ruin maker" mod viewtopic.php?f=25&t=124603

After all the work done to update the Ruin maker mod and upload it to the site as a fork
"Ruin maker Updated (Time fork)" https://mods.factorio.com/mod/ruin-maker_updated_fork a new error has appeared. If there are no problems when saving ruins with intact objects, then when trying to create ruins from damaged objects, an error occurs:

Code: Select all

The Ruin maker Updated (Fork) mod (0.1.4) has caused a fatal error.
Please report this error to the mod author.

Error while running event ruin-maker_updated_fork::on_gui_click (ID 1)
LuaEntityPrototype doesn't contain key max_health.
stack traceback:
 [C]: in function '__index'
 __ruin-maker_updated_fork__/output.lua:54: in function 'create_entity'
 __ruin-maker_updated_fork__/output.lua:87: in function 'output_selection'
 __ruin-maker_updated_fork__/control.lua:13: in function 'confirm_selection'
 __ruin-maker_updated_fork__/control.lua:19: in function <__ruin-maker_updated_fork__/control.lua:17>
It complains about this script - that the 'max_health' key is not specified there:

Code: Select all

30 local function create_entity(entities, center, damage, items, fluids, out)
31  if next(entities) == nil then return end
32
33  out[#out+1] = "  entities =\n"
34  out[#out+1] = "  {\n"
35
36  for _, entity in pairs(entities) do
37    local vec = util.vector_from_center(entity.position, center)
38
39    out[#out+1] = "    {\"" .. entity.name .. "\", "
40    out[#out+1] = "{x = " .. vec.x .. ", y = " .. vec.y .. "}, "
41
42    out[#out+1] = "{" -- extra options start
43
44    local dir = entity.direction
45    if dir ~= defines.direction.north then
46      out[#out+1] = "dir = \"" .. util.direction_to_str(dir) .. "\", "
47    end
48    if entity.is_entity_with_owner then
49      if entity.force.name ~= "player" and entity.force.name ~= "neutral" then
50        out[#out+1] = "force = \"enemy\", "
51      end
52    end
53    if damage and entity.is_entity_with_health and (entity.get_health_ratio() ~= 1) then
54      local dmg = math.floor(entity.prototype.max_health - entity.health)
55      out[#out+1] = "dmg = {dmg = " .. dmg .. "}, "
56    end
57    if items and entity.has_items_inside() then
58      local inv = nil
59      if entity.type == "container" or entity.type == "logistic-container" then
60        inv = entity.get_inventory(defines.inventory.chest)
61      elseif entity.type == "ammo-turret" then
62        inv = entity.get_inventory(defines.inventory.turret_ammo)
63      end
64      if inv then
65        out[#out+1] = "items = " .. serpent.line(inv.get_contents()) .. ", "
66      end
67    end
68    if fluids and entity.get_fluid_count() > 0 then
69      out[#out+1] = "fluids = " .. serpent.line(entity.get_fluid_contents()) .. ", "
70    end
71    if entity.type == "assembling-machine" then
72      local recipe = entity.get_recipe()
73      if recipe then
74        out[#out+1] = "recipe = \"" .. recipe.name .. "\", "
75      end
76    end
77    out[#out+1] = "}},\n" -- extra options and entity end
78  end
79
80  out[#out+1] = "  },\n"
81end
But it is there!!!! I don't understand what the problem is....

Code: Select all

53    if damage and entity.is_entity_with_health and (entity.get_health_ratio() ~= 1) then
54     local dmg = math.floor(entity.prototype.max_health - entity.health)
55      out[#out+1] = "dmg = {dmg = " .. dmg .. "}, "
56    end

Re: Error when saving ruins of damaged objects in "Ruin maker Updated (Time fork)"

Posted: Fri Dec 20, 2024 4:08 am
by Keysivi
Other forks related to this mod:

"The Ruins Mod Updated (Time fork)" https://mods.factorio.com/mod/Abandoned ... dated_fork

"The Ruins Mod - Base (Time)" https://mods.factorio.com/mod/AbandonedRuins-base

Re: Error when saving ruins of damaged objects in "Ruin maker Updated (Time fork)"

Posted: Fri Dec 20, 2024 6:56 am
by IsaacOscar
You're misunderstanding the error message, "LuaEntityPrototype doesn't contain key max_health" Bassically means that your code is doing "something.max_health", where something is a LuaEntityPrototype, but max_health is not listed in https://lua-api.factorio.com/latest/cla ... otype.html
So look for something else there you could use to get the maximum health
just replace .max_health with .get_max_health()

Re: Error when saving ruins of damaged objects in "Ruin maker Updated (Time fork)"

Posted: Fri Dec 20, 2024 9:18 am
by boskid
Keysivi wrote: Fri Dec 20, 2024 4:02 am

Code: Select all

54      local dmg = math.floor(entity.prototype.max_health - entity.health)
Change "entity.prototype.max_health" into "entity.max_health".

max_health on LuaEntity takes into account quality of the entity and all weird other effects like character health bonuses or enemies evolution factors. Prototype is context unaware so it is a method because it takes quality to obtain at least part of the context while still missing other factors.

Re: Error when saving ruins of damaged objects in "Ruin maker Updated (Time fork)"

Posted: Fri Dec 20, 2024 9:54 am
by Keysivi
IsaacOscar wrote: Fri Dec 20, 2024 6:56 am You're misunderstanding the error message, "LuaEntityPrototype doesn't contain key max_health" Bassically means that your code is doing "something.max_health", where something is a LuaEntityPrototype, but max_health is not listed in https://lua-api.factorio.com/latest/cla ... otype.html
So look for something else there you could use to get the maximum health
just replace .max_health with .get_max_health()
Thank you very much for your advice!!! I did - as you wrote:

Code: Select all

    if damage and entity.is_entity_with_health and (entity.get_health_ratio() ~= 1) then
      local dmg = math.floor(entity.prototype.get_max_health() - entity.health)
      out[#out+1] = "dmg = {dmg = " .. dmg .. "}, "
    end
This error does not appear anymore. Ruins with damaged and destroyed objects are successfully saved and spawned.

I hope that this was the last such error with this mod... Because I probably have already tired you out a lot...

Unfortunately, this mod turned out to be too complicated for my modest skills. Without your help, I would not have succeeded...

I think I will probably not repeat such an experience again...

I'd rather do something simpler, which will not be perceived as magical magic for me....

Re: Error when saving ruins of damaged objects in "Ruin maker Updated (Time fork)"

Posted: Fri Dec 20, 2024 9:56 am
by IsaacOscar
Keysivi wrote: Fri Dec 20, 2024 9:54 am
IsaacOscar wrote: Fri Dec 20, 2024 6:56 am You're misunderstanding the error message, "LuaEntityPrototype doesn't contain key max_health" Bassically means that your code is doing "something.max_health", where something is a LuaEntityPrototype, but max_health is not listed in https://lua-api.factorio.com/latest/cla ... otype.html
So look for something else there you could use to get the maximum health
just replace .max_health with .get_max_health()
Thank you very much for your advice!!! I did - as you wrote:

Code: Select all

    if damage and entity.is_entity_with_health and (entity.get_health_ratio() ~= 1) then
      local dmg = math.floor(entity.prototype.get_max_health() - entity.health)
      out[#out+1] = "dmg = {dmg = " .. dmg .. "}, "
    end
This error does not appear anymore. Ruins with damaged and destroyed objects are successfully saved and spawned.

I hope that this was the last such error with this mod... Because I probably have already tired you out a lot...

Unfortunately, this mod turned out to be too complicated for my modest skills. Without your help, I would not have succeeded...

I think I will probably not repeat such an experience again...

I'd rather do something simpler, which will not be perceived as magical magic for me....
Actually, (naturally) boskid's advice was slightly better than mine.

Re: Error when saving ruins of damaged objects in "Ruin maker Updated (Time fork)"

Posted: Fri Dec 20, 2024 9:58 am
by IsaacOscar
So far, your problems have been quite easy for to me solve, so I'm not tired of you (except that weird error you got about invalid entities that I couldn't reproduce...)

Re: Error when saving ruins of damaged objects in "Ruin maker Updated (Time fork)"

Posted: Fri Dec 20, 2024 10:08 am
by Keysivi
boskid wrote: Fri Dec 20, 2024 9:18 am Change "entity.prototype.max_health" into "entity.max_health".

max_health on LuaEntity takes into account quality of the entity and all weird other effects like character health bonuses or enemies evolution factors. Prototype is context unaware so it is a method because it takes quality to obtain at least part of the context while still missing other factors.
IsaacOscar wrote: Fri Dec 20, 2024 9:56 am Actually, (naturally) boskid's advice was slightly better than mine.

So far, your problems have been quite easy for to me solve, so I'm not tired of you (except that weird error you got about invalid entities that I couldn't reproduce...)

In any case, a huge thank you to everyone for your help!!!

I hope your help will be appreciated by all users of these mods, and not just me.