Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Functions to move pawns #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions Scripts/uetorch.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ typedef struct {

struct UObject;
struct AActor;
struct UPrimitiveComponent;
struct UMaterial;
struct FName;
struct UPhysicsHandleComponent;

void GetViewportSize(IntSize* r);
bool CaptureScreenshot(IntSize* size, void* data);
Expand All @@ -46,17 +49,29 @@ bool GetActorVelocity(AActor* object, float* x, float* y, float* z);
bool GetActorAngularVelocity(AActor* object, float* x, float* y, float* z);
bool GetActorScale3D(AActor* object, float* x, float* y, float* z);
bool GetActorBounds(AActor* object, float* x, float* y, float* z, float* boxX, float* boxY, float* boxZ);
bool GetActorForwardVector(AActor* object, float *x, float *y, float *z);

bool SetActorLocation(AActor* object, float x, float y, float z);
bool SetActorRotation(AActor* object, float pitch, float yaw, float roll);
bool SetActorLocationAndRotation(AActor* object, float x, float y, float z, float pitch, float yaw, float roll);
void SetActorVisible(AActor* object, bool visible);
bool SetActorVisible(AActor* object, bool visible);
bool SetActorVelocity(AActor* object, float x, float y, float z);
bool SetActorAngularVelocity(AActor* object, float x, float y, float z);
bool SetActorScale3D(AActor* object, float x, float y, float z);

bool DestroyActor(AActor* object);

bool SetMaterial(AActor* object, UMaterial* material);
bool AddForce(AActor* object, float x, float y, float z);

bool SimpleMoveToLocation(UObject* _this, AActor* object, float x, float y, float z);
bool SimpleMoveToActor(UObject* _this, AActor* object, AActor* goal);

bool GrabComponent(UPhysicsHandleComponent* HandleComponent, UPrimitiveComponent* target, FName* InBoneName, float x, float y, float z);
bool ReleaseComponent(UPhysicsHandleComponent* HandleComponent);
bool SetTargetLocation(UPhysicsHandleComponent* component, float x, float y, float z);
bool WakeRigidBody(UPrimitiveComponent* component);
bool IgnoreCollisionWithPawn(UPrimitiveComponent* component);
]]

local utlib = ffi.C
Expand Down Expand Up @@ -501,16 +516,58 @@ function uetorch.GetActorBounds(actor)
return {x = x[0], y = y[0], z = z[0], boxX = boxX[0], boxY = boxY[0], boxZ = boxZ[0]}
end

function uetorch.GetActorForwardVector(actor)
local x = ffi.new('float[?]', 1)
local y = ffi.new('float[?]', 1)
local z = ffi.new('float[?]', 1)
if not utlib.GetActorForwardVector(actor,x,y,z) then
return nil
end
return {x = x[0], y = y[0], z = z[0]}
end

uetorch.SetActorLocation = utlib.SetActorLocation
uetorch.SetActorRotation = utlib.SetActorRotation
uetorch.SetActorLocationAndRotation = utlib.SetActorLocationAndRotation
uetorch.SetActorVisible = utlib.SetActorVisible
uetorch.SetActorVelocity = utlib.SetActorVelocity
uetorch.SetActorAngularVelocity = utlib.SetActorAngularVelocity
uetorch.SetActorScale3D = utlib.SetActorScale3D
uetorch.DestroyActor = utlib.DestroyActor
uetorch.SetMaterial = utlib.SetMaterial
uetorch.AddForce = utlib.AddForce
uetorch.SetResolution = utlib.SetResolution
uetorch.SetMouse = utlib.SetMouse

return uetorch
-------------------------------------------------------------------------------
--
-- Pawn control
--
-- see https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/APawn/index.html
-- and https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/GameFramework/AController/index.html
-- for further reference
-------------------------------------------------------------------------------

function uetorch.SimpleMoveToLocation(actor, x, y, z)
return utlib.SimpleMoveToLocation(this, actor, x, y, z)
end

function uetorch.SimpleMoveToActor(actor, goal)
return utlib.SimpleMoveToActor(this, actor, goal)
end

-------------------------------------------------------------------------------
--
-- Handling (Grabbing and releasing) of actors
--
-- see https://docs.unrealengine.com/latest/INT/BlueprintAPI/Physics/Components/PhysicsHandle/index.html
-- for further reference
-------------------------------------------------------------------------------

uetorch.GrabComponent = utlib.GrabComponent
uetorch.ReleaseComponent = utlib.ReleaseComponent
uetorch.SetTargetLocation = utlib.SetTargetLocation
uetorch.WakeRigidBody = utlib.WakeRigidBody
uetorch.IgnoreCollisionWithPawn = utlib.IgnoreCollisionWithPawn

return uetorch
96 changes: 95 additions & 1 deletion Source/UETorch/Private/TorchContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "UETorchPrivatePCH.h"
#include "ScriptBlueprintGeneratedClass.h"
#include "TorchContext.h"
#include <string>

const ANSICHAR *UTPackage = "uetorch";

Expand All @@ -24,7 +25,7 @@ FTorchContext* FTorchContext::Create(const FString& SourceCode, UObject* Owner)
{
if (NewContext->Initialize(SourceCode, Owner))
{
// nothing
NewContext->RegisterUETorchLibrary();
}
else
{
Expand All @@ -51,6 +52,25 @@ void FTorchContext::Tick(float DeltaTime)
}
}

