Author Topic: [SOLVED]Finding the brick the player is looking at is not working  (Read 868 times)

I've been trying to find the brick the player is looking at when he clicks, but it's not working. I've tried code from other add-ons I thought did this as well as searched the coding help sub forum but nothing seems to work.  I'm getting the first error, and the echo is "0" :
Code: [Select]
function serverCmdLinkTardis( %c ){
messageClient(%c,0,"Click the vehicle spawn, now. Please don't use a jeep.");
%c.LT = true;
}
function Player::activateStuff(%player) {
Parent::activateStuff(%player);
%target = ContainerRayCast(%player.getEyePoint(), vectorAdd(%player.getEyePoint(), vectorScale(%player.getEyePoint(), 10)), $TypeMasks::FxBrickObjectType | $TypeMasks::FxBrickAlwaysObjectType);
if(!%player.client.LT)
return;
%player.client.LT = false;
if(!isObject(%target)){
echo(%target);
messageClient(%player.client,0,"Error: (1) : Couldn't find TARDIS.",0);
return; }
if(! %target.vehicle.getDatablock().rideable) {
messageClient(%player.client,0,"Error: (2) : Couldn't find TARDIS.",0);
return; }
//do they even own this brick?
if(!%target.client == %player.client) { //if the guy who clicked it isn't the same guy who owns the brick
messageClient(%player.client,0,"Quit trying to screw with other people's stuff.",0);
return;
}
%target.insidePos = %c.TARDISPOS;
messageClient(%player.client,0,"TARDIS registered sucsessfully. You can now click to enter it. Better not be a freaking jeep.",0);

}
my code probably looks a little different than normal torquescript -- the only other language I know is Lua and I kinda still have that mindset
I'm kinda still at this stage in torquescript:
« Last Edit: November 08, 2014, 04:02:53 PM by superdupercoolguy »

Well your raycast looks like it has three arguments, the norm has four.
Code: [Select]
%ray = ContainerRayCast(%start, %end, %targets, %obj);
%col = getWord(%ray, 0);
That's how I'd do a raycast, with defining arguments like:
Code: [Select]
%aimVec %obj.getEyeVector();
%start = %obj.getEyePoint();
%end = vectorAdd(%start, vectorScale(%aimVec, %range));
%targets = %this.raycastWeaponTargets;
This is from a weapon that uses raycasts instead of projectiles, so it uses %obj for the player and %this to define some stuffs.
%targets is obviously the typemasks

Your issue is that you're using %player.getEyePoint() instead of %player.getEyeVector() to get the direction they're looking at.

You have:
%target = ContainerRayCast(%player.getEyePoint(), vectorAdd(%player.getEyePoint(), vectorScale(%player.getEyePoint(), 10)), $TypeMasks::FxBrickObjectType | $TypeMasks::FxBrickAlwaysObjectType);

It should be:
%target = ContainerRayCast(%player.getEyePoint(), vectorAdd(%player.getEyePoint(), vectorScale(%player.getEyeVector(), 10)), $TypeMasks::FxBrickAlwaysObjectType);

The other change I made is removing $TypeMasks::FxBrickObjectType because, as the name implies, $TypeMasks::FxBrickAlwaysObjectType covers bricks in all scenarios.



Other issues I found:

if(!%target.client == %player.client) { //if the guy who clicked it isn't the same guy who owns the brick

The ! operator is a boolean "not" operator. This means it converts true values to false values, and false values to true values. Assuming %target.client exists, the ! operator will change its interpretation to false. If %target.client was "" or 0, it would change its interpretation to true. So basically this can have a value of either 0 or 1, and you're comparing it to a variable that will always have a value that is neither 0 nor 1.

What you meant to do was use the not equals operator, !=. In Lua, it's ~=. The line should look like this:

if(%target.client != %player.client) { //if the guy who clicked it isn't the same guy who owns the brick

You're also using a really weird indentation scheme for brackets. Usually closing brackets are never left on the same line as whatever came before it. This isn't an actual issue, it's just a nit-picky kind of thing.
« Last Edit: November 08, 2014, 12:50:06 AM by $trinick »

Thanks to both of you, that really helps. As for the bracketing scheme, I still have the lua mindset where something would be like like "if t==true then dostuff() end" so I naturally place the "then" or { on the same line and the end or } on a different line. I'll probably quit doing it eventually, when I feel more comfortable with Torquescript
Thanks for pointing out the incorrect usable of the ! operator. I didn't know about that, I've been learning from reading addons