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.

#21
10/23/2009 (2:26 pm)
Quote:well he is supposed to (with the getPosition info) decide wether or not he is onWorldLimit and if so, do something.

Okay, so how do world limits work? onWorldLimit is a callback (free hint there). So the next steps are:

1) How does the onWorldLimit callback work? When does it get called? Why does it get called?

Don't be abstract in the answer- we're talking about a specific function here, so tell me about it...
#22
10/23/2009 (3:48 pm)
How does the onWorldLimit callback work?
-The callback and it's variables.
onWorldLimit(%this, %limitMode, %limit)

When does it get called?
-It gets called as a function for, in this case enemy class.

Why does it get called?
-It gets called just as a fucntion amongst code. It is on its own as a function. That means that in the reference material I looked at there is no setWorldLimit or getWorldLImit anywhere, let alone, getPosition. Just onWorldLimit with if statements involving the 4 different cases, top, bottom, left, right and the action to take when one case is reached.


So what I am thinking is, that an object (in these two reference cases, Astroids and Fish) is assigned it's worldlImit in the editor, and the onWorldLimit, by default recognizes the cases of top, left etc, and will simply install the behaviors you have programmed the object to make once it hits anyone.


:?







If I am right, I only have one question tho, in some cases I will want to have a continually moving background, (the ultimate perception is that the camera is continually scrolling to the right at 10 units/sec) If I want to do that, I can not adjust an enemy's worldLimit this way. I need to access it in a different manner as I will have to setWorldLimit to the position of the camera and its position.


So in that, this is where I am thinking.

















#23
10/23/2009 (3:57 pm)
Quote:The callback and it's variables.

Not what I asked. Go look it up and find out how it works.

Quote:It gets called as a function for, in this case enemy class

Not what I asked. Go look it up and find out how it works.

Quote:It gets called just as a fucntion amongst code. It is on its own as a function. That means that in the reference material I looked at there is no setWorldLimit or getWorldLImit anywhere, let alone, getPosition. Just onWorldLimit with if statements involving the 4 different cases, top, bottom, left, right and the action to take when one case is reached.

Not what I asked. Go look it up and find out how it works.

I honestly didn't know how to use the world limits callback until this thread, and then I looked it up in the online docs here. Took me less than five minutes to find the information. I suggest you go to the docs and look up the reference for the information rather than trying to discern the meaning from looking at scripts or tutorials. You could have even typed in "onworldlimit" into the forum search box and learned a great deal from the other threads.

The next step will not proceed until you've done that.
#24
10/23/2009 (4:03 pm)
But does it feel to you that I am on the right track. I am grasping it, it is just he essentials are missing,










but how then am I "not answering what you asked."?







#25
10/23/2009 (4:05 pm)
ps, working on it.


Back to you in a bit.

Ren
#26
10/23/2009 (4:18 pm)

T:How does the onWorldLimit callback work?
R:The callback and it's variables.
onWorldLimit(%this, %limitMode, %limit)




Well, %this is the object(this is where I actually get confused.) What is %this?

%this as explained in C++ for Dummies says %this is a pointer to point out the current object with in a member function. So again I do not understand, and this time it is thru member function...

so Member function is defined as something else. To be honest to try and grasp the concept of what a member function is, as am looking at it in my C++ book is a little much, any how that is that so far.




T:When does it get called?
R:Here I am not sure. Is it continually, like all functions are?




T:Why does it get called?
R:I am not sure. Why does it get called?









#27
10/23/2009 (8:06 pm)
Quote:Well, %this is the object(this is where I actually get confused.) What is %this?

Rennie, I didn't ask you what %this was. I asked how the function onWorldLimit worked. I even told you where to go get the information. And instead, you looked up %this in a C++ book. Go look up the function itself and find the information. Your first issue is with following directions- I'm not sure if you have an aversion to that because you don't like being "told" things, or because you think that you need to find information out in your own way, but if you don't want to look for the correct information, you're going to confuse yourself. And Torque Script, while somewhat like C++, has some pretty big differences, so referencing a C++ book for Torque Script is not going to benefit you as much as referencing the Torque Script references in the documentation section (where you would also find information on world limits).

Quote:Here I am not sure. Is it continually, like all functions are?

Functions are not called continually unless done so from a loop. And again, if you had looked up the information in the documentation, you would be able to answer this question.

