Game Development Community

Coding Question

by Turbosheep · in General Discussion · 01/23/2010 (12:43 pm) · 5 replies

Hi,

After my forced abandonment of GML, I'm now getting myself aquainted with Torquescript. I'm used to the use of objects in GML (gamemaker language), and it appears that classes fulfill this role in TS. Now I'm following a tutorial about how to make a 'fish game', and the code seems comprehensible except for the many names for one object.

class of the player (fish) has class name: PlayerFish

In assigning controls I'm asked to put in this:

function PlayerFish::onLevelLoaded(%this, %scenegraph)
{

$FishPlayer=%this;

moveMap.bindCmd(keyboard, "w", "fishPlayerUp();", "fishPlayerUpStop();");
moveMap.bindCmd(keyboard, "s", "fishPlayerDown();", "fishPlayerDownStop();");
moveMap.bindCmd(keyboard, "a", "fishPlayerLeft();", "fishPlayerLeftStop();");
moveMap.bindCmd(keyboard, "d", "fishPlayerRight();", "fishPlayerRightStop();");
moveMap.bindCmd(keyboard, "space", "fishPlayerBoost();", "fishPlayerBoostStop();");

}

As I see it, PlayerFish = %this = $FishPlayer .

Why do I need three names for one object? Why not just keep using PlayerFish, like defined in the class?
Why define %this and $FishPlayer??

Many thanks for explanation. I'm still a beginner, sorry if this is appears obvious.


#1
01/23/2010 (1:29 pm)
I do believe that for classes, when a "%this" argument is given in a method of the class, the value of "%this" is an instance of an object in the class, and not of the class itself. So in the function call, $FishPlayer is stored with the value of the instance of an object of class PlayerFish so that the player instance would have global scope.
#2
01/23/2010 (2:06 pm)
So, if I understand you correctly, %this is bound to a variable like '$FishPlayer' in order to make the Class global? If that's the case, I'd like to know why we couldn't just use the class itself, for instance:

function PlayerFish::onLevelLoaded(PlayerFish, %scenegraph)
{
PlayerFish.setLinearVelocityX(PlayerFish.hSpeed);
}

I hope I'm properly clarifying my way of thinking; I'm pretty new to all this serious code stuff, GML seemed much easier to me.
#3
01/23/2010 (2:17 pm)
Not quite... Let us say that the playerfish in this function was "Lenny" (completely figurative). So whenever Lenny, a member of PlayerFish, called the method onLevelLoaded, the %this would not hold the class of PlayerFish, but would hold a reference to Lenny, the member of class PlayerFish that is calling the method. I think that is right. Having trouble finding a good example...

btw, what is %scenegraph? I don't use TGB.
#4
01/23/2010 (2:43 pm)
Thanks for all your help.

I need to get used to the idea that a class refers to a sort of 'race', rather than one individual.

If a class can refer to more then one object, why do I need to put the name of the class in the object I'm coding for (I mean adding a classing name in level builder when clicking on the object, the fish)? It feels like adding a name, and then adding a second name, namely 'Lenny'.

Or should I see it more like an opportunity to create a lot of global variables so pieces of code do not interfere with eachother?

I guess I still don't understand why can't use PlayerFish in functions... especially since I'm stating that PlayerFish=%this. If I can't use the class name, why not just use %this all the time instead of creating a new name again (first class, then %this, and then 'Lenny')?

This is what I found out about %scenegraph for you:

'The %scenegraph value is useful as well, since it represents our level object. Everything in our level is inside of the scenegraph object.'

I'd say %scenegraph = level.
#5
01/23/2010 (4:52 pm)
A good reason for a classname is for when you will have a bunch of objects that do all the same thing or have something done with them the same. For example, a common reason for className would be damage calls. If you have a group of objects that all get damaged the same way, you would group them in a class that would handle all of their damage calls the exact same way. So classes help with consistency.