Time for action - changing the teleporter entity to execute the C++ code

  1. First, we need to change the lua script. We want to change the function Teleporter:OnUsed to call our C++ function instead of doing the calculation in Lua.
  2. This is pretty easy. Change the function as shown below:
    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
  3. Save your changes of Teleporter.lua.
  4. With the use of user.actor:<function>, we can call all the Scriptbind functions that are implemented in ScriptBind_Actor.h. In this case, we simply call our newly created function.
  5. Now it is time to see if everything works as expected. Switch back to Visual Studio and add a breakpoint at the first line of your function.
  6. Press F5 to run Sandbox. Once Sandbox is started, load a level, place your Teleporter entity, jump ingame, and use it (Press F key). As soon you press F, Visual Studio should pop up and your breakpoint is triggered. If you mouse over the targetPos argument, you should see the current values.
  7. Press F5 again to see if the teleportation worked.
    Time for action - changing the teleporter entity to execute the C++ code

What just happened?

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?

  • Creating complex functionality in C++ can be much cleaner and robust than Lua
  • Lua can be slow: Run heavy code in C++ (math, physics, and so on)
  • It's easier to debug C++ code with Visual Studio
  • Not all engine functionality is exposed through Lua, so there are some things you can truly only do through C++

Have a go hero - doing the complete teleporter logic in C++

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

Tip

To get the current position of an entity in C++ you can call:

Vec3 pos = pActor->GetEntity()->GetWorldPos();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.245.140