Game Development Community

Spawning an object

by Richard Pettyjohn · in Torque Game Builder · 11/27/2006 (5:55 pm) · 16 replies

I've been trying to spawn an object at the mouse position when I press a key.

I came up with a couple of functions but I must be doing something wrong since my echo in the spawn function doesn't show up in the console and when I try to execute the function mannualy in the console I get I lot of "could not find object ' ' ".

Is there a easy way to do this?

#1
11/27/2006 (6:10 pm)
Sounds like one of your variables is not initialized like you think it should be. Try throwing in some liberal error statements like:

error("value of variable %myvar right now is: " @ %myvar);

and see which of your variables is not initialized. That should get you started. If you get some results you don't understand, post them here with some code in [ code ] tags and we can take a look.
#2
11/28/2006 (5:16 pm)
Ok, here's the code (I found most of it in the scroller demo):

function spawnRiffle()
{
   %riffle = new t2dStaticSprite()
   {
      scenegraph = %this.scenegraph;
      class = riffle;
      layer = %this.layer+1;
   };

   return %riffle;
}

function riffle::start()
{
    %this.setPosition("0, 0, 0, 0");
    %this.setWorldLimit( kill, "-52 -40 52 40" );
    %this.setImageMap(riffleImageMap);
    %this.setSize(3, 1.5);
    %this.setCollisionActive( true, true );
    %this.setCollisionPhysics(false, false);
    %this.setCollisionCallback(true);
}

The keyBind is sopose to execute the first one then the second one, here it is:

moveMap.bindCmd(keyboard, "w", "spawnRiffle();", "riffle::doNothing();");

I get these errors in the console when I push "w":

disagreement/gameScripts/spawner.cs (15): Unable to find object: '' attempting to call function 'setPosition'
disagreement/gameScripts/spawner.cs (16): Unable to find object: '' attempting to call function 'setWorldLimit'
disagreement/gameScripts/spawner.cs (17): Unable to find object: '' attempting to call function 'setImageMap'
disagreement/gameScripts/spawner.cs (18): Unable to find object: '' attempting to call function 'setSize'
disagreement/gameScripts/spawner.cs (19): Unable to find object: '' attempting to call function 'setCollisionActive'
disagreement/gameScripts/spawner.cs (20): Unable to find object: '' attempting to call function 'setCollisionPhysics'
disagreement/gameScripts/spawner.cs (0): Unable to find object: '' attempting to call function 'setCollisionCallback'

Ben, I'm not sure how use the error statement you posted but I think the variables all have problems according to the console.

Anyone know whats wrong?
#3
11/28/2006 (7:25 pm)
The first problem you've got is this function:
function spawnRiffle()
{
   %riffle = new t2dStaticSprite()
   {
      scenegraph = %this.scenegraph;
      class = riffle;
      layer = %this.layer+1;
   };

   return %riffle;
}

This function is not passed %this, so %this will be blank - so you're adding an object to nowhere because %this.scenegraph is undefined.
Your second method also doesn't work because you don't have %this as a parameter, so you're calling all those methods on nothing ;)

To get it closer to working, you'd edit it like so:
function spawnRiffle()
{
   %riffle = new t2dStaticSprite()
   {
      [b]scenegraph = $myScenegraph;[/b] //Assign this global variable somewhere, maybe startGame in game.cs
      class = riffle;
      layer = %this.layer+1;
   };

   return %riffle;
}

function riffle::start([b]%this[/b])
{
    %this.setPosition("0, 0, 0, 0");
    %this.setWorldLimit( kill, "-52 -40 52 40" );
    %this.setImageMap(riffleImageMap);
    %this.setSize(3, 1.5);
    %this.setCollisionActive( true, true );
    %this.setCollisionPhysics(false, false);
    %this.setCollisionCallback(true);
}

There are some other general problems, so I'd recommend that you do some of the tutorials before jumping cliffs without a whole car ;)
#4
11/28/2006 (7:47 pm)
Woah! I guess I should do some more of the tutorials.

I did a few but none of them had object spawning (is there one that I missed?). Some of them (scrollerDemo I think) spawned missiles. If I did something like that where I had an object that I spawned the riffle with would that code work a bit better?
#5
11/28/2006 (7:52 pm)
Well, it seems like you had the basic idea for how to do it just fine. The problem seemed more to be that you just don't have a great grasp of how TGB and Torquescript work yet. Just goin through a few of the tutorials should help you get that stuff. The new docs will be out soonish and should help you go through them quickly.

I think a better way to learn than building your own game straight up is to first modify some of the tutorials or demos. That way, you can see what your changes do and undo things that don't work.

Good luck and don't give up!
#6
11/30/2006 (6:46 pm)
I've been looking though the scrollerDemo tutorial and the scripts and I think I've done it the exact same way but I still can't get it to spawn. Is there a simple spawn function I've missed in the scipt? I've looked through player.cs, playermissile.cs, and ship.cs but I can't see anything wrong.
#7
12/03/2006 (3:27 pm)
I've been looking through the scripts and tutorials and I still can figure it out.

