How to get HitBlockCoords from FirstSolidHitTrace
#1
Salut,

I would like to use cLineBlockTracer.FirstSolidHitTrace to know what block the player is looking at. But the problem is that it returns only a boolean, which is true if there is a solid block, and false if not.




But in LineBlockTracer.h, it says :
/** Traces until the first solid block is hit (or until end, whichever comes first.
If a solid block was hit, returns true and fills a_HitCoords, a_HitBlockCoords and a_HitBlockFace.
If a_End is encountered without hitting any solid block, returns false and doesn't touch a_HitCoords, a_HitBlockCoords nor a_HitBlockFace.
a_HitCoords is the exact coords of the hit,
a_HitBlockCoords are the coords of the solid block that was hit,
a_HitBlockFace is the face of the solid block that was hit. */
static bool FirstSolidHitTrace(
  cWorld & a_World,
  const Vector3d & a_Start, const Vector3d & a_End,
  Vector3d & a_HitCoords,
  Vector3i & a_HitBlockCoords,
  eBlockFace & a_HitBlockFace
);

So I tried :
cLineBlockTracer.FirstSolidHitTrace(World, Start, End, a_HitCoords, a_HitBlockCoords, a_HitBlockFace);

which gives in the console :
Code:
error in function ’FirstSolidHitTrace’: Too many arguments.
argument #4 is ’nil’; ’[no object]’ expected.

I also tried other ways to get it, like just to refer to a_HitBlocksCoords further in the code, but I got nothing.

Could someone explain me how I can get these parameters, please ? Thanks ^^

Here is the full function :
function WhatISee(a_split, a_Player)
local World = a_Player:GetWorld();

local EyePos = a_Player:GetEyePosition();
local LookVector = a_Player:GetLookVector();
LookVector:Normalize();  -- Make the vector 1 m long

local Start = EyePos;
local End = EyePos + LookVector * 50;

cLineBlockTracer.FirstSolidHitTrace(World, Start, End, a_HitCoords, a_HitBlockCoords, a_HitBlockFace);

return true;
end


Beuj

(edited by xoft to use shcode tags)
Reply
Thanks given by:
#2
Hello, welcome to the forum.

You shouldn't look at the C++ code for Lua code parameters, they sometimes do differ, especially when the C++ code uses references as out-params - Lua can return multiple return values, so the API is reflecting that. Have a look at the API docs: https://api.cuberite.org/cLineBlockTracer.html

The function FirstSolidHitTrace() takes only 3 parameters, and returns 4 values, so the proper way to use it is:
local hasHit, hitCoords, hitBlockCoords, hitBlockFace = cLineBlockTracer:FirstSolidHitTrace(World, Start, End)
-- hasHit is a boolean
-- hitCoords is a Vector3d
-- hitBlockCoords is a Vector3i
-- hitBlockFace is an eBlockFace
LOGD("Looking at block {" .. hitBlockCoord.x .. ", " .. hitBlockCoord.y .. ", " .. hitBlockCoord.z .. "}")
Reply
Thanks given by:




Users browsing this thread: 2 Guest(s)