Game Development Community

Fishy Game with Points

by Drymetal · in Torque Game Builder · 04/22/2012 (2:50 am) · 11 replies

I did all the tutorials up to the Mole whacking one. The Fishy Game I wanted to try different things so I can learn using that project. So, I thought a point system similar to the one in the Mole Tutorial would be cool to add. Except - I can't get it to work. :( The scores are showing zeros after them so that part is working just fine. The issue is where I am trying to get it to update the fishies eaten or missed.

Here is the code for the page I added all this to. The very bottom I have it marked MY ADDITION FOR POINTS. The code there is giving me errors when I run the game. I'm not entirely sure why. Anyways - the code and a screenshot of the errors:

function FishFood::onLevelLoaded(%this, %scenegraph)
{
   
   %this.startPositionY = %this.getPositionY();
   %this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
   
   // THIS IS MY GUI SECTION
   %this.eatenCount = 0;
   %this.missedCount = 0;
   // Reset the GuiTextCtrls
   FoodGuiEaten.text = "Eaten:" SPC %this.eatenCount;
   FoodGuiMissed.text = "Missed:" SPC %this.missedCount;
}
//Then we'll add functions to increase score and update the GuiTextCtrls.
function counter::incEatenCount(%this)
{
   %this.eatenCount++;
   FoodGuiEaten.text = "Eaten:" SPC %this.eatenCount;
   
}
function counter::incMissedCount(%this)
{
   %this.missedCount++;
   FoodGuiMissed.text = "Missed:" SPC %this.missedCount;
}
// END OF GUI SECTION
function FishFood::onWorldLimit(%this, %mode, %limit)
{
   if(%limit $= "bottom")
   {
      %this.spawn();
   }
}
function FishFood::spawn(%this)
{
   %this.setPosition(getRandom(-50, 50), %this.startPositionY);
   %this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
   if(%dstObj.class $= "PlayerFish")
   {
     //MY ADDITION FOR POINTS
	  %this.incEatenCount();
	 //END OF MY ADDITION FOR POINTS
      %srcObj.spawn();
      %dstObj.modifyLife(%srcObj.lifeValue);
   } else if (%dstObj.class $= "Fish")
   {
	 //MY ADDITION FOR MISSED POINTS
	  %this.incMissedCount();
	 //END OF MY ADDITION FOR MISSED POINTS
      %srcObj.spawn();  
   }
		
}


Screenshot:
chadmccomas.com/1515.JPG


Any thoughts on what I'm doing wrong here? Thanks!

#1
04/22/2012 (11:59 am)
%this.incMissedCount();

That is the problem. %this means the current instance of the object.

But incMissedCount() is of type counter, the collision callback of type FishFood.
So you would need to use a different approach. I would add the counters for missed and catched food to the player and then use a global player variable to acces it.
Or add it to FoodGuiEaten/FoodGUIMissed.
#2
04/22/2012 (1:58 pm)
Ok. If I'm understanding wrong - it is probably due to too much time spent at Woodstock. Let me see if I understand correctly.

So the whole first part of the Gui - from // THIS IS MY GUI SECTION to // END OF MY GUI SECTION; I'll put all that in my player.cs file. Then I'll create a new global variable like: $newVariable = incMissedCount();?

And then on my collision thing on this file - instead of %this.incEatenCount();, I'll put $newVariable?

I'm telling ya - Woodstock was rough man. :( I hate to ask, but can you give me an example of what you mean? I don't think I'm accurately wrapping my head around this.
#3
04/22/2012 (5:03 pm)
With what you are trying to do originally, you are making the variable holders eatenCount and missedCount part of the level 'object'. Your functions would need to be type FishFood to work using the %this calls.

You could just make the counters into global variables and the functions not related to any particular object. Something like this:

function FishFood::onLevelLoaded(%this, %scenegraph)
{
   
   %this.startPositionY = %this.getPositionY();
   %this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
   
   $eatenCount = 0;
   $missedCount = 0;
   FoodGuiEaten.text = "Eaten:" SPC $eatenCount;
   FoodGuiMissed.text = "Missed:" SPC $missedCount;
}

