Skip to content

Commit

Permalink
[GameState|Key] Fixed registry access for singleplayer games.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 19, 2020
1 parent 6f59f92 commit 7e9d26b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/lua-api-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
mod:key {
id = "inventory",
name = "Inventory",
default_key = "E"
default_key = "E",

callback = function(client, screen_width, screen_height, gui_scale)
show_inventory(client, screen_width, screen_height, gui_scale)
Expand Down
4 changes: 2 additions & 2 deletions source/client/states/GameState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void GameState::onEvent(const sf::Event &event) {
gk::Mouse::setCursorVisible(false);
}
else if (event.type == sf::Event::KeyPressed) {
for (auto &key : m_registry.keys()) {
for (auto &key : Registry::getInstance().keys()) {
if (event.key.code == key.keycode()) {
m_clientCommandHandler.sendKeyPressed(key.id());
}
Expand Down Expand Up @@ -152,7 +152,7 @@ void GameState::update() {
}

if (!m_areModKeysLoaded) {
for (auto &it : m_registry.keys()) {
for (auto &it : Registry::getInstance().keys()) {
m_keyboardHandler->addKey(it.id(), it.name(), it.keycode(), it.stringID(), &it);
}

Expand Down
17 changes: 8 additions & 9 deletions source/common/world/Key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,8 @@ class Key : public ISerializable {
Key(u16 id, const std::string &stringID, const std::string &name)
: m_id(id), m_stringID(stringID), m_name(name) {}

void serialize(sf::Packet &packet) const override {
packet << m_id << m_stringID << m_name << m_defaultKey;
}

void deserialize(sf::Packet &packet) override {
packet >> m_id >> m_stringID >> m_name >> m_defaultKey;
m_keycode = gk::KeyboardUtils::getKeyFromName(m_defaultKey);
}
void serialize(sf::Packet &packet) const override { packet << m_id << m_stringID << m_name << m_defaultKey; }
void deserialize(sf::Packet &packet) override { packet >> m_id >> m_stringID >> m_name >> m_defaultKey; }

u16 id() const { return m_id; }

Expand All @@ -64,7 +58,12 @@ class Key : public ISerializable {
void setKeycode(sf::Keyboard::Key keycode) { m_keycode = keycode; if (m_parent) m_parent->m_keycode = keycode; }

const std::string &defaultKey() const { return m_defaultKey; }
void setDefaultKey(const std::string &defaultKey) { m_defaultKey = defaultKey; }
void setDefaultKey(const std::string &defaultKey) {
m_defaultKey = defaultKey;

if (m_keycode == sf::Keyboard::Unknown)
m_keycode = gk::KeyboardUtils::getKeyFromName(m_defaultKey);
}

const sol::unsafe_function &callback() const { return m_callback; }
void setCallback(const sol::unsafe_function &callback) { m_callback = callback; }
Expand Down

0 comments on commit 7e9d26b

Please sign in to comment.