Game Development Community

heat seeker or lack there of

by rennie moffat · in Torque Game Builder · 05/08/2010 (4:19 pm) · 6 replies

I have created a behavior of an enemy weapon which shoots its bullets to the last recorded destination of my player, or so I would hope. The problem, and perhaps I am not fully understanding something with programming but, it works like this....


onBehaviorAdd()
{
%this.schedule(100, shoot);
}

function shoot()
{
%target = %this.hero.getPosition();
%this.bullet.moveTo(%target);
}



the problem is that, if my player moves, early enough after the bullet has been fired, the bullet, bends. It changes direction to a new destination closer to the player/heros new position. Since this %target, is not called in onUpdate, but in a stand alone function, why would the %target change?


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.


#1
05/10/2010 (12:00 pm)
Logically, you'd want to attain a target position before the delay:

onBehaviorAdd()
{
%myTarget=%this.hero.getPosition();
%this.schedule(100,shoot,%myTarget);
}

function shoot(%this, %target)
{
%this.bullet.moveTo(%target);
}



I'm probably doing this incorrectly by syntax, but that's the idea.
#2
05/10/2010 (12:07 pm)
ok. In schedules I have not gotten into passing a variable into the new function besides %this.

so my schedules have been like this...


%this.schedule(350, shoot);



without doing the coding as of yet, just thinking out loud (so to speak), does the way you did it...


%myTarget=%this.hero.getPosition();
%this.schedule(100,shoot,%myTarget);


better insure the target is locked? I am guessing so.

thanks.
#3
05/10/2010 (12:22 pm)
Schedules pass arguments differently than regular functions, so doing:

%this.schedule(100,shoot,%myTarget);

is the same as

%this.shoot(%myTarget) when not using a schedule.

DO NOT DO %this.schedule(100,shoot(%myTarget)); <----!!! Will cause errors. Don't pass arguments through the function.
#4
05/10/2010 (12:47 pm)
hmmm. ok,
I just realized I made a mistake in reading your code.


I confused %myTarget with %target.


So, why would you use something like that? as in why would you pass %target into the inputs of the function? Is %target supposed to be %myTarget?





(novice programming question I am sure, but something I need to better comprehend)
#5
05/10/2010 (1:15 pm)
I used %myTarget so you wouldn't be confused.

I'm passing %myTarget into the function, then I'm referencing it with the variable %target once I'm in the shoot function.

Once you get into the Shoot function, it no longer remembers what %myTarget is because % mean local variable. That's why you have to pass the argument into the function.

As others have said, I suggest some side reading about the basics of object oriented programming and Variable Scope. It will help a lot of your small issues that come up continually.
#6
05/10/2010 (1:57 pm)
hmm, ok. no worries tho. I appreciate the feedback.

Cheers>

Really for me it is better understanding the power of passing of an argument into a function that isnt already set up in torque, ie %this, or %dstObject, etc. To, create my own, I am not there yet, in terms of comfort level.


But thank you.