From d24ccde864168eda66b093cdadcee197f10c4456 Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Mon, 2 Oct 2023 11:00:02 -0300 Subject: [PATCH] World:rayCast and World:queryBoundingBox have user args passed through to the callback function. Fixes #1194 --- src/modules/physics/box2d/World.cpp | 10 ++++++++-- src/modules/physics/box2d/World.h | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/modules/physics/box2d/World.cpp b/src/modules/physics/box2d/World.cpp index 0b7ec8653..bdda75ccd 100644 --- a/src/modules/physics/box2d/World.cpp +++ b/src/modules/physics/box2d/World.cpp @@ -147,6 +147,7 @@ World::QueryCallback::QueryCallback(World *world, lua_State *L, int idx) , funcidx(idx) { luaL_checktype(L, funcidx, LUA_TFUNCTION); + userargs = lua_gettop(L) - funcidx; } World::QueryCallback::~QueryCallback() @@ -162,7 +163,9 @@ bool World::QueryCallback::ReportFixture(b2Fixture *fixture) if (!f) throw love::Exception("A fixture has escaped Memoizer!"); luax_pushtype(L, f); - lua_call(L, 1, 1); + for (int i = 1; i <= userargs; i++) + lua_pushvalue(L, funcidx + i); + lua_call(L, 1 + userargs, 1); bool cont = luax_toboolean(L, -1); lua_pop(L, 1); return cont; @@ -199,6 +202,7 @@ World::RayCastCallback::RayCastCallback(World *world, lua_State *L, int idx) , funcidx(idx) { luaL_checktype(L, funcidx, LUA_TFUNCTION); + userargs = lua_gettop(L) - funcidx; } World::RayCastCallback::~RayCastCallback() @@ -220,7 +224,9 @@ float World::RayCastCallback::ReportFixture(b2Fixture *fixture, const b2Vec2 &po lua_pushnumber(L, normal.x); lua_pushnumber(L, normal.y); lua_pushnumber(L, fraction); - lua_call(L, 6, 1); + for (int i = 1; i <= userargs; i++) + lua_pushvalue(L, funcidx + i); + lua_call(L, 6 + userargs, 1); if (!lua_isnumber(L, -1)) luaL_error(L, "Raycast callback didn't return a number!"); float fraction = (float) lua_tonumber(L, -1); diff --git a/src/modules/physics/box2d/World.h b/src/modules/physics/box2d/World.h index ff81271ec..669e39921 100644 --- a/src/modules/physics/box2d/World.h +++ b/src/modules/physics/box2d/World.h @@ -100,6 +100,7 @@ class World : public Object, public b2ContactListener, public b2ContactFilter, p World *world; lua_State *L; int funcidx; + int userargs; }; class CollectCallback : public b2QueryCallback @@ -124,6 +125,7 @@ class World : public Object, public b2ContactListener, public b2ContactFilter, p World *world; lua_State *L; int funcidx; + int userargs; }; /**