Modify vehicle entities globally

Enhance your gameplay with these tools. This category is also the right place for tools useful for modders.
Mod databases, calculators, cheatsheets, multiplayer, scripts, libs and other useful stuff that is not strictly in-game mods.
Squaresoft
Burner Inserter
Burner Inserter
Posts: 7
Joined: Mon Oct 05, 2020 6:33 pm
Contact:

Modify vehicle entities globally

Post by Squaresoft »

Hello there.

I'd like to know if there is a command I can set to disable harvesting/mining cars, tanks and spidertrons (or any specific item ID changing the text I suppose) even while I'm not in it. Just disable it completely.

It's a small detail I'd like to play with. Even if it becomes an annoyance.
Sorry I couldn't find a specific section to talk about COMMAND codes.

Thank you.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2964
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Modify vehicle entities globally

Post by boskid »

Code: Select all

/c for _,e in pairs(game.player.surface.find_entities_filtered{name={"car","tank","spidertron"}}) do e.minable=false end
it only applies to the entities that are currently placed. Running any command will disable achievements.
Squaresoft
Burner Inserter
Burner Inserter
Posts: 7
Joined: Mon Oct 05, 2020 6:33 pm
Contact:

Re: Modify vehicle entities globally

Post by Squaresoft »

Thank you for your prompt response! Boskid.
Your command will be enough but just to be clear, there is no way to make this change constant without a Mod, right?
Last edited by Squaresoft on Mon Oct 05, 2020 7:12 pm, edited 1 time in total.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Modify vehicle entities globally

Post by darkfrei »

Squaresoft wrote: Mon Oct 05, 2020 6:57 pm Thank you for your prompt response! Boskid.
Your will be enough but just to be clear, there is no way to make this change constant without a Mod, right?

Code: Select all

/c

function on_built_entity (entity)
	if (entity.name == "car") or (entity.name == "tank") or (entity.name == "spidertron") then
		entity.minable=false
	end
end

script.on_event(defines.events.on_built_entity, function(event)
	on_built_entity (event.created_entity)
end)

script.on_event(defines.events.on_robot_built_entity, function(event)
	on_built_entity (event.created_entity)
end)

script.on_event(defines.events.script_raised_built, function(event)
	on_built_entity (event.entity)
end)
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2964
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Modify vehicle entities globally

Post by boskid »

darkfrei wrote: Mon Oct 05, 2020 7:02 pm
Using script.on_event in console command has multiple issues that i hate:
a/ After save-load hooks will stop working
b/ Because of the a, the replay will break
c/ Because of the a, multiplayer will desync (assuming other players would be able to join as they would most likely get the script mismatch when joining due to difference in registered hooks)

If you want to suggest broken things, at least know and tell what are the limitations.
Squaresoft wrote: Mon Oct 05, 2020 6:57 pm Your will be enough but just to be clear, there is no way to make this change constant without a Mod, right?
It is possible if you change the control.lua of your save file: you can copy everything from darkfrei code (excluding the /c from the beginning) and append it to the control.lua and then repack the save file.
Squaresoft
Burner Inserter
Burner Inserter
Posts: 7
Joined: Mon Oct 05, 2020 6:33 pm
Contact:

Re: Modify vehicle entities globally

Post by Squaresoft »

Oh multiplayer desync sounds like a real problem, yeah. Thank you for that info about scripts.

About repacking my save file, if I later host said save file online/LAN will it remain without problems for them too?.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2964
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Modify vehicle entities globally

Post by boskid »

If a script is in control.lua, it will be provided to all players and will survive the save-load so it will be persistent within that game.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Modify vehicle entities globally

Post by darkfrei »

boskid wrote: Mon Oct 05, 2020 7:10 pm a/ After save-load hooks will stop working
No
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2964
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Modify vehicle entities globally

Post by boskid »

darkfrei wrote: Mon Oct 05, 2020 7:26 pm
boskid wrote: Mon Oct 05, 2020 7:10 pm a/ After save-load hooks will stop working
No
Yes they will stop working if they were provided from the console.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Modify vehicle entities globally

Post by darkfrei »

Open your save file, there is the level.dat

all scripts are here
Attachments
Save004d.zip_Save004d_level.dat - Note2.png
Save004d.zip_Save004d_level.dat - Note2.png (45.17 KiB) Viewed 5104 times
Last edited by darkfrei on Mon Oct 05, 2020 7:32 pm, edited 1 time in total.
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2964
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: Modify vehicle entities globally

Post by boskid »

This is the console/chat history. It is only stored for the purpose of showing you the console/chat if you reopen it with a tilde.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Modify vehicle entities globally

Post by darkfrei »

boskid wrote: Mon Oct 05, 2020 7:31 pm This is the console/chat history. It is only stored for the purpose of showing you the console/chat if you reopen it with a tilde.
You are right, I've tried to mine the old car :|
Squaresoft
Burner Inserter
Burner Inserter
Posts: 7
Joined: Mon Oct 05, 2020 6:33 pm
Contact:

Re: Modify vehicle entities globally

Post by Squaresoft »

Just tried editing my savefile and it works flawlessly! Everything I wanted, thank you people ♥!.
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5207
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Modify vehicle entities globally

Post by eradicator »

boskid wrote: Mon Oct 05, 2020 7:10 pm
Squaresoft wrote: Mon Oct 05, 2020 6:57 pm Your will be enough but just to be clear, there is no way to make this change constant without a Mod, right?
It is possible if you change the control.lua of your save file: you can copy everything from darkfrei code (excluding the /c from the beginning) and append it to the control.lua and then repack the save file.
In the spirit of "telling the drawbacks":

That only works if the scenario doesn't already have handlers for the same events. If @OP is playing freeplay and it's the first random forum code snippet he copies it might be fine. But with custom scenarios or accumulating snippets it'll eventually break in non-obvious ways.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2905
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Modify vehicle entities globally

Post by darkfrei »

It's possible to extend the event handler, not replace it.

my-handler.lua:

Code: Select all

function on_built_entity (entity)
	if (entity.name == "car") or (entity.name == "tank") or (entity.name == "spidertron") then
		entity.minable=false
	end
end

local my_handler = { 
events =
{
	[defines.events.on_built_entity] = 	on_built_entity,
	[defines.events.on_robot_built_entity] = on_built_entity,
	[defines.events.script_raised_built] = 	on_built_entity,
}
}
return my_handler
And add it to the save file to control.lua as

Code: Select all

local handler = require("event_handler") -- see Factorio/data/core/lualib/event_handler.lua

 -- (here vanilla handlers)
 
handler.add_lib(require("my-handler"))
Post Reply

Return to “Tools”