[SOLVED] Starting items

Place to get help with not working mods / modding interface.
pieppiep
Fast Inserter
Fast Inserter
Posts: 170
Joined: Mon Mar 14, 2016 8:52 am
Contact:

[SOLVED] Starting items

Post by pieppiep »

I want to make a simple mod to change the starting items for a new game.
I already have this in the control.lua
script.on_event(defines.events.on_player_created, function(event)
game.local_player.insert{name="iron-plate", count=100}
end)
But the game tells me
This function can only be used when called from the console.
Last edited by pieppiep on Sat Jun 11, 2016 6:29 pm, edited 1 time in total.
pieppiep
Fast Inserter
Fast Inserter
Posts: 170
Joined: Mon Mar 14, 2016 8:52 am
Contact:

[SOLVED] Starting items

Post by pieppiep »

I've found example code in the base game in base/scenarios/sandbox/control.lua

This works
script.on_event(defines.events.on_player_created, function(event)
local player = game.get_player(event.player_index)
player.insert{name="iron-plate", count=100}
end)
User avatar
DedlySpyder
Filter Inserter
Filter Inserter
Posts: 254
Joined: Fri Jun 20, 2014 11:42 am
Contact:

Re: Starting items

Post by DedlySpyder »

Are you good them?

For explanation, "game.local_player" is only usable in the console, not in mods. There isn't really a "local player" from the mod's perspective.
User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 736
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: Starting items

Post by DRY411S »

I'd recommend taking a look at 'Spawn Gear' mod which changes what you start with.

Spawn Gear
pieppiep
Fast Inserter
Fast Inserter
Posts: 170
Joined: Mon Mar 14, 2016 8:52 am
Contact:

Re: Starting items

Post by pieppiep »

Yeah, my idea worked.

info.json
{
"name": "ReplycateEverything",
"version": "0.0.1",
"title": "Replycate Everything",
"author": "pieppiep",
"homepage": "",
"contact": "",
"description": "Replycate everything, mine nothing.",
"dependencies": ["base >= 0.12.15", "dark-matter-replicators >= 0.5.7"]
}
control.lua
require "defines"

local CHUNK_SIZE = 64

local function clear_chunk(surface, c_x, c_y)
for _, obj in ipairs(surface.find_entities_filtered{area = {{c_x - CHUNK_SIZE/2, c_y - CHUNK_SIZE/2}, {c_x + CHUNK_SIZE/2, c_y + CHUNK_SIZE/2}}}) do
if obj.valid and obj.type ~= "player" then
obj.destroy()
end
end
end

script.on_event(defines.events.on_chunk_generated, function(event)

local surface = game.surfaces['nauvis']
local c_x = event.area.left_top.x
local c_y = event.area.left_top.y

clear_chunk(surface, c_x, c_y)
end)

script.on_event(defines.events.on_player_created, function(event)
local player = game.get_player(event.player_index)
player.clear_items_inside()
player.insert{name="solar-panel", count=10}
player.insert{name="medium-electric-pole", count=3}
player.insert{name="replicator-1", count=1}
end)
You start with a solar panel, medium electric pole and a replicator from the dark matter replicators mod.
There is no ore to mine, no trees to cut and no oil to pump.
Create everything with the replicator you have.

I must admit the idea was better in theory.
Getting started is really boring this way.
But it worked :)
Post Reply

Return to “Modding help”