Solve a problem where if you give a name for cell and the name doesn't exist you get a crash during world query in GUECS.

master
Zed A. Shaw 1 week ago
parent 0e2f213871
commit 5a3b567fd1
  1. 25
      guecs.cpp
  2. 1
      guecs.hpp

@ -80,23 +80,24 @@ namespace guecs {
dbc::check(good, "LEL parsing failed."); dbc::check(good, "LEL parsing failed.");
for(auto& [name, cell] : $parser.cells) { for(auto& [name, cell] : $parser.cells) {
auto ent = entity(name); auto ent = init_entity(name);
$world.set<lel::Cell>(ent, cell); $world.set<lel::Cell>(ent, cell);
} }
} }
DinkyECS::Entity UI::init_entity(std::string name) {
auto entity = $world.entity();
// this lets you look up an entity by name
$name_ents.insert_or_assign(name, entity);
// this makes it easier to get the name during querying
$world.set<CellName>(entity, {name});
return entity;
}
DinkyECS::Entity UI::entity(std::string name) { DinkyECS::Entity UI::entity(std::string name) {
if($name_ents.contains(name)) { dbc::check($name_ents.contains(name),
// already exists so just return it fmt::format("GUECS entity {} does not exist. Forgot to init_entity?", name));
return $name_ents.at(name); return $name_ents.at(name);
} else {
auto entity = $world.entity();
// this lets you look up an entity by name
$name_ents.insert_or_assign(name, entity);
// this makes it easier to get the name during querying
$world.set<CellName>(entity, {name});
return entity;
}
} }
void UI::init() { void UI::init() {

@ -114,6 +114,7 @@ namespace guecs {
void position(int x, int y, int width, int height); void position(int x, int y, int width, int height);
void layout(std::string grid); void layout(std::string grid);
DinkyECS::Entity init_entity(std::string name);
DinkyECS::Entity entity(std::string name); DinkyECS::Entity entity(std::string name);
inline lel::CellMap& cells() { inline lel::CellMap& cells() {

Loading…
Cancel
Save