I have simple yet effective system how player can re-define input. Problem is saving to file and loading it back. I was using File.store_var() to save InputEvent to file (wile supporting InputEventMouseButton, InputEventJoypadButton and InputEventKey) and File.get_var() to load it back. It worked just fine in 3.1.0. However after updating to 3.1.1, instead of required input event, File.get_var() returns instance of something called EncodedObjectID. Should I use different method to save/load to file or somehow convert EncodedObjectAsID to input event. Why was this changed from 3.1 to 3.1.1

Huh, well I was going to suggest using get_var and store_var, but seeing as that doesn't work, I do not know. I've always just used the get/store var functions. I am also curious as to why that was changed.

You might be able to make a InputEvent and then pass that in. This QnA answer shows how to make a InputEvent through code and looking at the snippets posted, it looks like you'll need to store the scancode for keys and the button index for the mouse (I guess scancode for joysticks?)

That is really strange that it is not working with get/store var anymore. Hopefully this helps though!

@TwistedTwigleg said: Huh, well I was going to suggest using get_var and store_var, but seeing as that doesn't work, I do not know. I've always just used the get/store var functions. I am also curious as to why that was changed.

You might be able to make a InputEvent and then pass that in. This QnA answer shows how to make a InputEvent through code and looking at the snippets posted, it looks like you'll need to store the scancode for keys and the button index for the mouse (I guess scancode for joysticks?)

That is really strange that it is not working with get/store var anymore. Hopefully this helps though!

So instead of storing whole event, I need to store its components and build event on fly.... Dang, why was this changed

Well, this should do it right ?

func save_key(var file : File,var key : InputEvent):
	if key is InputEventMouseButton:
		file.store_var(0)
		file.store_var(key.device)
		file.store_var(key.button_index)
	elif key is InputEventJoypadButton:
		file.store_var(1)
		file.store_var(key.device)
		file.store_var(key.button_index)
	elif key is InputEventKey : 
		file.store_var(2)
		file.store_var(key.scancode)

func load_key(var file : File):
	var a = file.get_var()
	var b = null
	if a == 0:
		b = create_input_event(InputEventMouseButton, file.get_var(),file.get_var())
	elif a == 1:
		b = create_input_event(InputEventJoypadButton, file.get_var(),file.get_var())
	elif a == 2:
		b = create_input_event(InputEventKey, null,file.get_var())
	return b

func create_input_event(var type, var device_Id, var key):
	var event = type.new()
	if event is InputEventMouseButton or event is InputEventJoypadButton:
		event.device = device_Id
		event.button_index = key
	elif event is InputEventKey:
		event.scancode = key
	return event

(there was mistake before. I repaired it so it won't explode if someone try to copy-paste it)

@GarromOrcShaman said: So instead of storing whole event, I need to store its components and build event on fly.... Dang, why was this changed

Looks like, unfortunately. It might be that the change is a regression/bug though. Maybe make a issue on GitHub to see? I dunno, it seems strange they would change it like that.

(Also, I edited your post slightly)

Well, this should do it right ?

func save_key(var file : File,var key : InputEvent):
	if key is InputEventMouseButton:
		file.store_var(0)
		file.store_var(key.device)
		file.store_var(key.button_index)
	elif key is InputEventJoypadButton:
		file.store_var(1)
		file.store_var(key.device)
		file.store_var(key.button_index)
	elif key is InputEventKey : 
		file.store_var(2)
		file.store_var(key.scancode)

func load_key(var file : File):
	var a = file.get_var()
	var b = null
	if a == 0:
		b = create_input_event(InputEventMouseButton, file.get_var(),file.get_var())
	elif a == 1:
		b = create_input_event(InputEventJoypadButton, file.get_var(),file.get_var())
	elif a == 1:
		b = create_input_event(InputEventKey, null,file.get_var())
	return b

func create_input_event(var type, var device_Id, var key):
	var event = type.new()
	if event is InputEventMouseButton or event is InputEventJoypadButton:
		event.device = device_Id
		event.button_index = key
	elif event is InputEventKey:
		event.scancode = key
	return event

There is one thing thing you might want to change:

  • In the load_key function you have elif a == 1: twice. Looking at the code, I'm assume the second if is supposed to be elif a == 2:

Other than that, everything looks like it should work, though without testing the code I cannot say for sure.

@TwistedTwigleg said:

@GarromOrcShaman said: So instead of storing whole event, I need to store its components and build event on fly.... Dang, why was this changed

Looks like, unfortunately. It might be that the change is a regression/bug though. Maybe make a issue on GitHub to see? I dunno, it seems strange they would change it like that.

(Also, I edited your post slightly)

Well, this should do it right ?

func save_key(var file : File,var key : InputEvent):
	if key is InputEventMouseButton:
		file.store_var(0)
		file.store_var(key.device)
		file.store_var(key.button_index)
	elif key is InputEventJoypadButton:
		file.store_var(1)
		file.store_var(key.device)
		file.store_var(key.button_index)
	elif key is InputEventKey : 
		file.store_var(2)
		file.store_var(key.scancode)

func load_key(var file : File):
	var a = file.get_var()
	var b = null
	if a == 0:
		b = create_input_event(InputEventMouseButton, file.get_var(),file.get_var())
	elif a == 1:
		b = create_input_event(InputEventJoypadButton, file.get_var(),file.get_var())
	elif a == 1:
		b = create_input_event(InputEventKey, null,file.get_var())
	return b

func create_input_event(var type, var device_Id, var key):
	var event = type.new()
	if event is InputEventMouseButton or event is InputEventJoypadButton:
		event.device = device_Id
		event.button_index = key
	elif event is InputEventKey:
		event.scancode = key
	return event

There is one thing thing you might want to change:

  • In the load_key function you have elif a == 1: twice. Looking at the code, I'm assume the second if is supposed to be elif a == 2:

Other than that, everything looks like it should work, though without testing the code I cannot say for sure.

Yes, you are right. It was supposed to be 2... Was kinda surprised why keyboard don't work. I put it to test and it works just right. So if anyone have same problem, feel free to copy-paste my code

4 years later