From 3a5fa268dd0fc52818fb1389c8e212e5b4f9e895 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sun, 19 Sep 2021 12:57:11 +0200 Subject: [PATCH] Fixes Pokemon::get_Nickname in angelscript. --- src/Battling/Pokemon/CreatePokemon.cpp | 6 ++++ src/Battling/Pokemon/CreatePokemon.hpp | 4 ++- .../Battling/RegisterPokemonClass.cpp | 2 +- .../ScriptTypeTests/Battle/PokemonTests.cpp | 29 ++++++++++--------- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Battling/Pokemon/CreatePokemon.cpp b/src/Battling/Pokemon/CreatePokemon.cpp index 2c058ef..8b7ad54 100644 --- a/src/Battling/Pokemon/CreatePokemon.cpp +++ b/src/Battling/Pokemon/CreatePokemon.cpp @@ -159,4 +159,10 @@ namespace PkmnLib::Battling { _allowedExperienceGain = value; return *this; } + + CreatePokemon& CreatePokemon::WithNickname(const std::string& nickname) { + _nickname = nickname; + return *this; + } + } \ No newline at end of file diff --git a/src/Battling/Pokemon/CreatePokemon.hpp b/src/Battling/Pokemon/CreatePokemon.hpp index 7212c5f..b533b3d 100644 --- a/src/Battling/Pokemon/CreatePokemon.hpp +++ b/src/Battling/Pokemon/CreatePokemon.hpp @@ -9,7 +9,7 @@ namespace PkmnLib::Battling { ArbUt::StringView _species = ""_cnc; ArbUt::StringView _forme = "default"_cnc; level_int_t _level; - std::string _nickname = ""; + std::optional _nickname = ""; ArbUt::StringView _ability = ""_cnc; ArbUt::StringView _nature; @@ -66,6 +66,8 @@ namespace PkmnLib::Battling { CreatePokemon& WithNature(const ArbUt::StringView& nature); CreatePokemon& IsAllowedExperienceGain(bool value); + CreatePokemon& WithNickname(const std::string& nickname); + Pokemon* Build() { auto rand = ArbUt::Random(); diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp index d7458e2..5078dff 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp @@ -143,7 +143,7 @@ void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) { r = engine->RegisterObjectMethod("Pokemon", "uint32 get_CurrentHealth() const property", asMETHOD(PkmnLib::Battling::Pokemon, GetCurrentHealth), asCALL_THISCALL); Ensure(r >= 0); - r = engine->RegisterObjectMethod("Pokemon", "const string& get_Nickname() const property", + r = engine->RegisterObjectMethod("Pokemon", "string get_Nickname() const property", asFUNCTION(NicknameWrapper), asCALL_CDECL_OBJFIRST); Ensure(r >= 0); r = engine->RegisterObjectMethod("Pokemon", "const constString& get_ActiveAbility() const property", diff --git a/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp b/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp index ec07777..2bcdc55 100644 --- a/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp @@ -187,20 +187,21 @@ TEST_CASE("Validate Pokemon CurrentHealth in Script") { } TEST_CASE("Validate Pokemon Nickname in Script") { - // auto mainLib = TestLibrary::GetLibrary(); - // auto data = GetScript(mainLib, "testNickname"_cnc); - // - // auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies"_cnc, 30) - // .WithForme("default"_cnc) - // .WithGender(CreatureLib::Library::Gender::Male) - // .Build(); - // data.Context->SetArgObject(0, (void*)mon); - // auto name = std::string(mon->GetNickname()); - // data.Context->SetArgAddress(1, &name); - // - // REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); - // REQUIRE((bool)data.Context->GetReturnWord()); - // delete mon; + auto mainLib = TestLibrary::GetLibrary(); + auto data = GetScript(mainLib, "testNickname"_cnc); + + auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies"_cnc, 30) + .WithForme("default"_cnc) + .WithGender(CreatureLib::Library::Gender::Male) + .WithNickname("foobar") + .Build(); + data.Context->SetArgObject(0, (void*)mon); + auto name = std::string(mon->GetNickname().value()); + data.Context->SetArgAddress(1, &name); + + REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); + REQUIRE((bool)data.Context->GetReturnWord()); + delete mon; } TEST_CASE("Validate Pokemon Active Ability in Script") {