This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Okay so I have a input set up, where the player can choose the button configuration. However this gets reset when the game is closed, and the player will have to do it again.

I want to save the new input action created by the player. To eliminate this need.

However I'm having trouble, as
A. Saving stores it as a string, Not a InputEvent
B. Loading as a string, fails when converting to an InputMap

Which results in this error
Invalid type in function 'action_add_event' in base 'InputMap'. Cannot convert argument 2 from String to InputEvent.
Here's the complete code for context:

func save_game(string_file = "user://data.sav"):
var file = File.new()
file.open(string_file,file.WRITE)
var data=[]
#Save Options
var options_data = {}
var op_input_data = {}
for i in InputMap.get_actions():
    var input_actions = InputMap.get_action_list(i)
    op_input_data[str(i)] = input_actions
options_data["Inputs"] = op_input_data
options_data["Level"] = Globals.get("v/current_level")
data.append(options_data)
#Save Game
var game_data = {}
data.append(game_data)
#print(data)
file.store_var(data)
file.close()
func load_game(string_file = "user://data.sav"):
print("Loading...")
var file = File.new()
file.open(string_file,file.READ)
#Load Options
var data = file.get_var()
var options_data = data[0]#To get the array index, 
if options_data.has("Inputs"):#Inputs
    for i in options_data["Inputs"]:
        InputMap.add_action(str(i))
        InputMap.action_add_event(i,i[0])
if options_data.has("Level"): #Current_level, Disable this if testing another level
    var sav_level = options_data["Level"]
    get_tree().change_scene(sav_level)
print(data)
#Load Game WIP
file.close()
in Engine by (275 points)

I have no idea why enter code here has thrown the formatting off. Let me reassure you the formatting is correct in the Godot Editor, Else I wouldn't be getting Invalid type in function 'action_add_event' in base 'InputMap'. Cannot convert argument 2 from String to InputEvent.

1 Answer

+1 vote

There's a demo project that demonstrates configurable persistent input mapping.
In Godot 3.0 under the Templates tab of the project manager you can find it as "Gui Input Mapping Demo".

For key events, you use InputEventKey.scancode, OS.get_scancode_string, and OS. find_scancode_from_string to convert input events to strings and back.

by (291 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.