Page 1 of 1
Change entity's max health
Posted: Thu Jun 18, 2015 9:49 pm
by vedrit
So, I have an entity which is successfully pulling a value from another form (Example: require("file") entity max_health = variable --variable is defined in file.lua ) but when I do something like changing the variable's value through an event in control, it doesn't seem to update the entity's max health, even if I re-place down the entity. Do the entity values not change once the game is loaded?
Re: Change entity's max health
Posted: Thu Jun 18, 2015 10:08 pm
by SirRichie
There are two issues here that prevent the max_health from being updated
1) if you set max_health = some_variable, then max_health is the value of some_variable at that point in time. If you later update some_variable, this does not have any effect on max_health. This statement is generally true for any variable on the left hand side, no matter if it is an entity property or a helper variable.
2) any entity properties which you modify through data.raw are loaded only once upon game start and cannot be changed during the course of the game. In fact, you cannot make changes to data.raw in event handlers.
Re: Change entity's max health
Posted: Thu Jun 18, 2015 10:37 pm
by vedrit
Ok, so how would I get it to update the value of that variable?
I've never messed with data.raw or any of it's contents, as far as I'm aware, so...Unless it's something that's done automatically...
Re: Change entity's max health
Posted: Thu Jun 18, 2015 10:59 pm
by ThaPear
Changing an entity's max health can only be done in files executed through data.lua, data-updates.lua or data-final-fixes.lua.
The only other way is replacing every entity on the map with a different version which has higher max health.
Re: Change entity's max health
Posted: Fri Jun 19, 2015 7:01 am
by semvoz
What is your use case for that?
Maybe there is another way to do what you are trying to do?
Re: Change entity's max health
Posted: Fri Jun 19, 2015 5:21 pm
by vedrit
For my mod, Alien Walls, it's currently set up that research will increase health regeneration rate, but I would like for the research to increase max health as well
Re: Change entity's max health
Posted: Fri Jun 19, 2015 6:21 pm
by ThaPear
vedrit wrote:For my mod, Alien Walls, it's currently set up that research will increase health regeneration rate, but I would like for the research to increase max health as well
Then the only way is to replace all the wall sections/gates. You could do something like this:
Code: Select all
wallNames = {"hybrid-wall-level1",
"hybrid-wall-level2",
"hybrid-wall-level3"}
gateNames = {"hybrid-gate-level1",
"hybrid-gate-level2",
"hybrid-gate-level3"}
game.oninit(defines.events.ontick, function(event) OnInit() end)
game.onload(defines.events.ontick, function(event) OnInit() end)
game.onevent(defines.events.onrobotbuiltentity , function(event) OnBuilt(event.createdentity) end)
game.onevent(defines.events.onbuiltentity, function(event) OnBuilt(event.createdentity) end)
function OnInit()
if not glob.wallLevel then
glob.wallLevel = 1
end
end
function OnBuilt(entity)
-- We have to consider different names now, with the levels. So use string.find to check if it's a wall/gate.
if string.find(entity.name, "hybrid%-wall") or string.find(entity.name, "hybrid%-gate") then
-- If we're at level 2 we'll need to upgade.
local newWall = UpgradeWallSection(entity)
-- Store it in the global list for later healing and possible levelling.
table.insert(glob.alienwall, newWall)
end
end
function UpgradeWallSection(wall)
-- Get the current health percentage. We don't want all the new wall sections to be at max health if the old ones weren't.
local healthPercent = wall.health / game.entityprototypes[wall.name].maxhealth
local newWall = false
local newHealth = 0
-- Is the current thing a gate?
if string.find(wall.name, "gate") ~= nil then
-- Create a gate.
newWall = game.createentity{name = gateNames[glob.wallLevel], position = wall.position}
-- Set the health of the new level gate.
newWall.health = game.entityprototypes[gateNames[glob.wallLevel]].maxhealth * healthPercent
else
-- Create a wall section.
newWall = game.createentity{name = wallNames[glob.wallLevel], position = wall.position}
-- Set the health of the new level wall.
newWall.health = game.entityprototypes[wallNames[glob.wallLevel]].maxhealth * healthPercent
end
-- Destroy the old wall.
wall.destroy()
return newWall
end
function UpgradeMaxHealth()
local newWalls = {}
-- Increment the level.
glob.wallLevel = glob.wallLevel + 1
-- Replace each wall section (or gate) with one of the newer wall/gate level.
for _, wall in pairs(glob.alienwall) do
-- If it's dead we don't need to upgrade.
-- No need to remove it either, as the current list will be obsolete soon.
if wall.valid then
local newWall = UpgradeWallSection(wall)
-- Insert into the list of new wall sections.
table.insert(newWalls, newWall)
end
end
-- The old list is obsolete now, simply overwrite.
glob.alienwall = newWalls
end