[LuaKeyLoader] Moved callback inside key definition.

This commit is contained in:
Quentin Bazin 2020-06-19 03:47:22 +02:00
parent 5d741b30ee
commit ea38e6d397
4 changed files with 18 additions and 10 deletions

View File

@ -7,11 +7,11 @@ mod:key {
id = "inventory",
name = "Inventory",
default_key = "E"
}
mod:key_callback("default:inventory", function(client, screen_width, screen_height, gui_scale)
show_inventory(client, screen_width, screen_height, gui_scale)
end)
key_callback = function(client, screen_width, screen_height, gui_scale)
show_inventory(client, screen_width, screen_height, gui_scale)
end
}
```
## Attributes

View File

@ -93,10 +93,10 @@ end
mod:key {
id = "inventory",
name = "Inventory",
default_key = "E"
default_key = "E",
key_callback = function(client, screen_width, screen_height, gui_scale)
show_inventory(client, screen_width, screen_height, gui_scale)
end
}
mod:key_callback("default:inventory", function(client, screen_width, screen_height, gui_scale)
show_inventory(client, screen_width, screen_height, gui_scale)
end)

View File

@ -31,6 +31,8 @@
#include <gk/core/IntTypes.hpp>
#include <sol/sol.hpp>
#include "ISerializable.hpp"
#include "NetworkUtils.hpp"
@ -51,6 +53,8 @@ class Key : public ISerializable {
const std::string &defaultKey() const { return m_defaultKey; }
void setDefaultKey(const std::string &defaultKey) { m_defaultKey = defaultKey; }
void setCallback(const sol::unsafe_function &callback) { m_callback = callback; }
private:
u16 m_id;
@ -58,6 +62,8 @@ class Key : public ISerializable {
std::string m_name;
std::string m_defaultKey;
sol::unsafe_function m_callback;
};
#endif // KEY_HPP_

View File

@ -33,6 +33,8 @@ void LuaKeyLoader::loadKey(const sol::table &table) const {
std::string name = table["name"].get_or<std::string>(stringID);
std::string defaultKey = table["default_key"].get_or<std::string>("");
Registry::getInstance().registerKey(stringID, name).setDefaultKey(defaultKey);
auto &key = Registry::getInstance().registerKey(stringID, name);
key.setDefaultKey(defaultKey);
key.setCallback(table["callback"]);
}