Page 1 of 1

how does "on_console_command" work?

Posted: Mon Oct 04, 2021 10:25 am
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

Re: how does "on_console_command" work?

Posted: Mon Oct 04, 2021 10:47 am
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

Re: how does "on_console_command" work?

Posted: Mon Oct 04, 2021 5:29 pm
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)

Re: how does "on_console_command" work?

Posted: Mon Oct 04, 2021 10:55 pm
by DaveMcW
Use event.research.force instead of game.player.force (which only exists in the console).

Re: how does "on_console_command" work?

Posted: Tue Oct 05, 2021 9:07 am
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.