Page 1 of 1
Issue while doing lua stuff
Posted: Mon Jan 01, 2018 4:35 pm
by Remtak
Hi guys,
I'm new to modding and really happy about how modding is possible in this game.
However, I have create a mod that replace the inserters in game.
All the prototype are done but i want to delete the long inserter from the game since it won't be used anymore.
I have already add those lines:
Code: Select all
data.raw.recipe["long-handed-inserter"] = nil
data.raw.inserter["long-handed-inserter"] = nil
data.raw.item["long-handed-inserter"] = nil
Now, the game won't use the long inserter anymore since it doesn't exist.
But now the next issue:
When i go into the sandbox scenario, the game load the file : control.lua in the __base__/scenarios/sandbox folder
In this control.lua file there is:
Code: Select all
function give_items(player)
local items =
{
...
["long-handed-inserter"] = "50",
...
}
I would like to erase " ["long-handed-inserter"] = "50", " from the
control.lua file.
But if i'm not wrong the file is loaded by the game when you enter the sandbox scenario. So i can't call the function while the game load.
Then, I would like to create a
control.lua in my mod that manualy load the file with " require("scenarios.sandbox.control") " at the beggining and replace the function " give_items(player) " by the same one without the " ["long-handed-inserter"] = "50", ".
As i'm new to lua too it would be grateful if you could spare some help about it. And excuse me for my explanation i don't speak english fluently.
Happy new year everybody
.
Re: Issue while doing lua stuff
Posted: Mon Jan 01, 2018 9:05 pm
by darkfrei
Don't delete item by data stage, but destroy it by control stage.
Re: Issue while doing lua stuff
Posted: Tue Jan 02, 2018 12:23 am
by Remtak
Thanks for the answer.
So I would have to set my mod to delete the long hand inserter in the control.dat of the base game ?
Re: Issue while doing lua stuff
Posted: Tue Jan 02, 2018 8:10 am
by darkfrei
Re: Issue while doing lua stuff
Posted: Tue Jan 02, 2018 5:33 pm
by Remtak
Thanks a lot for the anwser.
I will watch the links and tell you if it works
.
Re: Issue while doing lua stuff
Posted: Tue Jan 02, 2018 6:09 pm
by darkfrei
So looks control.lua:
Code: Select all
script.on_event(defines.events.on_player_main_inventory_changed, function(event)
local player = game.players[event.player_index]
main (player)
end)
script.on_event(defines.events.on_player_quickbar_inventory_changed, function(event)
local player = game.players[event.player_index]
main (player)
end)
function main (player)
local item = "long-handed-inserter"
local character = player.character
local quickbar = character.get_quickbar()
local count_items = quickbar.get_item_count(item)
if count_items > 0 then
quickbar.remove({name=item, count=count_items})
end
local inventory = character.get_inventory(defines.inventory.player_main) or character.get_inventory(defines.inventory.god_main)
count_items = inventory.get_item_count(item)
if count_items > 0 then
inventory.remove({name=item, count=count_items})
end
end
Re: Issue while doing lua stuff
Posted: Tue Jan 02, 2018 7:01 pm
by Remtak
Okay, so by this control.lua code.
You check all the inventory change while in game but when there is 1 or more than 1 long inserters you remove it from the inventory.
Thanks to spare some time for my issue
.
Edit :
I tried your code but it returns this error. I think it doesn't work because the game add the item in the inventory and crash before running the code that you gave me to erase the item.
Code: Select all
29.181 Error MainLoop.cpp:1013: Exception at tick 62: Error while running event level::on_gui_click (ID 1)
...Data/Roaming/Factorio/temp/currently-playing/control.lua:138: long-handed-inserter is not a valid item
Will it be easier to set the control.lua to basicly erase the function that give the item " function give_items(player) " in the control.lua in the scenarios/sandbox folder and replace it with the same function without the long hand inserters ?
But at this point I don't know how to interract with the control.lua in the __base__ folder from a mod. I would say require() but this is only for the .lua files within the mod's folder
Re: Issue while doing lua stuff
Posted: Tue Jan 02, 2018 7:52 pm
by darkfrei
God controller wasn't found. I don't know how to deactivate some script from another mod (or scenario).
So, this works:
Code: Select all
script.on_event(defines.events.on_player_main_inventory_changed, function(event)
local player = game.players[event.player_index]
main (player)
end)
script.on_event(defines.events.on_player_quickbar_inventory_changed, function(event)
local player = game.players[event.player_index]
main (player)
end)
function main (player)
local item = "long-handed-inserter"
local controller = player.character or player
for i = 1, 10 do
local inventory = controller.get_inventory(i)
if inventory then
local count_items = inventory.get_item_count(item)
if count_items > 0 then
printAll("Invenory #".. i .. " had " .. count_items .. " items")
inventory.remove({name=item, count=count_items})
end
end
end
end
function printAll(text)
for player_index, player in pairs (game.players) do
game.players[player_index].print (text)
end
end
Re: Issue while doing lua stuff
Posted: Wed Jan 03, 2018 4:20 am
by DaveMcW
Why not edit the scenario?
Copy "C:\Program Files (x86)\Steam\steamapps\common\Factorio\data\base\scenarios\sandbox" to "%APPDATA%\Factorio\scenarios\sandbox".
Edit control.lua and remove long-handed-inserter.
Re: Issue while doing lua stuff
Posted: Wed Jan 03, 2018 11:21 am
by Remtak
Yeah sure I was about to do that but i was wondering a more automatic way to do that if the mod was active.
Furthermore, if I update the game the issue will be still there
.
And if i manage to understand how you guys deal with this kind of issue I could fix or create other mini mod that modify the base game.
Re: Issue while doing lua stuff
Posted: Wed Jan 03, 2018 4:49 pm
by chrisgbk
Remtak wrote:Yeah sure I was about to do that but i was wondering a more automatic way to do that if the mod was active.
Furthermore, if I update the game the issue will be still there
.
And if i manage to understand how you guys deal with this kind of issue I could fix or create other mini mod that modify the base game.
You could leave the item in the game, but remove the recipe so it can't be created, and do the previously mentioned monitoring of inventory. The reason it didn't work before was that the item was already removed, so the game failed to add it, so it never got to the point of being able to remove it after being inserted.
Re: Issue while doing lua stuff
Posted: Wed Jan 03, 2018 11:29 pm
by Remtak
Yup I delete :
Code: Select all
data.raw.inserter["long-handed-inserter"] = nil
data.raw.item["long-handed-inserter"] = nil
And add @darkfrei 's code.
Thanks a lot guys for the help.
.