Referencing a previously created local var (i.e. %genericenemy)
by Brian Wilson · in Torque Game Builder · 04/06/2005 (2:03 pm) · 6 replies
The premise of what I want to do is spawn bad guys on spawn pads created with local vars.
Effectively, I want to create my spawn pads at various locations and then reference them later via an different function for spawning the bad guys.
Here's how my code is now, without being able to reference the pads:
I use the game(); function to represent external calls to the functions.
should I be copying the %pad (%pad.tag == 3) -> $pad# ( $pad3) via some function I'm not aware of and referencing them that way?
Effectively, I want to create my spawn pads at various locations and then reference them later via an different function for spawning the bad guys.
Here's how my code is now, without being able to reference the pads:
function game()
{
createPad(1, -25, -25);
createPad(2, -25, 25);
createPad(3, 25, -25);
createPad(4, 25, 25);
spawnEnemy();
}
function createPad(%num, %x, %y)
{
%pad = new fxStaticSprite2d() { scenegraph = t2dSceneGraph; };
%pad.setPostion(%x SPC %y);
%pad.tag = pad @ %num; // I'm assuming I can use this var to reference?
}
function spawnEnemy()
{
%enemy = new fxStaticSprite2d() { scenegraph = t2dSceneGraph; };
%enemy.setPosition(???) // I'd like to be able to pick the pad to spawn on here
schedule(1000, 0, "spawnEnemy");
}I use the game(); function to represent external calls to the functions.
should I be copying the %pad (%pad.tag == 3) -> $pad# ( $pad3) via some function I'm not aware of and referencing them that way?
#2
04/06/2005 (2:12 pm)
I'd suggest using an array, or maybe a SimSet holding invisible objects at the appropriate positions...
#3
04/06/2005 (2:16 pm)
Unless I'm missing something, I agree with LoTekk. Make them global =)
#4
The design is basicly sound though in that you don't want to just make everythign global. Thats just good practice in any language :)
04/06/2005 (2:34 pm)
A global simset would be a good idea. That way youd only have one global object.The design is basicly sound though in that you don't want to just make everythign global. Thats just good practice in any language :)
#5
Note that is written by memoryu (i'm at work) so might have some minor errors but you get the idea I hope.
04/06/2005 (2:45 pm)
Somethin like this:function game()
{
$setPads = new SimSet();
createPad(1, -25, -25);
createPad(2, -25, 25);
createPad(3, 25, -25);
createPad(4, 25, 25);
spawnEnemy();
}
function createPad(%num, %x, %y)
{
%pad = new fxStaticSprite2d() { scenegraph = t2dSceneGraph; };
%pad.setPostion(%x SPC %y);
$setPads.add(%pad);
}
function spawnEnemy()
{
%enemy = new fxStaticSprite2d() { scenegraph = t2dSceneGraph; };
%ran = getRandom(0, $setPads.getCount());
%pad = $setPads.getObject(%ran);
%enemy.setPosition(%pad.getPosition()); // I'd like to be able to pick the pad to spawn on here
schedule(1000, 0, "spawnEnemy");
}Note that is written by memoryu (i'm at work) so might have some minor errors but you get the idea I hope.
#6
Yeah, I could easily do it with globals, but I wanted more dynamic control and me being a "best practice" kinda guy, I would rather use locals whenever possible.
I actually had everything working using globals, which is usually how I mock up a concept, then go back and rework the code to make it more modular and dynamic, as well as clean up the kludgier stuff, which is where I was stuck.
@Ben
I'm not experienced with SimSets/Groups and that sounds like where I should go, as John clarifies.
@Chris
See my response from Teck
@John
That's exactly what I was looking for, thanks for the insight and code.
@All
Thank you all for your responses, this community kicks ass.
04/06/2005 (10:22 pm)
@Teck Yeah, I could easily do it with globals, but I wanted more dynamic control and me being a "best practice" kinda guy, I would rather use locals whenever possible.
I actually had everything working using globals, which is usually how I mock up a concept, then go back and rework the code to make it more modular and dynamic, as well as clean up the kludgier stuff, which is where I was stuck.
@Ben
I'm not experienced with SimSets/Groups and that sounds like where I should go, as John clarifies.
@Chris
See my response from Teck
@John
That's exactly what I was looking for, thanks for the insight and code.
@All
Thank you all for your responses, this community kicks ass.
Torque Owner Teck Lee Tan