Quote:I am not sure. Why does it get called?

I'm not answering that. That's your job. I'm going to quote myself from the earlier post so you know where to get the information:

Quote:I honestly didn't know how to use the world limits callback until this thread, and then I looked it up in the online docs here. Took me less than five minutes to find the information. I suggest you go to the docs and look up the reference for the information rather than trying to discern the meaning from looking at scripts or tutorials. You could have even typed in "onworldlimit" into the forum search box and learned a great deal from the other threads.

Get the information, answer the questions, and advance to the next round of figuring out how to make you Enemy do "something" when it hits a world limit...

Refuse to look up the information, or ask me to just give you the answer, and I will refuse to help you with this or any other problem you post. You must do the work.
#28
10/23/2009 (9:44 pm)
Quote:
referencing a C++ book for Torque Script is not going to benefit you


I was only referencing to grasp the True basic concept of of certain items in Torque Script that are common in C++ like a pointer aka %this.

No you did not ask me what %this was, but I asked myself Ted. So please, easy on the motion. I am still in a stage where I am confirming what very rudimentary things are, like a "pointer" aka %this. so again, please my friend, do not attack me for learning.

Keep in mind, I use these threads as personal notes, so sometimes I write what I am thinking, this allows me to think things thru better when i come back to an old post I can better follow along in the future that way.


Quote:
T:When does it get called?
R:Here I am not sure. Is it continually, like all functions are?
T: Functions are not called continually unless done so from a loop. And again, if you had looked up the information in the documentation, you would be able to answer this question.

I looked for quite sometime at various things in TGN, if I knew it how to give a quick answer, I would, but currently, I CAN NOT give a CLEAR Conscise answer, so rather than typing gooble dee guk, I said I am unsure. If I knew it would have been trouble I would not have said anything. Sheesh.

Mind I spent about a half hour to an hour specifically looking at the answers to your questions. However I spent most time on the first bits. In the future if I not have a full answer I will not ask.


Quote:
T:Why does it get called?
R:I am not sure. Why does it get called?
T:I'm not answering that. That's your job. I'm going to quote myself from the earlier post so you know where to get the information:


I think this is foolish Ted, You have asked me to do some research, I have and did, by the end of it, I did not have a CLEAR idea of what it was, so instead of writing gooble dee guk I asked. My bad. Thanks anyways tho.


Again the perception that I am "Refusing to look up the information" is false. I am doing the work and I hope you can understand I am telling you my truth. So if that is not good enough, oh well but I hope it is not.













Good day Ted,
Ren











#29
10/23/2009 (10:00 pm)
RE: Why does it (worldLimit) get called?

I hope you do no think I am trying to sound like an smart ass. But my reasoning as to why an onworldLImit function is called is because it is there. It exists, so since it is there, it has to be called.
#30
10/23/2009 (10:10 pm)
Rennie, I don't mean to offend, but have you considered the possibility that programming may not be for you? It does take a certain mindset, and it's pretty obvious that what you're reading just isn't "clicking."

There's no shame in it, if that's the case. We all have our strengths and weaknesses. None of us can do everything well, and that's why game development tends to be a team effort. Have you considered teaming up with a good programmer, so that each of you can focus on what you do best?
#31
10/23/2009 (10:36 pm)
Quote:But my reasoning as to why an onworldLImit function is called is because it is there. It exists, so since it is there, it has to be called.

No, simply declaring a function doesn't cause it to be called. It has to be called explicitly, from some other TorqueScript or C++ code, and the statements inside the function are not executed until that happens. When some other code says "%foo = bar(%baz)", it's calling the bar() function, passing the value of %baz to it as an argument, and storing the value it returns in %foo.

A "callback" is a function that is called by the engine when a certain event happens. If you check the reference docs for the t2dSceneObject, you'll find a description of onWorldLimit(). The first paragraph, cunningly labeled "Event", describes what event will cause the engine to call it.

Quote:If I knew it would have been trouble I would not have said anything.

You're missing the point. It's not that quick answers are too much trouble, it's that giving them would be like handing out calculators in math class - it will help the students pass the test, but it won't help them learn how to multiply or divide. Discussing the material and assigning homework may be more difficult for the student (i.e. you!) right now, but it's far, far more helpful in the long run.
#32
10/24/2009 (1:19 am)
Quote:do not attack me for learning

