Code execution by button

From Mod Wiki

Revision as of 13:32, 6 July 2008 by Atrocious (Talk | contribs)
(diff) ←Older revision | Current revision (diff) | Newer revision→ (diff)
Jump to: navigation, search

It is not possible to create new in-game hotkeys for STALKER, but you can check key inputs when you are in the main menu. This could be used to create an antirad pseudo-hotkey (like described below) or to execute any other code, for example to gather coordinates on key press.

Extract or open gamedata\scripts\ui_main_menu.script and find the function main_menu:OnKeyboard(dik, keyboard_action) at the bottom. In vanilla 1.0005 it looks like this:

function main_menu:OnKeyboard(dik, keyboard_action)  --virtual function
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
	local bind = dik_to_bind(dik)
	local console = get_console()
 
	if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
		if dik == DIK_keys.DIK_ESCAPE then
			if level.present() and (db.actor ~= nil) and db.actor:alive() then
				console:execute("main_menu off")
			end
		end
 
--		if dik == DIK_keys.DIK_S then
--			self:OnButton_load_spawn()
 
--		else
		if dik == DIK_keys.DIK_Q then
			self:OnMessageQuitWin()
		end
 
	end
 
	return true
end

As you can see there are two (active) keys configured: ESC and Q. Now you can create another key and define the action that should happen on key press. I take NatVac's example for an anti-rad pseudo hotkey :

function main_menu:OnKeyboard(dik, keyboard_action)  --virtual function
	CUIScriptWnd.OnKeyboard(self,dik,keyboard_action)
	local bind = dik_to_bind(dik)
	local console = get_console()
 
	if keyboard_action == ui_events.WINDOW_KEY_PRESSED then
		if dik == DIK_keys.DIK_ESCAPE then
			if level.present() and (db.actor ~= nil) and db.actor:alive() then
				console:execute("main_menu off")
			end
		end
 
--		if dik == DIK_keys.DIK_S then
--			self:OnButton_load_spawn()
 
--		else
		if dik == DIK_keys.DIK_Q then
			self:OnMessageQuitWin()
		end
 
		if dik == DIK_keys.DIK_F1 then
			if level.present() and (db.actor ~= nil) and db.actor:alive() then
			  	if db.actor:object("antirad") ~= nil then
			  	         db.actor:eat(db.actor:object("antirad"))
			  	end
				console:execute("main_menu off")
			end
		end
 
	end
 
	return true
end

So by pressing ESC in game and then F1 as soon as the main menu opens, you can administer one antirad (if you have one) and the main menu will close. Of course you can also execute other code in external scripts by using my_script.my_function() instead of a code block inside the button event handler.

Personal tools