Game Development Community

Better Understanding %WorldLimit and %onUpdate Together

by rennie moffat · in Torque Game Builder · 10/22/2009 (12:42 pm) · 107 replies

Hi there, if some could, please tell me where I am going wrong with this, or if I am not, I sure I am though. Where I am having most trouble is where I link things, so to speak, I am not sure about my on update and even if it should exist and if it should what command should really go in there.


function Enemy::onLevelLoaded(%this)
{
  %this.owner.enableUpdateCallback();
   
  %left = getWord(%worldLimit, 1);
  %top = getWord(%worldLimit, 2);
  %right = getWord(%worldLimit, 3);
  %bottom = getWord(%worldLimit, 4);

  %this.setWorldLimit("NULL", %left, %top, %right, %bottom, true);
  
 }
 
 
function enemy::onUpdate(%this)
{
	%this.getPosition();	
}
 
 
function enemy::onWorldLimit(%this, %limitMode, %limit)
{
	
	
	%this.owner.getWorldLimit();
	
	switch$(%limit)
	{
		case "left":
		if (%this.left)
		{
			%this.owner.setLinearVelocityY(5);
		}
	        case "right":
		if (%this.right)
		{
			%this.owner.setLinearVelocityY(-5);
		}
	
	        case "top":
		if (%this.top)
		{
			%this.owner.setLinearVelocityX(5);
		}
	
	        case "bottom":
		if (%this.bottom)
		{
			%this.owner.setLinearVelocityX(-5);
		}
	

	}


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.

#41
10/24/2009 (2:55 pm)
I have no idea what you're saying, so I can't tell you if we're in the same ballpark or not. Neither of the above looks like a method to me - there's no class name. If you want to write a doSomething() method for the Spaceship class, you'd declare (not "use") it like this:

function Spaceship::doSomething(%this) {
    // Note the parameter! When this method is called,
    // %this is an object of class Spaceship.
}

Now, if you have an object of class Spaceship, you can call (not "use") its doSomething() method. Whether you call it as a method or as a function depends on how it was declared, NOT on where you're calling it from. So, assuming your Spaceship object is %ship, you'd call its doSomething() method like this:

%ship.doSomething()

Again - this is important! Whether a function is a method or not depends solely on whether it's declared as part of a class or not. Where it is called from makes no difference whatsoever.

Edit: I realize that I'm stressing the terminology, but that's because it's important! You need to learn the proper terms - if you make up new names for everything, other people won't understand what you're talking about.
#42
10/24/2009 (6:45 pm)
Hi Sherman, My internet keeps cutting out today, we've had a few storms today, nad mine usually gets messed up with the weather. Anyhow, I appreciate this, I did reply earlier but lost my post when my internet decide to cut so I apologise for my late response.


So this is good. I do have a question for you though regarding what you wrote.


function Spaceship::doSomething(%this) {  
    // Note the parameter! When this method is called,  
    // %this is an object of class Spaceship.  
////Me: when you say %this is an OBJECT of the class Spaceship I wonder what an object can be.
////(cont out of code..


}


Can an object of a class be...
%this.linearVelocity();


?





#43
10/24/2009 (7:07 pm)
The member function of a class could be "linearVelocity". As in:
%obj = new ScriptObject(Bob) { class = Spaceship; };

function Spaceship::linearVelocity( %this )
{
  // Do something here
}

%obj.linearVelocity();
Bob.linearVelocity();
Spaceship::linearVelocity( %obj );
#44
10/24/2009 (7:33 pm)
I am confused by what you wrote William.

%obj = new ScriptObject(Bob) { class = Spaceship; }  
 ///here  %obj is "defined" as a new ScriptObject(Bob) belonging to the Spaceship class.

function Spaceship::linearVelocity( %this )  
{  
  // Do something here  
}  
  
%obj.linearVelocity();  
Bob.linearVelocity();  
Spaceship::linearVelocity( %obj );  
///I am confused as to why you have set these down here, seemingly outside of a function
///also to not have a % or $ in front of Bob and Spaceship is surprising
#45
10/24/2009 (7:51 pm)
Hi guys, perhaps to help you help me, and if this is wrong because it is so miniscule I don't blame you however it may give you an idea in where I lack(or at least one facet of 7 million).


Just in messing around I made a player move back and forth between his world limits on the xAxis(-50 and 50), as he moves across the center line I set up a trigger so that whenever he passes it he triggers the trigger to spawn a new sceneObject, which will simply float up (or have yVelocity set to (-10)). However I am getting an error in my code AND I am unclear on how to call a new sceneObject. I realize this is tangent but a quick run thru could help.


Anyhow, this is much appreciated so thank you.



function centerTrigger::onLevelLoaded(%this, %scenegraph)
{
	%this.setEnterCallback(true);
}

function centerTrigger::onEnter(%this, %object)
{
	%this.spawn();
	%object = $Spacer3000;
}

function centerTrigger::spawn(%this)
{ 
         %this = %redRiser
	 %redRiser = new t2dStaticSprite(redRiser)
	 //the error occurs here.
        //also this relates to what %this is and defining something
       // I am unsure if %redRiser is being defined properly

         %redRiser.setPosition(0,0);
	 %redRiser.setLinearVelocityY(20); 
        //Also I am quite sure this might be sketchy
}




I am and will be going over all this code, but it is time for a break. I am learning and again thank you guys, you all deserve an e high 5.
#46
10/24/2009 (11:57 pm)
Sorry have just found addToScene. My programming continues. Thanks.
#47
10/25/2009 (12:23 am)
currently I have a static sprite classed as Spacer3000. It simply moves right, hits its world limit(50) and switches direction to left moves along till it hits its left limit (-50). When it passes the mid point tho I want to create a new static sprite which will simply move up at linearVelocityY(20).

I have placed a trigger in the middle of the screen (0,0) and made the following code to allow the trigger to spawn the new sprite. I am sure there may be many approaches to this problem but from what I Have written would someone please tell what it is that I am missing or not getting please.


function centerTrigger::onLevelLoaded(%this, %scenegraph)
{
	%this.setEnterCallback(true);
}

function centerTrigger::onEnter(%this, %object)
{
	%this.addToScene(%this.redRiser);
	%object = $Spacer3000;
}

function redRiser::onAdd()
{
	%this.setPosition(0,0);
	%this.setLinearVelocityY(40);	
}






@Sherman. I am still trying to understand how %this is an "object" of it's class
function Spacer3000::fly(%this)
{
%this.doThis();
//I am trying to grasp how %this is an "object"


#48
10/25/2009 (4:29 am)
These lines
%obj.linearVelocity();   
Bob.linearVelocity();   
Spaceship::linearVelocity( %obj );
were simply demonstrating three different ways that a member function could be called using TorqueScript.

But, on a related note, all code doesn't have to go into functions. If you want code to be executed when the ".cs" file is exec'd, you must put them outside of functions.

Moving on...

In your onEnter trigger code, I don't see where "%this.redRiser" is defined. Also the line "%this.addToScene(%this.redRiser);" implies that the trigger isn't already in your scene and that the "redRiser" is the scene. I highly doubt that this is what you meant. Make sure you define redRiser somewhere before you use it. (The compiler cannot determine your intent. You must spell it out completely.)

Also, what is the point of the line "%object = $Spacer3000;"? "%object" is a local variable and will cease to exist the moment that the function ends. (BTW, you once told me that you knew what a local variable was. I might be inclined to believe that you were lying to me.)

Finally, in your final portion directed to Sherman, you have a question about the "%this" object. If you have an object of class "Spacer3000" and call the function "fly" on it, then it will call the function shown. "%this" is the object and it's class is "Spacer3000".

Technically, "%this" is a numeric handle to the object and is resolved by TorqueScript into a previously created object when you call functions or access member variables.
#49
10/25/2009 (4:50 am)
I'd like to make one more comment.

Quote:
///I am confused as to why you have set these down here, seemingly outside of a function
///also to not have a % or $ in front of Bob and Spaceship is surprising

It's hard to respond to this except to say "Did you even try this code?"

If you had, you could have played with it to determine what does and doesn't work. You then might have stumbled upon namespaces and classes in the documentation.

I could responded with "Do you know what an example is?", but the answer to that is clear.
#50
10/25/2009 (12:43 pm)
Thanks William I will go over your post. Thanks. I have taken some of it and applied it to my code. But I do have some questions.


In your onEnter trigger code, I don't see where "%this.redRiser"
I have changed it to
%this.redRiser = $redRiser;
//tho to be honest I am still hesitant about this
and have removed onAddToScene, I agreed on this. So my question is where do I need to define redRiser and how. Currently as you can see it is in my onEnter function and defined as such.


%object, in onEnter is the object entering the trigger, thus $Spacer3000, am I wrong? And why would I lie, tho I do have some uncertanties. A local variable exits with in a function or a class?



Quote:
in your final portion directed to Sherman, you have a question about the "%this" object. If you have an object of class "Spacer3000" and call the function "fly" on it, then it will call the function shown. "%this" is the object and it's class is "Spacer3000".
So %this is the object (in this case a static sprite(?)) which is classed as Spacer3000. OK, so this use of this would be for differnt objects of the same class? For instance Class:Hero, Name:Hero1, Name:Hero2?









#51
10/25/2009 (5:03 pm)
Well... the new line "%this.redRiser = $redRiser;" could work. After you put it in and tested it, did it work?

Next, "%object" in "onEnter" _is_ the object entering the trigger. It doesn't make any logical sense to assign this to a specific object, then. Give it a minute's thought: what if you had 100 objects in your world that could go through the trigger? Do you think you are supposed to figure out what went into it?

Finally, making assumptions based upon previous posts, your Spacer3000 is a behavior, not a static sprite. But in general, yes, you'd have one class that can be assigned to many other objects.
#52
10/25/2009 (6:20 pm)
Quote:@Sherman. I am still trying to understand how %this is an "object" of it's class

function Spacer3000::fly(%this)
{  
%this.doThis();  
//I am trying to grasp how %this is an "object"

OK, assuming that you have a class Spacer3000, and that the class has the above fly() method. Now, let's create three objects that belong to the Spacer3000 class, or in other words, three instances of Spacer3000:

%foo = new ScriptObject(Bob) { class = Spacer3000; };
%bar = new ScriptObject(Bob) { class = Spacer3000; };
%baz = new ScriptObject(Bob) { class = Spacer3000; };

Now, when you want to call fly() for one of these instances in particular - without making all of them fly - you can do this:

%foo.fly()

Note that, even though the fly() method is declared as taking an argument called %this, I didn't pass any arguments to it when I called it. So where does the value of %this come from? It's the object for which the fly() method was called! In other words, when the above call to %foo.fly() runs, the %this inside fly() will refer to %foo. Likewise, if you call it with %baz.fly(), %this will refer to %baz. It's not just one object - it's whatever object the fly() method was called on.

Things are arranged this way so that, when you're writing the fly() method, you don't have to think about how many Spacer3000 objects there are outside of that method, or what variables refer to them. On the other hand, when you're looking at "the big picture," you can treat the fly() method as a "black box" and call it without having to think about what's inside it.

It's like using maps that have different scales. If you're travelling from Houston to Boston, you'll be looking at a large-scale map that has the major interstate highways on it, but doesn't include the smaller local roads that are irrelevant to your travel plans. But, once you get to Boston and you want to find your way around the city, you'll be looking at a subway map that doesn't include the interstates. At each "level of abstraction," you're given only the information that's required for you to perform the task at hand; if you had to keep everything in mind all at once, you'd be overwhelmed with information overload.
#53
10/26/2009 (4:28 am)
Sorry to go a little bit off-topic here, but I just wanted to say to William, Ted and Sherman (and many others ;) "thank you". It's good to know there are people like you on these forums.

Anyway, back on track.
Rennie, I'd love to be able to help you but, as I pointed out before, I'm pretty much novice, too, so I'll leave it to the big boys ;)
However, there is something that makes very difficult for me to understand your problems and how I could maybe help you with them.

As you know, in a script language like TorqueScript, there are some terms used with a special definition. Function. Method. Variable. Object. Call. Declare. Etc.
An object in TorqueScript is not the same object as you can have in continental law. Calling something in TorqueScript doesn't mean the same as calling a friend.

If you don't use the keywords of the language you're learning, it's hard to know what you mean. Same as if I would speak French or German to you. I'm not sure you'd understand what my problem is.

Maybe it could be a good idea to also focus on that part of TorqueScript?
#54
10/26/2009 (9:53 am)
@Seb: You're welcome. As is Rennie for the help I tried to give.

However, I think I will back away from helping Rennie, at least for the time being. A problem that I'm seeing is that there is not enough focus on one problem before moving on to the next one that is preventing him from learning things as fully as he should be before going off to the next lines of code, as is exhibited here where I've tried to help and he's changed course and focus within this thread without following through on the steps I was trying to outline for him to learn how to think like a coder. As I said before, if this doesn't work, I won't be able to help, and so I'm backing off.

@Rennie: Good luck to you, and I hope that you gain the knowledge you seek :)
#55
10/26/2009 (11:43 am)
Hi Guys. I am sorry for the late reply. I tried to post 3 times yesterday prior to Ted and Seb's post (you guys could be in a band together!) Anyhow, yes @William and @Sherman, yes thank you, you guys are great. I have gone over what you said and digested it a few times. As such I have continued, last night to work on my "redeRiser", tho I am still not bale to get it work.


What I am wondering is, if I want to call a new imageMap (one that is created onEnter), I am using getImageMap(%scenegraph), any how you will see if you look at the code, perhaps if you have a minute you can point out my glaring error, if there is one.


function centerTrigger::onLevelLoaded(%this, %scenegraph)
{
	%this.setEnterCallback(true);
}

function centerTrigger::onEnter(%this, %object)
{   
			%this.spawn();
			%object = $Spacer3000;	
}

function redRiser::spawn(%this)
{
	%this.redRiser.getImageMap(%scenegraph)
	{
		%scenegraph = new %this.redRiserImageMap;
	}	
	%this.redRiser.setPosition(0,0);
	%this.redRiser.setLinearVelocityY(40);	
}


@Seb, yes Sherman has made it clear I need to better my Torque vocabulary, which I am thanks, I recognize it's importance. Thanks.


@Ted
Yes I hope so too, perhaps if I was able to post last nigh, by seeing where I am struggling, in a relatively easy program (using trigger to spawn redRiser) you would not "back away". Anyhow, it is no worries and thanks!



#56
10/26/2009 (11:51 am)
From what I see (and again don't take my word for it as I might very well be totally wrong) I would say:

- line 09 seems to be useless.
- I see an error on line 14. Something important yet very tiny seems missing.

There might be other erros, but as I said, I'm definitely not a pro at TorqueScript so I won't point you out in the wrong direction...
#57
10/26/2009 (12:12 pm)
re: line 09 seems useless. Yah why? I mean Will had mentioned something about that line before, but why is it useless? Should it be more like %object.Spacer3000?


re:line14, mm ok cant see any error tho.
:?



Thanks for the hand Seb.
#58
10/26/2009 (12:36 pm)
for line 09, I'll let you think about what you're trying to do, when onEnter is called, when %object is created and what happens to %object during its lifespan (big hint ahead ;)

for line 14, it's something you know so it's just a matter to take a close look at every character in that line and every character that should be in that line (big hint again ;)
#59
10/26/2009 (1:17 pm)
Quote:perhaps if I was able to post last nigh, by seeing where I am struggling, in a relatively easy program (using trigger to spawn redRiser) you would not "back away"

Actually, this is why I'm backing away. You posted about having issues with a callback, and I was trying to help you with that, and you seemed to have forgotten that and moved on to another piece of script that has nothing to do with what you titled your thread. You have also stopped going through the process I was trying to use to teach you, in favor of simply asking others to debug your script for you.

I have no problem helping people, but I am only willing to do so if the person is willing to help themselves. In many ways, you are not, and I don't know why. So, rather than speculating or arguing or anything like that, I'm simply going to give up and go work on my own project...

Like I said, good luck with your project.
#60
10/26/2009 (1:54 pm)
The following script assumes a few things:
1) redRiserImageMap is already defiend in TGB (or elsewhere in scripts).
2) Objects that interact with this must have the "Send Collision" box checked in TGB (or elsewhere in scripts).
3) That your trigger is named "centerTrigger".

function centerTrigger::onEnter( %this, %object )
{
  %redRiser = new t2dStaticSprite()
  {
    sceneGraph = %this.getSceneGraph();
    imageMap = redRiserImageMap;
    size = "100 20";
    position = %this.getPosition();
    LinearVelocity = "0 -40";
  };
  %redRiser.schedule( 2500, safeDelete );
}