how to make a key configuration so that player can change keyboard keys for forwards, backwards, jump…etc
is there anyone here have a .blend example on who to change keyboard key in game?
something like:
forwards = set a key
backwards = set a key
jump = set a key…etc
Executing such a script will override the default setting that you entered via GUI.
To store/restore the sensors of all objects you can use this code:
import bge
try:
keyboardSettings = bge.logic.globalDict["keyboardSettings"]
except KeyError:
keyboardSettings = {}
bge.logic.globalDict["keyboardSettings"] = keyboardSettings
def storeKeyboardSensors(controller):
if not all(sensor.positive for sensor in controller.sensors):
return
scene = controller.owner.scene
for object in scene.objects:
sensorSettings = {}
keyboardSettings[object.name] = sensorSettings
for sensor in object.sensors:
try:
sensorSettings [sensor.name] = sensor.key
except AttributeError:
pass
def restoreKeyboardSensors(controller):
if not all(sensor.positive for sensor in controller.sensors):
return
scene = controller.owner.scene
for objectName in keyboardSettings:
object = scene.objects[objectName]
sensorSettings = keyboardSettings[objectName]
for sensorName in sensorSettings:
try:
object.sensors[sensorName].key = sensorSettings[sensorName]
except KeyError:
pass
The resulting change to the storage (globalDict) can be saved to file by using a saveActuator. You can load form file with the loadActuator. Be aware you need to store the keyboard mapping BEFORE saving and restore the mapping AFTER loading.
Remarks:
this code does not care additional keys (there can be up to two modifier keys)
this code does not provide an keyboard mapping editor (the snippet is simple code)
this mapping snippets expect “Cube” with the keyboard sensors “turn CCW” and “turn CW”
players can change all keys to one key, they can make W for forwarded, backward,jump…etc is there a way to fix this , so if you repeat a key it won’t save the change and a message appears saying error repeated key or something.
thank you
I suggest you create a “keyboard remapper”. I mean an object or multiple objects with the sole purpose to allow the user to change the settings.
It should contain a save/loader to save and load the current mapping to file.
On top of that you need a remapping editor. The editor provides a GUI and logic to change the key mapping. It can check what key is already assigned and prevent using it again (e.g. by showing a dialog to confirm removing the old assignment and assign it to the new input). Obviously your editor need to know all mappings.