Game Development Community

%collision $= ""

by rennie moffat · in General Discussion · 08/07/2009 (9:07 am) · 17 replies

%collision $= ""

Specifically I am wondering what the significance is of the ""?




They hold info, a blank later to be determined variable like CLAMP?

About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
08/07/2009 (9:42 am)
And this comes from what file?

% = local
$ = global

You can change local variables on the fly. If you can change global variables on the fly, I haven't managed it.

An empty variable usually means a clean space waiting for something to populate it.

If I remember correctly, I don't have script head on right now.
#2
08/07/2009 (10:23 am)
(yes, % = local and $ = global when used as a prefix for variable names. Note however that that is not what $ means when used as $= in the statement above)

%collision          $=               ""
(local var)  (string equality)  (empty string)

It says: is the value of %collision (as a string) equal to an empty string.

This differs from saying
%collision == ""

Because a regular equality operator (==) would view each side of the statement as boolean. So:

%collision = "0";
%collision == ""; // This is true, since both 0 and (emtpy string) are boolean FALSE
%collision $= ""; // This is false, since "0" is not the same sting as ""

Follow?
#3
08/07/2009 (10:27 am)
%collision          $=               ""  
(local var)  (string equality)  (empty string)

so an empty string could be many things, for instance x, y?
#4
08/07/2009 (10:30 am)
%collision == ""; // This is true, since both 0 and (emtpy string) are boolean FALSE


so this would be used (boolean) in any type of calculation the engine must perform? for instance, calculating velocity and actual movement of an object? Core coding?
#5
08/07/2009 (10:36 am)
Quote:You can change local variables on the fly. If you can change global variables on the fly, I haven't managed it.

o_O Are you sure? In my experience you can change global vars at any time. Though there are some global vars you won't be able to change only because they are being continually reset by some other function. AFAIK Torque has no mechanism for creating read-only variables. Read-only Object fields, yes, but not variables.
#6
08/07/2009 (10:48 am)
@ rennie:

An empty string is just that. Empty. It's defined as data of type String with zero length.

The statements as they are used above are actually completely useless. Those are boolean operators which will return a true or a false when comparing the two sides of the statement. The code above will compile and run, but it will do absolutely nothing.
%collision $= ""
may return TRUE or FALSE depending on the value of %collision, however since nothing is done with that returned value, there is no point in evaluating that statement.

In a real code situation, you are likely to see statements like those inside conditional blocks like "if" or "while". Example:
%collision = %item.radiusCheck();
if (%collision !$= "")
{
   %collision.pickUp(%item);
}

or

function Item::isColliding(%this)
{
   return %this.radiusCheck() !$= "";
}

(note the !$= means "not equal as a string")
#7
08/07/2009 (10:54 am)
what is not equal to a string? All I see is a $ (gloabl nothing) is not (!) equal to "" Nothing.


The fact it is equal to nothing I understand, the string is empty, but what is the global (if there is with $) factor in this case?
#8
08/07/2009 (11:13 am)
$= is the string comparison operator
== is the object identity equality operator

One is an operator that compares the values of strings, the other compares object identity.

$= tests two strings to see if they have the same values
!$= is the negated form of $=

Essentially, you just need to know that when comparing the values of strings, you should use $= and !$= instead of == and !=.

Matt
#9
08/07/2009 (11:19 am)
Ok, the $ has nothing to do with global or local in this case. Yes the $ when used as a prefix for a variable name defines that variable as global. However, when the $ is used with a = as in $= then it has a different meaning altogether. The two characters "$=" together form an equality operator. Like "<=" (less than or equal), it becomes a single operator. "$=" means "compare the two values as strings".

See, TorqueScript is a typeless language. Unlike C++ which requires you to specify what type each variable is (eg: "int foo" or "bool foo"), TorqueScript will accept any type for any variable, and it will attempt to convert between types as necessary. This can be very useful, but it can also cause trouble.

Here's a real example from a function I wrote:

function AntigravShip::power(%this, %on)
{
   if (%on $= "") %on = %this.isMoveDisabled();
	
   if (%on && %this.isMoveDisabled())
   {
      %this.disableMove(false);
      %this.client.Play2D(powerUpSound);
   }
   else if (!%on && !%this.isMoveDisabled())
   {
      %this.disableMove(true);
      %this.client.Play2D(powerDownSound);
   }
}

I wanted to be able to say
%ship.power(1); // Power On
%ship.power(0); // Power Off
%ship.power();  // Toggle on/off

