Need a little help updating Floors mod
Posted: Sun Jan 18, 2015 2:49 am
Hello, all. I'm very familiar with Lua (via Garry's Mod, then Love2D), but this is my first time looking at Factorio's modding system. I'm trying to update the Floors mod to work with the latest version of Factorio, specifically, in multiplayer.
Running the 0.0.3 version of the mod in multiplayer and attempting to place a "Floor-O-Matic" causes a script error and closes Factorio. The error is:
"Error while running the event handler: _ _ floors _ _\control.lua:254: Map doesn't contain 1 player, this function can't be used"
The code it's referring to is this:
From what I understand, the issue is that in multiplayer, game.player is nil and instead game.players is used. Another thread had a snippet of code which suggests a solution:
However, this is not quite the solution. The intended behaviour would seemingly be to insert 1 Floor-O-Matic into the inventory of the player who just placed it, not into every player's inventory. So we need to determine the index of the player who placed it. According to the wiki, this information is not provided to the event handler function (it only gives createdentity). Is there a proper way to determine who placed it, or am I going to have to do something hacky like iterate through all players and just give the FOM to the one standing nearest to it?
Running the 0.0.3 version of the mod in multiplayer and attempting to place a "Floor-O-Matic" causes a script error and closes Factorio. The error is:
"Error while running the event handler: _ _ floors _ _\control.lua:254: Map doesn't contain 1 player, this function can't be used"
The code it's referring to is this:
Code: Select all
function safe_insert(i)
if not i then return false end
if game.player then -- the culprit, line 254
if game.player.character then
if (game.player.caninsert{name=i, count=1}==true) then
game.player.character.insert{name=i, count=1}
return true
end
end
end
return false
end
game.onevent(defines.events.onbuiltentity, function(event)
local is_floor=false
for _, ftype in ipairs(floor_types) do
if (event.createdentity.name==ftype) then is_floor=true end
end
if (is_floor==true) then
update_area(event.createdentity.position.x,event.createdentity.position.y,event.createdentity.name)
elseif (event.createdentity.name=="floor-b-gone") then
delete_area(event.createdentity.position.x,event.createdentity.position.y)
safe_insert("floor-b-gone")
elseif (event.createdentity.name=="floor-o-matic") then
flooromatic(event.createdentity.position.x,event.createdentity.position.y)
safe_insert("floor-o-matic")
end
end)
Code: Select all
for i, player in ipairs(game.players) do
-- call player functions here
end