None of what I said was an attack, Rennie (we know that text does not carry tone). What I'm doing is telling you to stay focused. If you can't focus on the task, then I can't help you step through these problems in the way that will teach you how to troubleshoot, and thus I can't help you. Focus is a big thing with coding and scripting- if you allow yourself to get distracted by tangential things when trying to run down a problem, at best you're adding to the amount of time to solve a problem, and at worst you're going to lose sight of what you were looking for.

Quote:Keep in mind, I use these threads as personal notes, so sometimes I write what I am thinking, this allows me to think things thru better when i come back to an old post I can better follow along in the future that way.

I've noticed that. These aren't my forums to say the best way that they can be used, but I think it's probably easier for you to keep a journal- I do this myself for my design (as well as tacking pieces of paper with functions scribbled on them to my wall... it's less creepy than it sounds).

Quote:You have asked me to do some research, I have and did

I think Sherman said it best with the analogy of the calculators in math class. The information was sitting there the whole time, but what I wanted you to learn was not just how the world limits worked, but how to spot the problems in the script, which is far more valuable to learn.

Look, it's not that people don't make scripting mistakes. I do it all the time, and for simple stuff like typos, reusing variables when I should be using others (caused me an hour's worth of grief the other day until I noticed it), or even forgetting to put the ; at the end of the lines. Most code/script has many errors when first entered, and teh better a person is at finding the errors, the faster they can fix them and move on.

My tack here was to make you focus not on the specific answer, but on the methods. You tend to happily take tangents and then relate them, forgetting about the original vector of conversation. In normal cases, this is fine, but I needed you to concentrate on the task at hand in order to learn, or else you would drift away from it and then not learn it.

I would say that if you want to continue this, it's up to you, and you can only learn more from it. However, if you don't desire to do this, then you may want to consider what Sherman brought up as well (I'm one of ten people on my team- we can't do everything by ourselves, as much as we might want to).
#33
10/24/2009 (3:56 am)
Because I like to brag, I just want to mention that I've never used world limit functions before, and this code took me just 1 minute to write and 30 seconds to test.

// Make sure all enemies are just of class "Enemy", don't set their
// name to "Enemy"!  This also assumes that the enemies were placed
// within the screen already.
function Enemy::onLevelLoaded( %this )
{
  // Give it an initial speed!
  %this.setLinearVelocityX( 100 );
  %this.setLinearVelocityY( 100 );
  
  // My world is 1024x768 centered around (0,0).
  %this.setWorldLimit( "NULL", -512, -384, 512, 384, true );
}
 
function Enemy::onWorldLimit( %this, %limitMode, %limit )
{
  // If I've hit the left, start moving me to the right.
  // If I've hit the right, start moving me to the left.
  // If I've hit the top, start moving me down.
  // If I've hit the bottom, start moving me up!
  switch$( %limit )
  {
    case "left":
      %this.setLinearVelocityX( 100 );
    case "right":
      %this.setLinearVelocityX( -100 );
    case "top":
      %this.setLinearVelocityY( 100 );
    case "bottom":
      %this.setLinearVelocityY( -100 );
  }
}
#34
10/24/2009 (4:33 am)
@Ted - Sorry, Ted, to interrupt your thread (pun not intended). But I've tried your approach on a simple level. (Although, I'm keeping in mind what you've done here for the day when I teach programming. I think it would really jump-start somebody who is trying to get started in programming but has no background.)
#35
10/24/2009 (11:36 am)
Quote:Most code/script has many errors when first entered, and teh better a person is at finding the errors, the faster they can fix them and move on.

I see what you did there... :-)
#36
10/24/2009 (1:04 pm)
Quote:
Rennie, I don't mean to offend, but have you considered the possibility that programming may not be for you? It does take a certain mindset, and it's pretty obvious that what you're reading just isn't "clicking."

There's no shame in it, if that's the case. We all have our strengths and weaknesses. None of us can do everything well, and that's why game development tends to be a team effort. Have you considered teaming up with a good programmer, so that each of you can focus on what you do best?


at first I thought perhaps there was no way, but I am determined, I do understand it. Perhaps my questions seem "stupid" (notice the quotations, as it is all relative) but I am sure I will get there. Each day I sit down it begins to make more and more sense. I have only been dealing with SERIOUS, when I say serious I mean Torque Script for 3 months now. Prior I knew how to link pages with buttons in flash, not much really. So it is along road for sure and I am willing to get there. And I will too.