function incEatenCount()
{
   $eatenCount++;
   FoodGuiEaten.text = "Eaten:" SPC $eatenCount; 
}

function incMissedCount()
{
   $missedCount++;
   FoodGuiMissed.text = "Missed:" SPC $missedCount;
}

function FishFood::onWorldLimit(%this, %mode, %limit)
{
   if(%limit $= "bottom")
   {
      %this.spawn();
   }
}

function FishFood::spawn(%this)
{
   %this.setPosition(getRandom(-50, 50), %this.startPositionY);
   %this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}

function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
   if(%dstObj.class $= "PlayerFish")
   {
        incEatenCount();
         %srcObj.spawn();
      %dstObj.modifyLife(%srcObj.lifeValue);
   } else if (%dstObj.class $= "Fish")
   {
	  incMissedCount();
       %srcObj.spawn();  
   }

#4
04/22/2012 (5:50 pm)
That ended up working exactly like I intended. I think the issue I'm having is in wrapping my head around the %this thing or the why and when it is needed.

So, I'm going to use the dumbest analogy since the dawn of man in an effort to see if I understand the difference between yours and mine. Before, was my code made in a way that the stuff I needed was in a block or house type thing - you know, even though it is on the same page, it is confined to a particular block. And when I tried to access it in a different "block" or whatever - it couldn't find it because they were separate?

But - the way you did it - it isn't being confined to any certain place and can be found later on in the page?

So why did the mole whacking tutorial have all that %this stuff in it if it wasn't needed? Ugh. On the bright side - once I wrap my head around the basics, it should get easier. (At least PHP did.)
#5
04/23/2012 (7:44 am)
Right.

The "block or house type thing" you mentioned is a CLASS. Down at the bottom of the first page of the whack-a-mole tutorial you see a point where it tells you to change the class of the object to "mole".

After that, said object will be able to use any functions that start with "function mole::". That's how classes roll.

The reason for using classes shows up with things like the onWorldLimit, Spawn, and onCollision functions that Harry above left IN the fishfood class. Each bit of food is the same and has the same function needs. You wouldn't want a top-level (no class) function for each of them like:

SpawnFirstFishFoodPiece(), SpawnSecondFishFoodPiece(), etc..

It would get out of hand rather quickly. Spawning is just something that each piece of fishfood has to do, and they'll all do it just the same as each other.

The "%this" inside of any function is the actual object... so in the case of your original code up top, calling "%this.incMissedCount()" was resolving to something like "fishfoodpiece1.incMissedCount()".

fishfoodpiece1 is of class "FishFood", so it went looking for:

function FishFood::incMissedCount()

and couldn't find it. That class doesn't have that function. In the Mole tutorial part 2 they go through the trouble of changing the class of the scenegraph (the whole world, really) to "MoleLevel". Then in mole::animationEnd() they call "%this.scenegraph.incWhackedCount()", which takes a mole (%this), finds it's scenegraph (of class moleLevel), and then calls the incWhackedCount for that scenegraph.

Seems awfully convoluted to me for such a simple thing as a score!

Anyway... sorry if this was long or rambly.

I think some of the tutorials are messing with people that are newer to certain types of programming more than they're helping!


#6
04/23/2012 (11:54 am)
Too bad there isn't an idiots book for Torquescript; I'd buy it in a heartbeat. Oh well.

I took your previous advice and bought Torsion today. If anything I like that it colors everything so it is easier to read and I also like that when I point to %this <- It shows me what object this is. That should help a LOT.

I don't know what breakpoints are. THough that seemed to be a big thing. And I don't know how to use the debugger yet. But maybe that will be great to have also. The browser thing on the left is great as well as being able to open the project instead of 20 files.

Classes, functions, objects, variables, oh my. I wonder if any online colleges offer torquescript classes. :)

