1
General / Re: Initialize a Container of sf::Keyboard:Key and bool pairs
« on: April 14, 2021, 01:19:31 am »
Thanks kojack. This turned out to be a more layered question than I originally thought, but at this point, I think all parts of it have been clarified and answered, thanks to your reply filling the last gap. If there are any questions about any of the topics here that aren't clear, though, I'd be happy to discuss.
=== The Question Parts We've Answered ===
1. The wall I hit implementing this code originally, was what you point out. I had the static variable, without the instantiation in the .cpp file. For many other static variables throughout the application, I had used inline, but couldn't manage to make it work in this case (although I bet there is syntax that could work). In the original question, the .cpp instantiation is not there, but by a later time, I had discovered the answer and posted my finding as a reply, albeit with a different data structure -- but I didn't clarify that the same would have worked for the static std::array (so thanks for pointing it out).
2. I also wanted to inquire about other possible ways to make this work in case I was looking at the problem wrong or reinventing the wheel. With the discovery of the "event.key.control" and the like, a simple use case became evident, which will probably suffice for most people (ctrl,alt,shift,system).
3. Third, is what kind of datastructure is ideal to tackle the "every key" goal. I do think that the unordered map is clean and expressive, and also allows for 'at-reference-instantiation' of values (so that init isn't even necessary -- when you reference a key (as in key-value-pair key, not sfml key) in an unordered map that doesn't exist in that map, a default value will be created at the key). Shout out to eXpl0it3r
4. How to iterate the sf::Keyboard::Key enum. Which, is also solved!
So, I thank everyone for their contribution, and I believe (and hope) that this thread may help others. Also hidden here is a demonstration of how to implement a toggle in a game loop via the "checkToggle" function which I didn't include all of the code for, but did briefly explain via a comment.
Thanks again everyone
=== The Question Parts We've Answered ===
1. The wall I hit implementing this code originally, was what you point out. I had the static variable, without the instantiation in the .cpp file. For many other static variables throughout the application, I had used inline, but couldn't manage to make it work in this case (although I bet there is syntax that could work). In the original question, the .cpp instantiation is not there, but by a later time, I had discovered the answer and posted my finding as a reply, albeit with a different data structure -- but I didn't clarify that the same would have worked for the static std::array (so thanks for pointing it out).
2. I also wanted to inquire about other possible ways to make this work in case I was looking at the problem wrong or reinventing the wheel. With the discovery of the "event.key.control" and the like, a simple use case became evident, which will probably suffice for most people (ctrl,alt,shift,system).
3. Third, is what kind of datastructure is ideal to tackle the "every key" goal. I do think that the unordered map is clean and expressive, and also allows for 'at-reference-instantiation' of values (so that init isn't even necessary -- when you reference a key (as in key-value-pair key, not sfml key) in an unordered map that doesn't exist in that map, a default value will be created at the key). Shout out to eXpl0it3r
4. How to iterate the sf::Keyboard::Key enum. Which, is also solved!
So, I thank everyone for their contribution, and I believe (and hope) that this thread may help others. Also hidden here is a demonstration of how to implement a toggle in a game loop via the "checkToggle" function which I didn't include all of the code for, but did briefly explain via a comment.
Thanks again everyone