luaL_Reg UETorchLib[] =
{
{"GetActorPhysicsHandleComponent", FTorchLibrary::GetActorPhysicsHandleComponent},
{"GetActorMeshComponentAsPrimitive", FTorchLibrary::GetActorMeshComponentAsPrimitive},
{"LineTraceMeshComponent", FTorchLibrary::LineTraceMeshComponent},
{ NULL, NULL }
};

void FTorchContext::RegisterUETorchLibrary(){
lua_getglobal(LuaState, "UETorch");
if (lua_isnil(LuaState, -1))
{
lua_pop(LuaState, 1);
lua_newtable(LuaState);
}
luaL_setfuncs(LuaState, UETorchLib, 0);
lua_setglobal(LuaState, "UETorch");
}

bool FTorchContext::CallFunctionString(const FString& FunctionName, FString In, FString& Out)
{
check(LuaState);
Expand Down Expand Up @@ -141,3 +161,77 @@ bool FTorchUtils::CallFunctionArray(lua_State* LuaState, const ANSICHAR* Functio
lua_pop(LuaState, 1);
return bResult;
}

int FTorchLibrary::GetActorPhysicsHandleComponent(lua_State* LuaState)
{
AActor* object = (AActor*)lua_touserdata(LuaState, 1);
if(object == NULL) {
printf("Object is null\n");
return 0;
}
UPhysicsHandleComponent* component = object->FindComponentByClass<UPhysicsHandleComponent>();
if(component == NULL) {
printf("Object doesn't have a UPhysicsHandleComponent\n");
return 0;
}
lua_pushlightuserdata(LuaState, component);
return 1;
}

int FTorchLibrary::GetActorMeshComponentAsPrimitive(lua_State* LuaState)
{
AActor* object = (AActor*)lua_touserdata(LuaState, 1);
if(object == NULL) {
printf("Object is null\n");
return 0;
}
UStaticMeshComponent* meshComponent = object->FindComponentByClass<UStaticMeshComponent>();
if(meshComponent == NULL) {
printf("Object doesn't have an UStaticMeshComponent\n");
return 0;
}
UPrimitiveComponent* PrimitiveComponent = Cast<UPrimitiveComponent>(meshComponent);
lua_pushlightuserdata(LuaState, PrimitiveComponent);
return 1;
}

int FTorchLibrary::LineTraceMeshComponent(lua_State* LuaState)
{
AActor* object = (AActor*)lua_touserdata(LuaState, 1);
if(object == NULL) {
printf("Object is null\n");
return 0;
}
UStaticMeshComponent* meshComponent = object->FindComponentByClass<UStaticMeshComponent>();
if(meshComponent == NULL) {
printf("Object doesn't have an UStaticMeshComponent\n");
return 0;
}
FCollisionQueryParams TraceParams(FName(TEXT("UETorch LineTraceMeshComponent")), true, nullptr);
float startx = lua_tonumber(LuaState, 2);
float starty = lua_tonumber(LuaState, 3);
float startz = lua_tonumber(LuaState, 4);
FVector Start(startx, starty, startz);
float endx = lua_tonumber(LuaState, 5);
float endy = lua_tonumber(LuaState, 6);
float endz = lua_tonumber(LuaState, 7);
FVector End(endx, endy, endz);
FHitResult OutHit(ForceInit);
bool isHit = meshComponent->LineTraceComponent(OutHit, Start, End, TraceParams);
if(!isHit) return 0;
lua_newtable(LuaState);
int index = lua_gettop(LuaState);
lua_pushlstring(LuaState, "x", 1);
lua_pushnumber(LuaState, OutHit.Location.X);
lua_settable(LuaState, index);
lua_pushlstring(LuaState, "y", 1);
lua_pushnumber(LuaState, OutHit.Location.Y);
lua_settable(LuaState, index);
lua_pushlstring(LuaState, "z", 1);
lua_pushnumber(LuaState, OutHit.Location.Z);
lua_settable(LuaState, index);
lua_pushlstring(LuaState, "BoneName", 8);
lua_pushlightuserdata(LuaState, &(OutHit.BoneName));
lua_settable(LuaState, index);
return 1;
}
7 changes: 7 additions & 0 deletions Source/UETorch/Private/TorchContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FTorchContext : public FLuaContext
static FTorchContext* Create(const FString& SourceCode, UObject* Owner);

void Tick(float DeltaTime);
void RegisterUETorchLibrary();
bool CallFunctionString(const FString& FunctionName, FString In, FString& Out);
bool CallFunctionArray(const FString& FunctionName, const TArray<FString>& In, FString& Out);
};
Expand All @@ -31,3 +32,9 @@ struct FTorchUtils {
static bool CallFunctionString(lua_State* LuaState, const ANSICHAR* FunctionName, const ANSICHAR* In, FString& Out);
static bool CallFunctionArray(lua_State* LuaState, const ANSICHAR* FunctionName, const TArray<FString>& In, FString& Out);
};

struct FTorchLibrary {
static int GetActorPhysicsHandleComponent(lua_State* LuaState);
static int GetActorMeshComponentAsPrimitive(lua_State* LuaState);
static int LineTraceMeshComponent(lua_State* LuaState);
};
Loading