Skip to content

Commit

Permalink
Can now set translation from script
Browse files Browse the repository at this point in the history
  • Loading branch information
tomheeleynz committed Jul 20, 2023
1 parent c9b8461 commit 1835761
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
7 changes: 7 additions & 0 deletions Arcane/src/Arcane/Scripting/Script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ namespace Arcane
lua_setfield(L, -2, "HasComponent");
PrintStack(L);

lua_pushcfunction(L, ScriptingEngine::SetTransform);
PrintStack(L);

lua_setfield(L, -2, "SetTranslation");
PrintStack(L);


lua_rawgeti(L, LUA_REGISTRYINDEX, m_ObjectIndex);
PrintStack(L);

Expand Down
9 changes: 6 additions & 3 deletions Arcane/src/Arcane/Scripting/ScriptGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ namespace Arcane
const char* index = lua_tostring(L, -2);
if (strcmp(index, "x") == 0)
{
vec3->x = (int)lua_tonumber(L, -1);
vec3->x = (float)lua_tonumber(L, -1);
}
else if (strcmp(index, "y") == 0)
{
vec3->y = (int)lua_tonumber(L, -1);
vec3->y = (float)lua_tonumber(L, -1);
}
else if (strcmp(index, "z") == 0)
{
vec3->z = (int)lua_tonumber(L, -1);
vec3->z = (float)lua_tonumber(L, -1);
}
else
{
Expand All @@ -222,6 +222,7 @@ namespace Arcane
return 1;
};


lua_pushstring(L, "__index");
lua_pushcfunction(L, Vector3Index);
lua_settable(L, -3);
Expand Down Expand Up @@ -278,8 +279,10 @@ namespace Arcane
auto CreateTransformComponent = [](lua_State* L) -> int {
void* ptrToTransformComponent = lua_newuserdata(L, sizeof(TransformComponent));
new (ptrToTransformComponent) TransformComponent();

luaL_getmetatable(L, "TransformComponentMetatable");
lua_setmetatable(L, -2);

return 1;
};

Expand Down
10 changes: 4 additions & 6 deletions Arcane/src/Arcane/Scripting/ScriptingEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,7 @@ namespace Arcane
TransformComponent& transformComponent = entity.GetComponent<TransformComponent>();
TransformComponent** transformUserData = (TransformComponent**)lua_newuserdata(L, sizeof(TransformComponent));
*transformUserData = &transformComponent;
PrintStack(L);

luaL_getmetatable(L, "TransformComponentMetatable");
PrintStack(L);

lua_setmetatable(L, -2);
}

Expand Down Expand Up @@ -117,8 +113,10 @@ namespace Arcane

int ScriptingEngine::SetTransform(lua_State* L)
{
PrintStack(L);

glm::vec3* newTranslation = (glm::vec3*)lua_touserdata(L, -1);
lua_getfield(L, -2, "EntityId");
Entity entity = *(Entity*)lua_touserdata(L, -1);
entity.GetComponent<TransformComponent>().pos = *newTranslation;
return 0;
}

Expand Down

0 comments on commit 1835761

Please sign in to comment.