Quote:
No, simply declaring a function doesn't cause it to be called. It has to be called explicitly, from some other TorqueScript or C++ code, and the statements inside the function are not executed until that happens. When some other code says "%foo = bar(%baz)", it's calling the bar() function, passing the value of %baz to it as an argument, and storing the value it returns in %foo.

see, this is a concept I am just beginning to "get" now. I got it before, but like how a kid nows the colour red is red, thats it. NOt like how I know it is a reflection of light and that in RGB scale it is 100%R 0%B 0%G or any of the infinite variations you can get by playing with it.

Thanks, I am really starting to "get" it now.





Quote:
A "callback" is a function that is called by the engine when a certain event happens. If you check the reference docs for the t2dSceneObject, you'll find a description of onWorldLimit(). The first paragraph, cunningly labeled "Event", describes what event will cause the engine to call it.

ok, see I am now, thanks to this able to clearly differentiate the difference between the method and the callback. For instance setWorldLimit is a method. Therefore it can go inside a function. However onWorldLimit can not. is this true? is this the right approach to dealing with callbacks and methods respectively?




Quote:
You're missing the point. It's not that quick answers are too much trouble, it's that giving them would be like handing out calculators in math class - it will help the students pass the test, but it won't help them learn how to multiply or divide. Discussing the material and assigning homework may be more difficult for the student (i.e. you!) right now, but it's far, far more helpful in the long run.

I agree, thanks guys, this has been very helpful.


TGB all the way!










@ William
Thanks, yes I am a newb, you are not, anyhow! (that is said jokingly) I see, I have seen this before, what you have written but as written above am truly starting to grasp it now. However, just knowing what each variable is I still have trouble in KNOWING how they relate.

My trouble in thinking tho may have been sorted out. I was (and still not 100% in all cases) but in worldLimit , onWorldLimit is called because setWorldLimit is set to callback "true".



I am thinking my main problem is with callbacks and methods. If this is the case just recognizing that now I should be able to move on just fine. But I do think that is the root of my problem, for instance with getPosition.



More specifically with onUpdate, so I suppose my quick question is what is update? and what are generally plugged into onUpdate. This is what I am researching now.



Thanks for all your help everyone and sorry for the late reply, I intended to write this morning but my internet was down until just now. I appreciate it.

Ren
#37
10/24/2009 (1:38 pm)
Quote:ok, see I am now, thanks to this able to clearly differentiate the difference between the method and the callback. For instance setWorldLimit is a method. Therefore it can go inside a function. However onWorldLimit can not. is this true? is this the right approach to dealing with callbacks and methods respectively?

I have no idea if your mental model is right or not, because I have only a vague idea what you really mean when you write that "it can go inside a function." Functions and methods don't "go," they're either declared or called. Declaring one defines it, and calling one causes the code you've defined for it to be executed.

OnWorldLimit() is a method and a callback. A callback can be either a function or a method; the word "callback" describes how it's called, not what it is. What makes it a callback (or, more accurately, a "callback method") is that instead of you calling it from some other code that you've written, the engine will call it when a certain event happens.

Edit: BTW, I'm not questioning your determination, just pointing out that determination isn't all that's needed. You also need to have the ability to think in a certain way. It's not about "stupid" or "smart" either - I've known some incredibly smart people who just could not get it. Similarly, I can't play music no matter how hard I've tried to learn. I just don't have that gene in me, and no amount of work will change that.
#38
10/24/2009 (1:42 pm)
Quote:
Functions and methods don't "go," they're either declared or called. Declaring one defines it, and calling one causes the code you've defined for it to be executed.


oh I see. This helps a lot thanks.

One quick question though regarding this, can a method be a function?


#39
10/24/2009 (1:46 pm)
Quote:One quick question though regarding this, can a method be a function?

Yes, a method is a type of function that's associated with an object. You call it by writing "%object.methodName()". A function stands by itself; you call it with "methodName()".
#40
10/24/2009 (1:52 pm)
So the difference between a method and function is only their position, by that I mean how they are used. Meaning, I call a...

function moveTo()
{
%this.point()
}


but I could have a

function goHere()
{
%this.moveTo();
}




I know this is really brief, and I am about to run out the door, but this is the same ballpark we are talking about?