When I pass 1 to the function power(), %on is true. When I pass 0, %on is false. When I pass nothing, %on is also false, but if I compare it as a string I find it is empty. And so I can distinguish between having passed 0 and having passed nothing as an argument to power().

If I had written this instead:

function AntigravShip::power(%this, %on)
{
   if (%on == "") %on = %this.isMoveDisabled(); // Doesn't work!
	
   if (%on && %this.isMoveDisabled())
   {
      %this.disableMove(false);
      %this.client.Play2D(powerUpSound);
   }
   else if (!%on && !%this.isMoveDisabled())
   {
      %this.disableMove(true);
      %this.client.Play2D(powerDownSound);
   }
}

Then the function would not work properly, because both a number 0 and an empty string ("") are both considered FALSE. I needed to check for an empty string, but still be able to pass a 0. And so "$=" will compare the string %on with the string "". Since the string "0" contains one character, a zero, it is not equal to an empty string "" containing no characters.

Does that help?
#10
08/07/2009 (11:34 am)
this is great. I imagine I am causing some frustration over just a relatively mute point for me know. I will be going over this and using it as a reference as I develop. Thanks!
#11
08/07/2009 (12:14 pm)
No worries. I doubt anyone here is frustrated. There are a lot of symbols and concepts in TorqueScript and programming in general that can be very confusing at first.

In regards to the topic: I'd like to point out also that TorqueScript has no concept of "undefined" or "undeclared" variables. When you use a variable for the first time it is automatically created and initialized to an empty string. (Which is why I compare %on to an empty string in the function above)

This behavior can be very useful. As in this example:
function registerDamage(%victim, %attacker, %damage)
{
   if (isObject(%victim))
   {
      %victim.damageFrom[%attacker.player] += %damage;
   }
}
Where I want each player to track how much damage they have received from each other player. I don't have to declare a damageFrom[playerX] for every other player. Each damageFrom[playerX] will be created as I need it.

Unfortunately, this behavior can also cause trouble, because TorqueScript has no way to warn you when you misspell a variable name. For example, let's say I misspell %delta in this function:
function GameConnection::incScore(%this, %delta)
{
   %this.score += %detla; // Notice the misspelling here? Torque won't.

   // inform all clients
   commandAll('updateScore', %this, %this.score);
}
That will compile and run fine. It will show no errors, yet it won't work, and you'll be left banging your head against the wall wondering why.

Just something to watch out for.
#12
08/07/2009 (12:39 pm)
With out going thru all of your reply, here is a question.


registerDamage


these are words that are in the base core of the TorqueScript language? or are they somewhat abstract?
#13
08/07/2009 (12:40 pm)
and why in this line do you decide to use [] instead of ()?


%victim.damageFrom[%attacker.player] += %damage
#14
08/07/2009 (12:43 pm)
RE: bum script, ex. spelling error and file still runs, just behavior/function not perform, thats kinda shitty. but... my main problem as of now is games/files that won't even run.



:?Bummer I guess, I have anther thread about that here.

[html]
http://www.garagegames.com/community/forums/viewthread/99029
[/html]
ps. what is the code for providing a link?
#15
08/07/2009 (5:18 pm)
Quote:and why in this line do you decide to use [] instead of ()?

Because brackets look so much cooler than parentheses. :}

...

Seriously though, brackets and parentheses have completely different uses and they are not interchangeable. Parentheses () delimit arguments to functions and conditions for flow control statements (ex: if, while, switch). They also group operations to determine the order of processing. Whereas brackets [] usually denote an array index (although TorqueScript doesn't actually support arrays "as such"). More information about these and other elements of TorqueScript syntax can be found here.

As for registerDamage, in the code above registerDamage is being defined as a function. Functions allow grouping of multiple lines of code for later use. More on that subject here.

And the code for links is [ url ]. See this page on MarkupLite for more information.
#16
08/07/2009 (9:11 pm)
SUHWEEEEEEEEEET>
IT just keeps getiing better.


My favourite it video ever. Its awsome.. ha ha hah ha..
nbacnca
out/


#17
08/07/2009 (9:17 pm)
ps.
my spelling is terrible, bought my grammer is fine. as in check out this gramatic video.


IS it cool to post vids in here as celebration?





http://www.dailymotion.com/video/x15g3u_chaka-khan-rufus-sweet-thing_music



if not please enjoy on your own time.


Chaka Kahn Sweet Thing. Nice Track.