Game Development Community

Script made objects not appearing

by James Higgins · in Torque Game Builder · 08/24/2007 (6:02 pm) · 2 replies

I've made up something that, from what I understand, should be creating a few objects, but they never do.

function spawner::onLevelLoaded(%this, %scenegraph)
{
%this.block = new t2dStaticSprite()
{
scenegraph = SceneWindow2D.getScenegraph();
class = block;
blockCreator = %this;
setWorldLimit( clamp, getWords(%this.getWorldLimit(), 1, 4) );
setPosition(%this.getPosition());
setImageMap(redImageMap);
setSize(16, 16);
};
}

Can someone please tell me what I'm doing wrong? I have the classes typed in on the objects that should spawn them, I have the exec code for the script in the main file, etc.

Thanks

#1
08/24/2007 (6:43 pm)
You are most likely missing a couple of additional method calls, but a root issue is that you are making multiple method calls on your object within the parameters to the new operator--which is not good practice. The Torquescript parser will not complain (it's in fact a very low level bug in the parser, and extremely non-trivial to fix), but you shouldn't ever make method calls to an object within the { } body of the new operator.

Try the following:

function spawner::onLevelLoaded(%this, %scenegraph)
{
  %this.block = new t2dStaticSprite() 
  {
    scenegraph = %scenegraph; // ::onLevelLoaded provides the scenegraph as a param, so use it :)
    class = block;
    imageMap = redImageMap;
  };
  %this.block.blockCreator = %this;
  %this.block.setWorldLimit( clamp, getWords(%this.getWorldLimit(), 1, 4) );
  %this.block.setPosition(%this.getPosition() );
  %this.setSize(16, 16);
}

Note: There may very well be another parameter or two that you should set to make sure the block is configured properly (collision comes to mind, for example), but the above code demonstrates what should be within the { }; portion of your new statement, and what shouldn't.
#2
08/24/2007 (7:24 pm)
Thank you very much, that fixed most of the problem.

I thought I could pin it down, but I was wrong. For some reason, only the first block appears where it is supposed to, the others appear near the middle of the screen and flash. Thanks ahead of time for any help.
----
Fixed it, apparently it had something to do with setting the world limits.