IIYAMA

[Verification] Binding keys ending up double/tripple

Recommended Posts

I have been trying to bindkeys to a command clientside and save them inside a xml file.

 

But the mta binding cache is stacking up. Keys: 'x', 'u', 'x' See screenshot bellow.

Spoiler

ccjtfrffdzeqt.png

 

The problem shows up when you edit the bindkey in the settings menu and load the xml file after saving it.

  • To use the bindkeys, you have to bind it with any key on your keyboard.
  • To know which bindkeys are bound to a command with your previous visit, you have to bind any key of your keyboard to that command in order to use: getBoundKeys / getKeyBoundToCommand
  • If you use unbindKey, it will unbind the command for all other keys, but not remove it from the cache.
  • And another problem, I can't remove keys/'alternative keys' from the cache unless I click on the 'Load defaults button on the right bottom'.

 

 

This is the code I have been testing with:

Spoiler


local bindsAndCommands = {
	{cmd = "useGadget", defaultKey = "z", keyState = "down"} 
	-- cmd = spaces in it!
}

local bindSettingsFileName = "bindSettings.xml"

local function bindSettingsLoading ()
	if not fileExists (bindSettingsFileName) then
		local file = xmlCreateFile(bindSettingsFileName, "cookie")
		if file then
			for i=1, #bindsAndCommands do
				local bindData = bindsAndCommands[i]
				local NewNode = xmlCreateChild(file, bindData.cmd)
				xmlNodeSetValue ( NewNode, bindData.defaultKey)
				if not getKeyBoundToCommand(bindData.cmd) then
					bindKey(bindData.defaultKey, bindData.keyState, bindData.cmd)
				end
			end		
			xmlSaveFile(file)
			xmlUnloadFile(file) 
		elseif fileExists (bindSettingsFileName) then -- corrupted
			fileDelete (bindSettingsFileName)
		end
	else
		local file = xmlLoadFile (bindSettingsFileName )
		if file then
			
			for i=1, #bindsAndCommands do
				local bindData = bindsAndCommands[i]
				local oldNode = xmlFindChild ( file, bindData.cmd, 0)

				local useThisKey = oldNode and xmlNodeGetValue(oldNode) or bindData.defaultKey
				bindKey(useThisKey, bindData.keyState, bindData.cmd)
				
			end
			
			xmlUnloadFile(file) 
		else -- corrupted
			fileDelete (bindSettingsFileName)
		end
	end
	removeEventHandler("onClientResourceStart", resourceRoot, bindSettingsLoading)
	bindSettingsLoading = nil
end





local bindSettingsStore = function ()
	if not fileExists (bindSettingsFileName) then
		local file = xmlCreateFile(bindSettingsFileName, "cookie")
		if file then
			for i=1, #bindsAndCommands do
				local bindData = bindsAndCommands[i]
				local NewNode = xmlCreateChild(file, bindData.cmd)
				xmlNodeSetValue ( NewNode, getKeyBoundToCommand(bindData.cmd) or bindData.defaultKey)
			end		
			xmlSaveFile(file)
			xmlUnloadFile(file)  
		elseif fileExists (bindSettingsFileName) then -- corrupted
			fileDelete (bindSettingsFileName)
		end
	else
		local file = xmlLoadFile (bindSettingsFileName)
		if file then
			for i=1, #bindsAndCommands do
				local bindData = bindsAndCommands[i]
				xmlNodeSetValue (
					xmlFindChild ( file, bindData.cmd, 0) or xmlCreateChild(file, bindData.cmd), 
					getKeyBoundToCommand(bindData.cmd) or bindData.defaultKey
				)
			end
			xmlSaveFile(file)
			xmlUnloadFile(file)  
		else -- corrupted
			fileDelete (bindSettingsFileName)
		end
	end
end


addEventHandler("onClientResourceStart", resourceRoot, bindSettingsLoading)
addEventHandler("onClientResourceStop", resourceRoot, bindSettingsStore)

 

 

Edited by IIYAMA

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.