Game Development Community

setting up stuff in onLevelLoaded

by rennie moffat · in Torque Game Builder · 12/27/2009 (2:39 pm) · 11 replies

Hi, I am wondering if someone can help me. I have written this onLevelLoaded for my ballClass. The class has three objects in it and I have set up certain things like position and mount for each. I would expect if I call the script and name and call everything appropriately it should work, as in when I run my game, this should be here, and this should be mounted etc.

could somebody PLEASE tell me if I am making some obvious error? Thanks.


function ballClass::onLevelLoaded(%this, %scenegraph)
{
%this.ball = new t2dStaticSprite(ball);
%this.ball.mount(%this.guy, 0, 0, 0, 0, true, true, true);

%this.guy = new t2dStaticSprite(guy);
%this.guy.position(30, 20);

%this.area = new t2dSceneObject(area);
%this.area.position(0, 0);

%this.setUseMouseEvents(true);
}

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
12/27/2009 (4:19 pm)
I havn't done TS in ages, so I am going to take a shot.

Should %this.guy be declared before %this.ball? I don't know if it matters in TS in ages, I am kinda rusty right now..
#2
12/27/2009 (4:45 pm)
I don't think it does. but may well do. I just thought what I was doing was quite obvious. I am trying to see if I can just code stuff vs, using the editor. Tho in talking to someone they suggest to use the editor as much as possible.




#3
12/27/2009 (5:10 pm)
You are creating two t2dSceneStaticSprites, but not assigning them any useful values? Particularly what image they are supposed to use? In addition to that, these sound like your main "player" objects...why would you create them at level load instead of the editor?
#4
12/27/2009 (5:28 pm)
I thought I was by using new t2dstaticsprite.

I thought that would call it?
#5
12/28/2009 (8:16 am)
I think when onLevelLoaded is called, your ball object already exists, so no need to create another one. And %this is your refererence to the ball.

Then you mount %this.guy before it is created at all, so you actually mount a null object.

And the line

%this.guy = new t2dStaticSprite(guy);

creates only a generic sprite with no image at all, and it is not added to the scene graph, so it will never be displayed, even if it had an image.
#6
12/28/2009 (1:51 pm)
hmm, right @ Heiko, re: mounting a null object.

I have been reading. I thought a computer reads code, top down then back up, thinking it could pick and choose from the stack. But what you say makes sense.


re the sprite, how do you know it only creates a generic sprite with no image at all? Does it need the tag image map or would I need something more like this.


onlevelLoaded()
{
%thisguy = neew t2dStaticSprite()
{
scenegraph = %this.scenegraph;
image = guyImageMap;
};
]

etc?

Thanks


#7
12/29/2009 (8:12 am)
Yes, I think this is the minimum you have to define during a creation of a new sprite, if you want to see something at all. Anything else does not make much sense. Oh, and you have to define the frame number within the map.

I usually create the sprite in the editor, and then I look into the created level files(in data/level/mylevel.t2d>). You can actually copy the code from there and paste it into your own code.

Example:

new t2dStaticSprite() {
imageMap = "BackgroundImageMap";
frame = "0";
canSaveDynamicFields = "1";
Position = "7.164 5.522";
size = "327.059 247.025";
Layer = "10";
...
};

As you can see, the scenegraph is missing here; this comes from the special format of the level files, because these objects are defined directly inside the scenegraph object.
#8
12/29/2009 (12:07 pm)
hmm ok, so normally you would have to, in that sequence, define the scenegraph?


so inside the call for a new sprite...


scenegraph = %this.scenegraph;



?



The scenegraph and calling it still slightly confuses me. I know the scenegraph is an object containing all objects on that level(?). so by declaring the scenegraph in the sprites new creation I say it is going into THIS level(?)


Also, you say can copy the code from a level. I will look into it but I was not aware the levels has accessible code.
#9
12/29/2009 (12:56 pm)
You can build a whole level from scratch with TorqueScript alone. Some other interesting files are in the "managed" folders. Almost everything created by the editor is saved in a script file. Exceptions are Tilemaps and particle effects, which are stored in binary files.

The scenegraph is the strutured container holding all scene objects in a tree structure. So everything which is used in a level must be added to it, either with

scenegraph = %scenegrap

during creation, or using

%scenegraph.addToLevel(%object);


(many functions have %scenegraph as an argument, esp. onLevelLoaded)

The scene graph is a key structure in almost all render engines and not Torque-specific, and you can find an article about it on Wikipedia, which should help.

#10
12/29/2009 (1:44 pm)
Cool man thanks so much. I did not realize a scenegraph was universal, not just to Torque, thanks.


I guess calling a
scenegraph = %this.scenegraph

is a bit backwards, since a scenegraph cant belong to anything.


Anyhow, thanks!
#11
12/31/2009 (1:17 pm)
A scene object belongs to a scene graph, and with object.getScenegraph() you can get the scenegraph it belongs to. The line scenegraph=%scenegraph in the definition block adds the defined object to the scenegraph.