Fixes bug in scriptset where lookup would no longer point to valid script after removing one preceding it.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-06-05 12:39:29 +02:00
parent b0e2e9935a
commit 23efc9b6ed
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 24 additions and 2 deletions

View File

@ -52,10 +52,19 @@ namespace CreatureLib::Battling {
void Remove(u32 keyHash) {
auto v = _lookup.TryGet(keyHash);
if (v.has_value()) {
auto script = _scripts[v.value()];
auto index = v.value();
auto script = _scripts[index];
script->OnRemove();
_scripts.Remove(v.value());
_lookup.Remove(keyHash);
// This is extremely inefficient. We need to do something better for this.
ResetLookup();
}
}
void ResetLookup() {
_lookup.Clear();
for (size_t i = 0; i < _scripts.Count(); ++i) {
_lookup.Insert(_scripts[i]->GetName(), i);
}
}

View File

@ -84,4 +84,17 @@ TEST_CASE("Add two scripts to script set, then remove them") {
REQUIRE(it.At(0)->GetName() == "foobar2");
}
TEST_CASE("Add two scripts to script set, remove the first, get second by name") {
auto set = ScriptSet();
auto s = new TestScript("foobar");
auto s2 = new TestScript("foobar2");
set.Add(s);
set.Add(s2);
REQUIRE(set.Count() == 2);
set.Remove("foobar"_cnc.GetHash());
REQUIRE(set.Count() == 1);
auto retrieved = set.Get("foobar2"_cnc);
REQUIRE(retrieved == s2);
}
#endif