From dfcdfd83439721bbf22ebb853b11d6fb291ea5ac Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Tue, 24 Aug 2021 20:43:01 +0200 Subject: [PATCH] Further fixes for setting pokemon held items from angelscript. --- .../Battling/RegisterPokemonClass.cpp | 5 ++++- .../ScriptTypeTests/Battle/PokemonTests.cpp | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp index 30f531c..106b4bc 100644 --- a/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp +++ b/src/ScriptResolving/AngelScript/TypeRegistry/Battling/RegisterPokemonClass.cpp @@ -77,6 +77,9 @@ CScriptArray* GetMoves(const PkmnLib::Battling::Pokemon* obj) { static bool HasHeldItem(const PkmnLib::Battling::Pokemon* obj, const ArbUt::StringView& str) { return obj->HasHeldItem(str.GetHash()); } +static void SetHeldItemWrapper(PkmnLib::Battling::Pokemon* obj, const ArbUt::StringView& str) { + return obj->SetHeldItem(str); +} static std::string NicknameWrapper(const PkmnLib::Battling::Pokemon* obj) { return std::string(obj->GetNickname()); } @@ -126,7 +129,7 @@ void RegisterPokemonClass::RegisterPokemonType(asIScriptEngine* engine) { Ensure(r >= 0); r = engine->RegisterObjectMethod( "Pokemon", "void SetHeldItem(const constString &in name)", - asMETHODPR(PkmnLib::Battling::Pokemon, SetHeldItem, (const ArbUt::BasicStringView&), void), asCALL_THISCALL); + asFUNCTION(SetHeldItemWrapper), asCALL_CDECL_OBJFIRST); Ensure(r >= 0); r = engine->RegisterObjectMethod("Pokemon", "void SetHeldItem(const Item@ item)", asMETHODPR(PkmnLib::Battling::Pokemon, SetHeldItem, diff --git a/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp b/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp index bc0162d..94724db 100644 --- a/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp +++ b/tests/ScriptTests/ScriptTypeTests/Battle/PokemonTests.cpp @@ -25,6 +25,7 @@ class testScript1 : PkmnScript { void testHeal(Pokemon@ p, uint32 amount){ p.Heal(amount); } bool testMove(Pokemon@ p, uint index, LearnedMove@ move){ return p.GetMoves()[index] is move; } bool testHasHeldItem(Pokemon@ p, const constString &in item){ return p.HasHeldItem(item); } + void testSetHeldItem(Pokemon@ p, const constString &in item){ p.SetHeldItem(item); } }} )"}}; @@ -333,4 +334,24 @@ TEST_CASE("Validate Pokemon HasHeldItem in Script") { delete mon; } +TEST_CASE("Test Pokemon SetHeldItem in Script") { + auto mainLib = TestLibrary::GetLibrary(); + + auto mon = PkmnLib::Battling::CreatePokemon(mainLib, "testSpecies3"_cnc, 30) + .WithForme("default"_cnc) + .Build(); + auto data = GetScript(mainLib, "testSetHeldItem"_cnc); + + data.Context->SetArgObject(0, const_cast(mon)); + ArbUt::StringView item = "testItem"_cnc; + data.Context->SetArgAddress(1, &item); + + REQUIRE(data.Context->Execute() == asEXECUTION_FINISHED); + REQUIRE((bool)data.Context->GetReturnDWord()); + REQUIRE(mon->HasHeldItem("testItem"_cnc)); + + delete mon; +} + + #endif \ No newline at end of file