Page 1 of 1

Modify vehicle entities globally

Posted: Mon Oct 05, 2020 6:43 pm
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.

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 6:51 pm
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.

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 6:57 pm
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?

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:02 pm
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)

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:10 pm
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.

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:20 pm
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?.

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:24 pm
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.

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:26 pm
by darkfrei
boskid wrote:
Mon Oct 05, 2020 7:10 pm
a/ After save-load hooks will stop working
No

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:28 pm
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.

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:30 pm
by darkfrei
Open your save file, there is the level.dat

all scripts are here

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:31 pm
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.

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 7:33 pm
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 :|

Re: Modify vehicle entities globally

Posted: Mon Oct 05, 2020 8:33 pm
by Squaresoft
Just tried editing my savefile and it works flawlessly! Everything I wanted, thank you people ♥!.

Re: Modify vehicle entities globally

Posted: Tue Oct 06, 2020 1:00 am
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.

Re: Modify vehicle entities globally

Posted: Tue Oct 06, 2020 10:46 am
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"))