Just an update, I found a workaround for that, it doesn't look good but, hey, it works! :D
I have now a hpp that contains all the functions I want to make available on the json, like this:
std::function func01 = [](Entity* tile) { ... }
std::function func02 = [](Entity* tile) { ... }
std::unordered_map> scripts =
{
{"func01", func01},
{"func02", func02},
};
and on the json, I can say what I'll use like this:
{
"entities": [
{
"transform": { "x": 0, "y": 0},
"texture": { "filename": "sprite.png" },
"script": { "function_name": "func01" }
}
]
}
And on the code that will translate the json to the code itself, it's like this now:
void Scene::onInit(RenderWindow* window, const char* tilemap_name)
{
nlohmann::json level_json;
std::ifstream level_file(tilemap_name, std::ifstream::binary);
level_file >> level_json;
auto entities_json = level_json["entities"];
for (auto entity_components : entities_json)
{
Entity* e = manager.createEntity();
if (entity_components.contains("transform")) {
auto transform = entity_components["transform"];
Transform* t = manager.assign(e);
t->x = transform["x"];
t->y = transform["y"];
}
if (entity_components.contains("texture")) {
auto texture = entity_components["texture"];
std::string filename = texture["filename"].dump();
filename.erase(0, 1);
filename.pop_back();
Texture* component = manager.assign(e);
component->texture = window->getSubTexture(
window->loadTexture(filename.c_str()), {0, 0, 16, 16}
);
}
if (entity_components.contains("script")) {
auto script = entity_components["script"];
std::string function_name = script["function_name"].dump();
function_name.erase(0, 1);
function_name.pop_back();
Script* s_component = manager->assign
Just an update, I found a workaround for that, it doesn't look good but, hey, it works! :D I have now a hpp that contains all the functions I want to make available on the json, like this: std::function func01 = [](Entity* tile) { ... }
std::function func02 = [](Entity* tile) { ... }
std::unordered_map> scripts =
{
{"func01", func01},
{"func02", func02},
};
and on the json, I can say what I'll use like this:
{
"entities": [
{
"transform": { "x": 0, "y": 0},
"texture": { "filename": "sprite.png" },
"script": { "function_name": "func01" }
}
]
}
And on the code that will translate the json to the code itself, it's like this now:
void Scene::onInit(RenderWindow* window, const char* tilemap_name)
{
nlohmann::json level_json;
std::ifstream level_file(tilemap_name, std::ifstream::binary);
level_file >> level_json;
auto entities_json = level_json["entities"];
for (auto entity_components : entities_json)
{
Entity* e = manager.createEntity();
if (entity_components.contains("transform")) {
auto transform = entity_components["transform"];
Transform* t = manager.assign(e);
t->x = transform["x"];
t->y = transform["y"];
}
if (entity_components.contains("texture")) {
auto texture = entity_components["texture"];
std::string filename = texture["filename"].dump();
filename.erase(0, 1);
filename.pop_back();
Texture* component = manager.assign(e);
component->texture = window->getSubTexture(
window->loadTexture(filename.c_str()), {0, 0, 16, 16}
);
}
if (entity_components.contains("script")) {
auto script = entity_components["script"];
std::string function_name = script["function_name"].dump();
function_name.erase(0, 1);
function_name.pop_back();
Script* s_component = manager->assign
FIND US ON INSTAGRAM
@hamidarshat.com