Author Topic: Right-Click-To-Attack Help  (Read 1543 times)

Here's the code I tried using:
Code: [Select]
package Escapists_Attacking
{
function Armor::onTrigger(%data, %obj, %slot, %io)
{
if(%slot == 4)
{
if(%io)
{
%obj.playThread(2, shiftUp);
%start = %obj.getEyePoint();
%vec = vectorScale(%obj.getMuzzleVector(%slot), 5  * getWord(%obj.getScale(), 2) );
%end = vectorAdd(%start, %vec);
%mask = $TypeMasks::PlayerObjectType;

%rayCast = containerRayCast(%start,%end,%mask);

if(!%rayCast)
return;

%hitObj = getWord(%rayCast, 0);

%hitObj.client.player.setDamageFlash(0.1 * %obj.client.Escapists["Damage"]);
%hitObj.client.Escapists["HP"] -= (%obj.client.Escapists["damage"] + %obj.client.Escapists["EquippedWeaponDmg"]);
}
}
parent::onTrigger(%data, %obj, %slot, %io);
}
};

What happens when you click it, is that it makes your screen flash, and it doesn't even change hitClient's HP.

You're firing a ray looking for players. The first it finds will always be the player firing it, because it's right there.

To fix it, add a fourth argument to the containerraycast call that is the player firing it:
%rayCast = containerRayCast(%start,%end,%mask,%obj);
This tells the function to ignore that object

Also, %hitObj will always be a player, since that's the only object type you're scanning for, so adding .client.player isn't adding anything
« Last Edit: July 24, 2015, 06:00:24 PM by Headcrab Zombie »

Also, %hitObj will always be a player, since that's the only object type you're scanning for, so adding .client.player isn't adding anything
It's a good idea to note that %hitObj might also be an AIPlayer vs a Regular Player, you might want to check if there is a client before carrying on.

It's a good idea to note that %hitObj might also be an AIPlayer vs a Regular Player, you might want to check if there is a client before carrying on.
Or just use getClassName() on it.

It shouldn't hit the player that's firing it. Raycasts ignore objects that they originate inside. But if Torque's being weird and that fixes it for you, then go for it. It doesn't hurt to add it as an exemption.

Or just use getClassName() on it.

SimObject::getClassName() is an extra function call and a string comparison. if(%object.client) does an integer comparison (much faster) and avoids a function call. You don't need to call isObject because there should never be a situation where %object.client is set but the client doesn't exist as players are removed from the game when their client disconnects.
« Last Edit: July 24, 2015, 06:23:33 PM by $trinick »

SimObject::getClassName() is an extra function call and a string comparison. if(%object.client) does an integer comparison (much faster) and avoids a function call. You don't need to call isObject because there should never be a situation where %object.client is set but the client doesn't exist as players are removed from the game when their client disconnects.
Picky, picky.

I don't think that it really matters to be that efficient.

It shouldn't hit the player that's firing it. Raycasts ignore objects that they originate inside.
Check the documentation in the stickies





I don't think that it really matters to be that efficient.
Such small efficiency changes wouldn't be worth pursuing if it meant redoing a ton of code. But simply using a slightly different check whenever you write something isn't hard

exempt can be more than one thing, up to six objects.

(Point3F start, Point3F end, bitset mask, SceneObject exempt, exempt2, exempt3, exempt4, exempt5, exempt6)

Check the documentation in the stickies

That documentation is out of date for the version of TGE we're running. It doesn't even mention initContainerBoxSearch. Also what Valcle said. I'm too lazy to log into my GarageGames account and download the source to copy it here, but you can easily test this in game:

echo(isObject(firstWord(containerRayCast(%start=localClientConnection.player.getPosition(), vectorAdd(%start, "0 0 10"), $TypeMasks::PlayerObjectType))));



EDIT: and then a confirmation with the start and end points swapped:

echo(isObject(firstWord(containerRayCast(vectorAdd(%end=localClientConnection.player.getPosition(), "0 0 10"), %end, $TypeMasks::PlayerObjectType))));

« Last Edit: July 24, 2015, 07:39:13 PM by $trinick »

Picky, picky.

I don't think that it really matters to be that efficient.
It does matter, especially when you're teaching someone. Teach them to do it right. It's not even that difficult and the more they learn, the better.

Besides, if everyone was thinking "Oh, it's just one thing, it's ok not to be totally efficient", you would eventually get to the point where inefficiencies are piling up, things start to have impact in numbers.

You will never get to that point making an add-on unless you're making huge inefficiencies.

You're firing a ray looking for players. The first it finds will always be the player firing it, because it's right there.
Well, this explains how I knocked myself out.