Author Topic: Spawn a New Brick Correctly  (Read 1261 times)

I want to know how to spawn a new brick that can be hammered and has a designated client, etc. Also I'd like to know how to get the client from a brick.

Code: [Select]
function createBrick(%cl, %data, %pos, %color, %angleID)
{
if(!isObject(%data) || %data.getClassName() !$= "fxDTSBrickData")
return -1;
if(getWordCount(%pos) != 3)
return -1;
if(%angleID $= "")
%angleID = 0;
if(isObject(%cl) && (%cl.getClassName() $= "GameConnection" | %cl.getClassName() $= "AIConnection"))
{
%blid = %cl.bl_id;
if(%blid $= "")
%blid = -1;
%flag = 1;
}
else if(isObject(%cl) && %cl.getClassName() $= "SimGroup" && MainBrickgroup.isMember(%cl))
{
%group = %cl;
%cl = %group.client;
%blid = %group.bl_id;
}
else %cl = 0;
switch(%angleID)
{
case 0:
%rot = "1 0 0 0";
case 1:
%rot = "0 0 1 90";
case 2:
%rot = "0 0 1 180";
case 3:
%rot = "0 0 -1 90";
}
(%brick = new fxDtsBrick()
{
client = %cl;
colorFxID = 0;
colorID = %color;
datablock = %data;
isPlanted = 1;
position = getWord(%pos, 0) SPC getWord(%pos, 1) SPC getWord(%pos, 2);
rotation = %rot;
shapeFxID = 0;
stackBL_ID = %blid;
}).angleID = %angleID;
%err = %brick.plant();
%brick.setTrusted(1);
missionCleanup.add(%brick);
if(%flag) %cl.brickgroup.add(%brick);
else if(isObject(%group)) %group.add(%brick);
return %brick TAB %err;
}

People keep complaining that it's wrong because I'm setting angleID outside the brick, except it never works properly if I set it inside.
If the 'correct' way doesn't work, it's by definition not correct.


Plus most functions people seem to use don't let you use a brickgroup as opposed to a client, or return both the brick and the error code.

Code: [Select]
if(isObject(%cl) && (%cl.getClassName() $= "GameConnection" | %cl.

Can't quote properly because I'm on my iPad, but that last | should be ||

Can't quote properly because I'm on my iPad, but that last | should be ||
no it doesn't
both || and | work in torque
although by convention it really should be 2

Both || and | work in every scripting language with either one of them.


uh not /every/ scripting language

Name one programming language which uses one as a logical operator and doesn't include the other.

Compared || to |
In cases where only the right side was true, both ran just as fast.
In cases where both sides/left side were true, || ran faster.

However, when using ||, if the left side is true, the right side never gets called, so any variable assignments or function calls will never happen, as apposed to using |.

| also adds bits.

| is just bitwise OR, isn't it?

| is just bitwise OR, isn't it?
Yes, but technically, true/false can be interpreted as bits since they are 1/0.

Name one programming language which uses one as a logical operator and doesn't include the other.
Haskell and Seed7
:3

Just because | can be used doesn't mean it should be used. | and || abstract to completely different commands. || short circuits if the first one is true and doesn't even check the second one whereas | doesn't and needlessly checks both even when the first one is true. Non-short-circuiting operators also may not work with anything that is a non-boolean value, for example if(%client.player && %client.camera) will always work, but if(%client.player & %client.camera) is not guaranteed to work.

-snip-

It's also worth noting that normally || and && evaluate true and false exclusively. Since TGE is stupid and weird, they can be used on anything.  This results in really weird behavior - as best I can tell, the actual logic behind Torque's || is something like this:

Code: [Select]
function shortCircuitingOr(%a, %b)
{ return %a ? %a : %b; }

Try echo(5 || 2);.


Similarly, && seems to be something like this:

Code: [Select]
function shortCircuitingAnd(%a, %b)
{ if(%a) return %b; }