how does "on_console_command" work?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
unfunf22
Inserter
Inserter
Posts: 31
Joined: Fri May 20, 2016 9:00 pm
Contact:

how does "on_console_command" work?

Post by unfunf22 »

hi i´m looking for an example because my mod does not load correctly on scenario start up and i have no interest in using a cheat command like /c research all.

my idea where like

Code: Select all

on_console_command check /turbine
if technology x and x are researched then enable this technology
if possible, but im open to suggestions.
thanks in advance

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2241
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: how does "on_console_command" work?

Post by boskid »

I think there are at least 2 ways your problem can be solved and none of them use the on_console_command event.

1/ First is to use the LuaCommandProcessor. With the following:

Code: Select all

commands.add_command("check_something", nil, function() game.print("check something was called") end)
you would register a command and then when a player does `/check_something` from the console, this function will be called.

2/ Second (based on your specific use case) would be to have an event handler for `defines.events.on_research_finished` so the player does not need to issue or even remember any custom commands

User avatar
unfunf22
Inserter
Inserter
Posts: 31
Joined: Fri May 20, 2016 9:00 pm
Contact:

Re: how does "on_console_command" work?

Post by unfunf22 »

can you give me an example how defines.events.on_research_finished would work because the last time i tinker with this kind of event my ups and fps where dropping like steel balls.

i have tested this function but it gave me an error at the moment i finished engine research. something like attempted to index player nil.

Code: Select all

script.on_event(defines.events.on_research_finished,function(event) --this code is just for scenario because some scenarios wont load the mod.

	if event.research.name == "engine" then
		game.player.force.technologies['wind-turbine'].enabled=true
	end
	if event.research.name == "electric-engine" and event.research.name == "wind-turbine" then
		game.player.force.technologies['wind-turbine2'].enabled=true
	end
	if event.research.name == "advanced-electronics-2" and event.research.name == "wind-turbine2" then
		game.player.force.technologies['wind-turbine3'].enabled=true
	end
	
	--old and janky
	--if game.player.force.technologies['engine'].researched=true then
	--	game.player.force.technologies['wind-turbine'].enabled=true
	--end
	
	--if game.player.force.technologies['wind-turbine'].researched=true and game.player.force.technologies['electric-engine'].researched=true then
	--game.player.force.technologies['wind-turbine2'].enabled=true
	--end
	
	--if game.player.force.technologies['wind-turbine2'].researched=true and game.player.force.technologies['advanced-electronics-2'].researched=true then
	--game.player.force.technologies['wind-turbine3'].enabled=true
	--end
	
end)

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: how does "on_console_command" work?

Post by DaveMcW »

Use event.research.force instead of game.player.force (which only exists in the console).

User avatar
unfunf22
Inserter
Inserter
Posts: 31
Joined: Fri May 20, 2016 9:00 pm
Contact:

Re: how does "on_console_command" work?

Post by unfunf22 »

thanks that fixes my problem now the mod works as I had desired and of course thank you boskid for directing me in the right direction.

Post Reply

Return to “Modding help”