Can someone please help me?
#8
12/04/2006 (12:38 pm)
You don't have a %this, your spawn scripts in the above examples (even Tom's) still contain a %this. Also, you can't bind a command to a namespace unless it is the namespace of a named object. You'll have to bind your move command to the specific instance of the riffle, not to the riffle namespace. The clue is in this console error

Unable to find object: '' attempting to call function 'setPosition'

The object id is empty because you don't have an object named riffle, you have an object in the namespace riffle. Replace that in your bind command with the actual id of a riffle.
#9
12/06/2006 (10:03 am)
Ok, I have tried some of the tutorials to spawn some static sprites, everything works perfect until I use safeDelete the sprite with a mouse event. I can respawn the sprite inmediately after clicked, but that way I can't get what I want.

I have this functions (don't know how to wrap them apart):

function gameBalloon::onAdd(%this, %scenegraph)
{
%this.startY = %this.getPositionY();
%this.schedule(1000, "spawn");//%this.spawn();
%this.setUseMouseEvents( true );
}
function gameBalloon::onWorldLimit(%this, %mode, %limit)
{
if(%limit $= "top")
{
//schedule works as a timer
%this.schedule(2000, "spawn");//%this.spawn();
}
}
function gameBalloon::spawn(%this)
{
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
%this.setPositionX(getRandom(%this.minX, %this.maxX));
%this.setPositionY(%this.startY);
}
function gameBalloon::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
//%this.safeDelete();
%this.spawn();
}

So I have some balloons sprites off of the bottom of stage, as soon as I play my game the balloons start moving upwards until reach top of stage. Once top is reached them are respawned, that's ok, but if I delete them using safeDelete they can't be respawned again... is there a way to delete them but still been on the stage as balloons? I mean, how can I be able to add more and more balloons at certain interval of time, maybe randomly and how can I choose balloon sprites from my library?

I hope this makes some sense...

Thanks
#10
12/06/2006 (12:33 pm)
I would change two things in your code:

Add a function that simply resets a balloon as though it's getting spawned but doesn't set any linearVelocity (use that instead of deleting).

Have a named prototype or model balloon that can be cloned to make more balloons when needed.

Hope that helps!
#11
12/06/2006 (2:18 pm)
Thanks Tom:

So I set linearVelocity to 0 and send the balloon out of sight? I believe it's an option.

But how can I clone a model? Should I use a for statement? you know (%i = 0; %i <= xNumber; %i++)?

The problem is that my programmer background is just ActionScript from Flash... and I think that I want to go too fast with Torque... maybe i should complete the tutorials again, take notes, and read more posts...

Anyway... I will be very open to learn TGB in the next couple of years. Seems pretty cool.

Have a nice day
#12
12/06/2006 (2:34 pm)
Well, really, what to do with the balloons depends on your gameplay. If you wanted to just make a clicked balloon go back to the start, that would be easy - you could just respawn it. Or you could have it go back to the start and schedule it to start moving up after a while.

The real issue, I think, is that you need a balloon manager. All balloons could be added to it when they're loaded or added to the scene, and the manager would use a simset to keep track of them. Then, you could make more balloons using the manager or delete them easily.

If you haven't gone through the new 1.1.3 docs, I'd recommend reading the component tutorials - they're new and go over some useful stuff. Also, skimming though TGB Reference can be very enlightening.

Anyway, people in the community are more than willing to offer plans-of-attack for any problems you have. The tutorials are great, but it's also important to go through some of the example games and read through the scripts, making sure you understand how each system works.

Hope that gets you on your way!
#13
12/06/2006 (5:52 pm)
Thanks again Tom!

Well, the truth is that I have been reading the new docs (I believe well improved since last version -good point for the GG team-) and I understand some stuff but there are some stuff that is not aboarded... I know how to respawn a balloon sprite after clicked or when it reach the world limits. But the script reference is lack of examples for every situation -a logical and comprehensible thing considering all the possibilities-. I think that mole tutorial will help with that respawn thing.

But if I can ask for a quick explanation/where to find a tutorial... how can I build that sprite manager?

I was about to put down a confuse request... but I think that the best is start armed with patiente and open mind from the most basic stuff :)

I will do that for some days/weeks and then, when needed I will come for help.

But if there is a good tutorial about this it will be very welcomed...

I'm on my way!!!

And hey guys at this community: You're doing a great job!
#14
12/07/2006 (6:58 pm)
Isaac, how did you spawn your balloons?

I'm just wondering because I still haven't gotten anything to spawn the way I want.
#15
12/08/2006 (1:21 am)
Richard, did you try the suggestions in my last post before your thread got hi-jacked? I noticed a number of errors in your scripts and pointed them out.
#16
12/08/2006 (8:04 am)
Ooopss! Sorry for the hi-jack!

Richard, I'm new to TGB and I can't nderstand a lot right now... but I guess that if you follow/use the scroller code you will spawn... but I can see that everything lays in the "logic" and conditions you need to do something... use schedule... and I think that is better to add all the properties of a sprite through code than using the dynamic fields...

good luck