Game Development Community

Fish Game Tutorial

by Dan rgdyman · in Torque Game Builder · 02/10/2009 (4:36 pm) · 4 replies

Hello All... My name is Dan and Im currently working through the default tutorials and managed to find myself stuck in the ' advanced'
portion of the " Fish Game " tutorial. :)

Anyway... Im fairly new to programming/scripting so please bare with me
and save the 'heckling' till im out of the room.. ;)

I completed the first tutorial with flting colors.
Now im at the Fish Game chapter 4.

4. Adjusting Movement

My HERO...." The Fish ".... wont turn around for me when I tell it to in Game.. He will however, Change movement direction but still looks in the original facing direction. ( He's Moonwalking to the left)
Will Someone do a favor for me and have a look at what I have in the script and possably explain to me why this HERO wont turn around
before he gets eaten alive.....

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();");
}


function fishPlayerUp()
{
$FishPlayer.setLinearVelocityY( -15 );
}


function fishPlayerDown()
{
$FishPlayer.setLinearVelocityY( 15 );
}


function fishPlayerLeft()
{
$FishPlayer.setLinearVelocityX( -30 );
$this.setFlipX(true);
}

function fishPlayerRight()
{
$FishPlayer.setLinearVelocityX( 30 );
$this.setFlipX(false);
}


function fishPlayerUpStop()
{
$FishPlayer.setLinearVelocityY( 0 );
}

Thanks,
Dan

Ok so I have been troubleshooting this simple script and still the
(now called) Stupid Fish wont turn around. I broke the script down to just the " Left Turn Key " ( A ) and he still MoonWalks backward.
Heres what I have

function PlayerFish::onLevelLoaded(%this, %scenegraph)
{
$this.setFlipX(true);
$FishPlayer = %this;
moveMap.bindCmd(keyboard, "a", "fishPlayerLeft ();", "fishPlayerLeftStop();");
}

function fishPlayerLeft()
{
$this.setFlipX(true);
$FishPlayer.setLinearVelocityX( -30 );
}

function fishPlayerLeftStop()
{
$FishPlayer.setLinearVelocityX( 0 );
}

any ideas?

About the author

Recent Threads


#1
02/12/2009 (10:33 pm)
When you run your game, hit the "~" tilde key to pop up the console. At the bottom see if there is any red text that says anything about a "script error."

The problem I see:

function PlayerFish::onLevelLoaded(%this, %scenegraph)
{
$this.setFlipX(true);
$FishPlayer = %this;
moveMap.bindCmd(keyboard, "a", "fishPlayerLeft ();", "fishPlayerLeftStop();");
}

is "$this" should be "%this"

a $ means a global variable and a % means a local variable, int he function header it says:

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

So you also need to match that.

I'm guessing if you bring up the console it will mention that :)

If you game runs into a script error it will use the previously compiled script files, which is probably why your game isn't changing (it keeps hitting an error so won't update the script data).
#2
02/13/2009 (7:53 am)
If you game runs into a script error it will use the previously compiled script files, which is probably why your game isn't changing (it keeps hitting an error so won't update the script data).



Thank you so much.. This I didnt know.. :)

Yes... ' errors' popped up . I had no idea about "~"either..
live and learn.

After doing the above mention all is good now. ( watch out world )

Thanks again
Dan

I didnt want to start a whole new thread for this so here it is.

The " FishFood " isnt feeding my PlayerFish uponcollision()
The Food will in fact respawn() and the such. Just wont feed my fish so Im just swimming around and dieing ( after the fish shrinks of course)
So my " Stupid Fish " is starving to death ;)

Heres the FishFood

function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{

if(%dstObj.class $= "PlayerFish")
{
%srcObj.spawn();
%dstObj.modifylife(%dstObj.lifeValue);
}
else if (%dstObj.class $= "Fish")
{
%srcObj.spawn();
}
}

All the names match ( "PlayerFish" and " Fish " )
Im starting to get my head around " % " vs " $ " but still need practice at scripting. A few trial and errors with a couple nudges in the right direction I'll get it.

Thanks for any help

Dan



EDIT /////// current road block //////////

Alright Ive been digging through the DOCS today learning what the problem is with my " FishFood " problem. I found the onColliosion()
info. Also found out that (Please correct me if Im wrong)

%srcObject - t2dSceneObject
The object sending the collision. // This would be MY Player Fish


%dstObject - t2dSceneObject
The object receiving the collision // This Would Be My Fish Food

I sat back to think about whos doing what ( whos sending / whos recieveing) If both things are moveing whos sending and whos recieving?

If I was standing there picking my nose and someone come up to hit me
I would be recieving the collision ( right ?) But if I was swinging at the person the same time we both would be Sending and Recieving the collision at the same time.. ( right ? )

Im a little lost.. pls help me out.. :)
Dan


#3
02/13/2009 (10:49 pm)
Some good questions... the answer to what is sending and receiving (or more specifically what is handling the sending and receiving in your callback) is in the method name itself:

function FishFood::onCollision(%srcObj, %dstObj,

Your method is attached to the "FishFood", hence the %srcObj would be the instance of the FishFood (%srcObj could be replaced with %this)... %dstObj is being checked for "FishClass" so it's definitely the fish that is eating it.

Now to your problem, you have a slight difference in your script than the tutorial script... here's your script with the error in bold (btw t here are "[ code][ /code]" tags for this forum, minus the spaces I added, it would make your code more readable :):

function FishFood::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{

if(%dstObj.class $= "PlayerFish")
{
%srcObj.spawn();
%dstObj.modifylife([b]%dstObj.lifeValue[/b]);
}
else if (%dstObj.class $= "Fish")
{
%srcObj.spawn();
}
}

Here is the tutorial's script:

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

Once you finish this tutorial (you are nearly done) I would highly recommend the Behavior Fish Tutorial. You basically accomplish the same thing, but with behaviors. Behaviors can hugely help you create scripts that interact with the editor to increase efficiency :)
#4
02/14/2009 (8:09 am)
( whos colliding with who )

Thank you for clearing that up. It seems to be working. I say
"SEEMS " because the fish wont grow. But I looked at the script and I noticed there isnt a conditional test to grow instead of shrink..
What I see is this in modifylife function.
%this.life += %dmg;

Now with this on level load the fish will just grow. I changed it
to a ( -= ) instead of ( += ) and like magic the fish shrinks.

Please dont misunderstand me I am by no means " picking " on the tutorial.
I would preferr "deliberate" mistakes in the tutorial just to help
the " noob" ( me ) acually learn the code than just copy/paste the damn thing and learn nothing.. But at anyrate.. I will probably have
a million questions on this tutorial ( why is this and why is that )
I should probably just move on to what you said and goto the behavior
tutorial.. this will probably explain a few of my questions..:)

Thank you very much for taking the time me. Im sure I'll be returning with new and improved questions..

Thanks again..:)
Dan


/////// EDIT //////////

O.K. This is gonna be WAY NOOB question ( but it is what it is :) )
When I hit those wonderful road-blocks while Im doing a turtorial
I will always try to get it working on my own first..
Anyway....As I went through the DOCS I noticed I REALLY need to know
what " exactly" Im looking for... I went to Documentation,TGB,
TGB Scripting Language Documentation, ( This is were I found all
the onCollision() stuff. I got lucky and found it sinse I really had no idea what I was looking for.
So for my noob question..... " How would I know what exactly what Im looking for?" Hard question just to answer I know.. It depends what Im
stuck on.. a collision issue or something else...To understand what to look up.. would this be simply having XP in scripting/coding?