function Teleporter:OnUsed(user) --check „user" being valid if (not user) then return 0; end --compute target position from current position + teleport direction local vCurPos = {}; user:GetWorldPos(vCurPos); local vTargetDir = {}; --assign a temp vector as targetDir „type" vTargetDir.x = self.Properties.teleportDirX; vTargetDir.y = self.Properties.teleportDirY; vTargetDir.z = self.Properties.teleportDirZ; local vTargetPos = vecAdd(vCurPos, vTargetDir); --call the scriptbind function user.actor:TeleportTo(vTargetPos); end
Scriptbind
functions that are implemented in ScriptBind_Actor.h. In this case, we simply call our newly created function.Congratulations! You created a new, interactive entity that calls the C++ code. Good job!
By calling user.actor:TeleportTo(vTargetPos);
, we executed the C++ Scriptbind
function. The user object is a reference to the actor that uses the Teleporter entity. It was passed to the Teleporter:OnUsed(user)
function. By using the user.actor:<fct name>
function, we can access all the Scriptbind
functions that are defined in the ScriptBind_Actor.h file.
So what is this good for? Why don't we use Lua for everything?
We still did a lot of logic in the Lua script itself. Try to change the C++ function to do the complete Teleporter logic. The Lua script should only call:
function Teleporter:OnUsed(user) local vTargetDir = {}; --assign a temp vector as targetDir „type" vTargetDir.x = self.Properties.teleportDirX; vTargetDir.y = self.Properties.teleportDirY; vTargetDir.z = self.Properties.teleportDirZ; --call the scriptbind function user.actor:TeleportTo(vTargetDir); end
18.220.245.140