Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Save/Load keybindings  (Read 6298 times)

0 Members and 1 Guest are viewing this topic.

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
[SOLVED] Save/Load keybindings
« on: September 30, 2014, 09:59:18 am »
Hi,

I'm using ini-files to save and load keybindings. Using sf::Joystick is quite easy: I read/write the buttons' uint's. Handling axis is nearly the same, but I'm using a small function mapping "x" to sf::Joystick::Axis::X and so on. But doing this for keyboard (e.g. "w" to sf::Keyboard::W) isn't fun at all. Is there a clever way to do this? I want to avoid a large switch considering the entire sf::Keyboard enum. Maybe there's a way?
« Last Edit: January 26, 2015, 08:27:44 pm by Glocke »
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
AW: Save/Load keybindings
« Reply #1 on: September 30, 2014, 10:01:42 am »
You could take a look at Thor's action system.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Save/Load keybindings
« Reply #2 on: September 30, 2014, 10:23:15 am »
More precisely, the Thor header InputNames.hpp will help you :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Save/Load keybindings
« Reply #3 on: September 30, 2014, 10:31:42 am »
Well, (for me) it seems like Thor creates a hashmap to map sf::Keyboard::Key to std::string and vice versa..

What's about the enum values of sf::Keyboard::Key. Afaik every C-style enum is based on int-values (but aliased by the enum). So the sf::Keyboard::Key's should use int values at the internals. Are these values deterministic? The first two enum elements explicitly carry the values -1 and 0 - this might imply B as 1, C as 2 and so on. Testing this (means: converting to integer and printing to stdout) confirmed this - at least for the first 4-5 elements.
But these values should be deterministic, because of the two initial values, aren't they?
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Save/Load keybindings
« Reply #4 on: September 30, 2014, 11:14:49 am »
If deterministic means that every compiler translates the enumerator's values to the same integer: yes

If deterministic means that you can serialize the numbers and future versions will still be compatible: no
That's why you should prefer string serialization. It can also be read and written by humans.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Save/Load keybindings
« Reply #5 on: September 30, 2014, 11:22:12 am »
If deterministic means that you can serialize the numbers and future versions will still be compatible: no
That's why you should prefer string serialization. It can also be read and written by humans.

Ok thanks. I guess I'll adapt Thor's mapping approach in the near future.
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Save/Load keybindings
« Reply #6 on: September 30, 2014, 11:24:41 am »
You could also just use it ;)

If you don't want to install and link Thor, you can also copy the files (just keep the license). The advantage is, when SFML supports more keys, I will adapt Thor correspondingly, so your code works automatically with more keys.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Save/Load keybindings
« Reply #7 on: September 30, 2014, 11:36:31 am »
You could also just use it ;)

At the moment I don't want to add extra dependencies to my project. But I thinkg if I'll need more Thor-features later, I'll use it ;)

If you don't want to install and link Thor, you can also copy the files (just keep the license). The advantage is, when SFML supports more keys, I will adapt Thor correspondingly, so your code works automatically with more keys.

 8)
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

Rosme

  • Full Member
  • ***
  • Posts: 169
  • Proud member of the shoe club
    • View Profile
    • Code-Concept
Re: Save/Load keybindings
« Reply #8 on: October 10, 2014, 12:37:20 am »
You could also just use it ;)
At the moment I don't want to add extra dependencies to my project. But I thinkg if I'll need more Thor-features later, I'll use it ;)
Considering the license and how easy it is to use, the reason of adding extra dependencies shouldn't be. You'll find that Thor offers a lot of thing that can be usefull. This looks to me like a case of NIH.
GitHub
Code Concept
Twitter
Rosme on IRC/Discord

Glocke

  • Sr. Member
  • ****
  • Posts: 289
  • Hobby Dev/GameDev
    • View Profile
Re: Save/Load keybindings
« Reply #9 on: October 10, 2014, 09:16:09 am »
Well, that's why I finally added Thor to my project to load keybindings :)
Current project: Racod's Lair - Rogue-inspired Coop Action RPG

 

anything