Issue while doing lua stuff

Place to get help with not working mods / modding interface.
Remtak
Burner Inserter
Burner Inserter
Posts: 6
Joined: Mon Jan 01, 2018 4:17 pm
Contact:

Issue while doing lua stuff

Post 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 :).
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Issue while doing lua stuff

Post by darkfrei »

Don't delete item by data stage, but destroy it by control stage.
Remtak
Burner Inserter
Burner Inserter
Posts: 6
Joined: Mon Jan 01, 2018 4:17 pm
Contact:

Re: Issue while doing lua stuff

Post 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 ?
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Issue while doing lua stuff

Post by darkfrei »

Last edited by darkfrei on Tue Jan 02, 2018 5:49 pm, edited 2 times in total.
Remtak
Burner Inserter
Burner Inserter
Posts: 6
Joined: Mon Jan 01, 2018 4:17 pm
Contact:

Re: Issue while doing lua stuff

Post by Remtak »

Thanks a lot for the anwser.
I will watch the links and tell you if it works :).
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Issue while doing lua stuff

Post 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
Attachments
Easy_Inventory_0.0.1.zip
For 0.16
(940 Bytes) Downloaded 92 times
Remtak
Burner Inserter
Burner Inserter
Posts: 6
Joined: Mon Jan 01, 2018 4:17 pm
Contact:

Re: Issue while doing lua stuff

Post 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
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Issue while doing lua stuff

Post 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
Attachments
Easy_Inventory_0.0.2.zip
For 0.16
(1016 Bytes) Downloaded 98 times
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3724
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Issue while doing lua stuff

Post 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.
Remtak
Burner Inserter
Burner Inserter
Posts: 6
Joined: Mon Jan 01, 2018 4:17 pm
Contact:

Re: Issue while doing lua stuff

Post 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.
chrisgbk
Long Handed Inserter
Long Handed Inserter
Posts: 93
Joined: Mon Jan 02, 2017 4:31 am
Contact:

Re: Issue while doing lua stuff

Post 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.
Remtak
Burner Inserter
Burner Inserter
Posts: 6
Joined: Mon Jan 01, 2018 4:17 pm
Contact:

Re: Issue while doing lua stuff

Post 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. :).
Post Reply

Return to “Modding help”