Help me update an old private mod to 1.1

Looking for a mod? Have a review on a mod you'd like to share?
Post Reply
Peter34
Smart Inserter
Smart Inserter
Posts: 1100
Joined: Mon Nov 10, 2014 12:44 pm
Contact:

Help me update an old private mod to 1.1

Post by Peter34 »

I made an old mod for my own use, for 0.16 or 0.17, and despite being very simple it worked well.

Now it no longer works, and I'm not sure what to do. I've tried removing the dependencies on other mods (Agent Orange (which I've "hack-updated" to 1.1 for my own use), as well as Klonan's Torches mod), but that didn't make the mod work, so the issue is clearly something else.

This is the entirety of the control.lua file, which did work 2-3 years ago:

Code: Select all

script.on_event(defines.events.on_player_created, function(event)
		local player = game.players[event.player_index]
		player.insert{name="solid-fuel", count=4}
		player.insert{name="cliff-explosives", count=10}
		player.insert{name="wooden-chest", count=2}
		player.insert{name="burner-mining-drill", count=2}
		player.insert{name="stone-furnace", count=2}
end)
What do I update it to, to make it work in 1.1?

Pi-C
Smart Inserter
Smart Inserter
Posts: 1648
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help me update an old private mod to 1.1

Post by Pi-C »

I think you should also listen to on_cutscene_cancelled. (Since Factorio 1.0, a player will be created without an attached character when a new game is started; the character will be added once the cutscene has been cancelled or finished of its own.)
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Help me update an old private mod to 1.1

Post by darkfrei »

Peter34 wrote:
Thu Jan 14, 2021 6:10 am

What do I update it to, to make it work in 1.1?
https://wiki.factorio.com/Console#Add_i ... _inventory

User avatar
Deadlock989
Smart Inserter
Smart Inserter
Posts: 2528
Joined: Fri Nov 06, 2015 7:41 pm

Re: Help me update an old private mod to 1.1

Post by Deadlock989 »

OP hasn't given any info about what it is that's not working - an error message would have helped people help you. But Pi-C has probably guessed correctly, that the player is no longer guaranteed to have a character at the start of new games and therefore has no inventory to insert anything into. Assuming you want to play regular "freeplay" scenario Factorio, the default scenario for Factorio games, it has its own remote interface handler for defining starting and respawn inventories for all players. That same scenario also generates the "crash" cutscene and the cancelling of said cutscene so that is all handled for you as well.

Code: Select all

	if remote.interfaces["freeplay"] then
		remote.call("freeplay", "set_skip_intro", true/false)
		remote.call("freeplay", "set_disable_crashsite", true/false)
		remote.call("freeplay", "set_respawn_items", {list_of_items})
		remote.call("freeplay", "set_created_items", {list_of_items})
	end
These should be called from within the on_init() handler. The lists of items are key/value pairs where the key is the item name and the value is the amount. If you want no items, use an empty list {}.
Image

User avatar
jamiechi1
Fast Inserter
Fast Inserter
Posts: 196
Joined: Wed Jan 03, 2018 10:12 pm

Re: Help me update an old private mod to 1.1

Post by jamiechi1 »

Pi-C wrote:
Thu Jan 14, 2021 9:44 am
I think you should also listen to on_cutscene_cancelled. (Since Factorio 1.0, a player will be created without an attached character when a new game is started; the character will be added once the cutscene has been cancelled or finished of its own.)
A good example of this is Ingos Advanced Start mod. It uses on_cutscene_cancelled to do this. It works great with version 1.1.

I also like the post by Deadlock989. I always wanted to know how to skip the intro and crash site thing.
Thanks for this.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1648
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help me update an old private mod to 1.1

Post by Pi-C »

jamiechi1 wrote:
Wed Mar 03, 2021 3:26 am
I also like the post by Deadlock989. I always wanted to know how to skip the intro and crash site thing.
Thanks for quoting me! I remembered there was something or other where you could change the startup/respawn items, but "startup items" was too general as a search term. Now I've found the relevant info (remote.call) from Deadlock's post again just when I needed it. :-D
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
jamiechi1
Fast Inserter
Fast Inserter
Posts: 196
Joined: Wed Jan 03, 2018 10:12 pm

Re: Help me update an old private mod to 1.1

Post by jamiechi1 »

I have been searching for hours and can not find an example of the key/value pairs.
Specifically in this example. Could you show me and example of a couple of items for the {list_of_items} as shown above.
The wiki's tell about the key/value pairs but no examples. Is the key the item name and the value how many?
Like {"offshore-pump",1} ? And is a list of these pairs separated by commas or semicolons?

I have difficulty with Lua. I miss C++. :cry:

Edit: I finally figured it out. This works:
remote.call("freeplay", "set_created_items", {["offshore-pump"]=1,["iron-gear-wheel"]=1})

Also found out that the hand-crank-generator mod explains this in more detail. Lots of good comments inside. :D

Pi-C
Smart Inserter
Smart Inserter
Posts: 1648
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Help me update an old private mod to 1.1

Post by Pi-C »

jamiechi1 wrote:
Wed Mar 03, 2021 11:41 am
I have been searching for hours and can not find an example of the key/value pairs.
Specifically in this example. Could you show me and example of a couple of items for the {list_of_items} as shown above.
The wiki's tell about the key/value pairs but no examples. Is the key the item name and the value how many?
Like {"offshore-pump",1} ? And is a list of these pairs separated by commas or semicolons?

I have difficulty with Lua. I miss C++.
This is the vanilla list of items players will get when they are created (from base/scenarios/freeplay/freeplay.lua):

Code: Select all

local created_items = function()
  return
  {
    ["iron-plate"] = 8,
    ["wood"] = 1,
    ["pistol"] = 1,
    ["firearm-magazine"] = 10,
    ["burner-mining-drill"] = 1,
    ["stone-furnace"] = 1
  }
end
Suppose you want to extract a list of all items of which you get more than 1 piece, you could add this to control.lua (to be executed in script.on_init):

Code: Select all

if remote.interfaces["freeplay"] then
  local new_list = {}
  local created = remote.call("freeplay", "get_created_items")
  
  for item, amount in pairs(created) do
    if amount > 1 then
      new_list[item] = amount
    end
  end
  
  -- Overwrite the list of items players get on starting a new game
  remote.call("freeplay", "set_created_items", new_list)
end
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

User avatar
jamiechi1
Fast Inserter
Fast Inserter
Posts: 196
Joined: Wed Jan 03, 2018 10:12 pm

Re: Help me update an old private mod to 1.1

Post by jamiechi1 »

Thank you. It is starting to sink in now. :)

Post Reply

Return to “Questions, reviews and ratings”