#7
04/23/2012 (12:11 pm)
You know what? That's exactly what I'm going to do. In 75 years or so whenever I become an expert at Torquescript, I'm going to create my own online school for poor little people like me who want to learn it.

I'll create a program that will show more and more advanced code and it will be like an English assignment in High School. Remember: "Click on all the transitive verbs in the following paragraph and then click on the direct subject and all of the objects for each transitive verb."

Except my beautiful program will say something like, "In the following page of code, click on all the functions." -or- "In the following 10,000 lines of code, the variable $tinTinRooRoo occurs 753 times. Find what the variable means and paste it in the box."

In fact - other than all these superlative exercises, the whole class will be creating a massive video game; including all the polish, graphic design, audio design and coupons for a bag of popcorn from your local Wal•Mart."

Man - I'll be rich. Filthy Rich. It'll be such a awesome class that even Gabe Newell will sign up just so they can learn the most efficient way to create a game.
#8
04/23/2012 (12:14 pm)
Yeah I've pondered writing a book on T2D... seems like there's a market. But it'd be a huuuuuge time commitment that I just can't make right now.

Plus I'm a horrible programmer. :D I'd probably accidentally spread my bad habits all over the T2D community, and that would be no good for anyone.

breakpoints are the awesomest. So you've got your fishy game project open in torsion... you go to the line you want... say line 17 in your above sample inside incEatenCount(). Now you click just to the left of the line number in Torsion in the grey area and it'll drop a red dot.

Now hit F5 to start the debugger, (I highly recommend running your game in windowed mode when doing this), and when the game reaches that line of code it will PAUSE and bring you back to Torsion.

From there, you can use F10 and F11 to step through lines (11 goes INTO functions on that line, 10 just executes the whole line) and you can bounce through the program seeing what happens.

At the bottom, click on the "Watch" tab, and drop something into one of the Name fields...like say: %this.

It'll tell you what %this is at that time. Or try %this.getname() to get the name instead of the ID number. You can run functions in there... so %this.getposition() or guy5.whatismyhealth() or whatever will work.

In this way at any given point in your game (the breakpoint) you can check the value of anything you'd like! $gameScore, $timeofday, areTheseTwoColliding(%thing1, %thing2), etc.. Whatever you need.

line by line debugging like that will be your best friend if you end up doing a lot of Torquescripting!
#9
04/23/2012 (5:12 pm)
Hmmm. That does sound pretty darn useful. That's pretty smooth it can do it without crashing anything. Ten bucks said 99% of my time will be spent going one word at a time.

In regards to writing a book - I think it is needed. But since a book isn't feasible for anyone as it would require an inane amount of time - we should form a group and get as many people involved as possible to come up with superb documentation. I'm serious. I could be the guinea pig. lol If I can't understand it - grandma Matilda won't be able to either and therefore the documentation would need to be rewritten.

It does makes sense. If like 30 (or 1,000) people collaborated on coming up with an excellent source of documentation it would make TGB more accessible to new people (read Idiots. E.g., Me.) which would make the community bigger. It would also help out those who already have an understanding of it - and ultimately it would lead to more quality projects being made in the community which could only help us all.
#10
04/25/2012 (9:14 pm)
I'm all for a group collaboration to write superb documentation. I'd even be willing to donate some of my time. It's just I would want someone organizing all the work that needs done. An overseer if you will. Me just going into TDN to write articles is not enough, and I would wind up hosting the tutorials I would write in my own space.
#11
04/26/2012 (4:35 am)
I'm all for it too. I'd be up for organizing all the work and "grading" things that are written. (From the perspective of a noob.) I'm half tempted to create a forum on the backend of one of my sites just so there could be collaboration. I just wonder how many people would volunteer and be willing to help create it?

I think the most important and critical part - at first - would be in explaining the language in a way that is simple and easily accessible. And if we could make an extensive list of scripts that do specific things - I think that'd be awesome. Along with tutorials of course.

I think the more minds that would dedicate to it - the more efficient, cleaner and practical the documentation would be. And surely - regardless of programming level - anyone and everyone could benefit from a project such as this.