Starting in script
by Daniel McNeese · in Torque Game Builder · 06/19/2007 (12:57 am) · 19 replies
If I've already placed an object in the level via the editor, I know how to make a script have it do something when the level starts. Just use a function like "playerFish::onLevelLoaded" to have something already in the level take the actions I want.
But what if I want to bypass the level editor and use scripts to create objects in an empty level, or even create a level from scratch? How do I go about that?
But what if I want to bypass the level editor and use scripts to create objects in an empty level, or even create a level from scratch? How do I go about that?
About the author
#2
Thanks for the advice, but it isn't that simple. I intend for my game to have a level editor for the users, and I can't just give them TGB's. So I have to figure out how to make my own.
I saw that part, but that has the player's ship - a pre-existing object in the level - create the missile. I need to create objects independently. I've tried duplicating that code (with adjustments to suit my needs), and it didn't work. Here's my code:
%this.testObject = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = player;
imageMap = "playerImageMap";
%this.setPositionX(1);
%this.setPositionY(1);
}
How would I alter this to make it work independently? And yes, I have this script invoked with an exec() command and the imagemap in question does exist.
06/19/2007 (1:45 am)
Quote:To create levels I sugest you use the level editor
Thanks for the advice, but it isn't that simple. I intend for my game to have a level editor for the users, and I can't just give them TGB's. So I have to figure out how to make my own.
Quote:Thats an example of createing a missile used in the shooter tutorial on TDN
I saw that part, but that has the player's ship - a pre-existing object in the level - create the missile. I need to create objects independently. I've tried duplicating that code (with adjustments to suit my needs), and it didn't work. Here's my code:
%this.testObject = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
class = player;
imageMap = "playerImageMap";
%this.setPositionX(1);
%this.setPositionY(1);
}
How would I alter this to make it work independently? And yes, I have this script invoked with an exec() command and the imagemap in question does exist.
#3
06/19/2007 (3:19 am)
Apologies for all the duplicate posts. I just figured out what was causing them and deleted them.
#4
As for the level builder I understand now. I did something similar a while back with a maze game but a hard-drive failure means that code has been lost. Tiles can be changed quite easily on the fly and saved so thats not a problem. As for other sprites etc it depends very much on what you want the player to be able to do. For example you could write somthing very similar to the level builder (GG did it so it is possible ;) though you may want to limit them to what sprites they can use, where they can place objects and so on. Depending on what you want to do you can make it work by saving everything the player does to his level as a .cs script and run it when a level loads, createing all the objects and so on.
06/19/2007 (5:13 am)
Any errors in the console? In the code above you haven't given the new object a position (youve called setPostion on %this). Try this function:function createTestObject()
{
%obj = new t2dStaticSprite()
{
scenegraph = t2dscenegraph; // <- whatever the main scenegraph is called...I can't remember its name
class = player;
imageMap = "playerImageMap";
}
%obj.setPositionX(1);
%obj.setPositionY(1);
}As for the level builder I understand now. I did something similar a while back with a maze game but a hard-drive failure means that code has been lost. Tiles can be changed quite easily on the fly and saved so thats not a problem. As for other sprites etc it depends very much on what you want the player to be able to do. For example you could write somthing very similar to the level builder (GG did it so it is possible ;) though you may want to limit them to what sprites they can use, where they can place objects and so on. Depending on what you want to do you can make it work by saving everything the player does to his level as a .cs script and run it when a level loads, createing all the objects and so on.
#5
No, I just get an empty level. Here's my new version of the code (also doing nothing):
I already partially implemented an editor in Game Maker before deciding to switch to TGB, so I pretty much know how - in a pseudocode sense - to make it work. But I don't know how to implement specific actions in TGB code yet.
06/19/2007 (7:52 pm)
Quote:Any errors in the console?
No, I just get an empty level. Here's my new version of the code (also doing nothing):
Quote:
function createTestObject()
{
%obj = new t2dStaticSprite()
{
class = player;
imageMap = playerImageMap;
}
%obj.setPositionX(1);
%obj.setPositionY(1);
}
createTestObject();
Quote:For example you could write somthing very similar to the level builder (GG did it so it is possible ;)
I already partially implemented an editor in Game Maker before deciding to switch to TGB, so I pretty much know how - in a pseudocode sense - to make it work. But I don't know how to implement specific actions in TGB code yet.
#6
You didn't assign yours a scenegraph.
06/20/2007 (6:48 am)
Ok this code works:function createTestObject()
{
%obj = new t2dStaticSprite()
{
scenegraph = sceneWindow2D.getSceneGraph();
class = player;
imageMap = "playerImageMap";
};
%obj.setPositionX(1);
%obj.setPositionY(1);
%obj.setSize(10);
}You didn't assign yours a scenegraph.
#7
Anyway, I just tried your code exactly as written (cut-&-paste job), adding only a "createTestObject();" command to actually call the function. I'm afraid I got nothing.
You tested this function on your machine and it worked? Exactly how and where did you call it?
06/25/2007 (6:27 pm)
Sorry it took so long to respond, but I was dealing with some other problems.Anyway, I just tried your code exactly as written (cut-&-paste job), adding only a "createTestObject();" command to actually call the function. I'm afraid I got nothing.
You tested this function on your machine and it worked? Exactly how and where did you call it?
#8
06/26/2007 (5:22 am)
That function would only work if you had an image named 'playerImageMap' loaded into the editor.
#9
06/26/2007 (7:04 am)
I did.
#10
06/26/2007 (5:29 pm)
Where are you calling createTestObject, and have you tried running it directly through the console?
#11
As for the console, I'm not sure what you're refering to.
06/27/2007 (1:42 am)
I tried adding a "createTestObject();" line right after defining the function. When that didn't work I tried adding it right after the exec() command in game.cs that invoked the script. Neither worked.As for the console, I'm not sure what you're refering to.
#12
That code pretty much looks good, except for two things:
1. Try: %obj.setPosition(0, 0);
2. The default size is 10 so you don't have to call "setSize(10)" and even so, there are 2 arguments that you must pass in: %width then %height.
You may not be able to see the image, if your camera is off center or if your playerImage is not a valid image.
EDIT: You can access the console by pressing the tilde key, right below the escape key. (On my keyboard anyway) The console is very useful, I'm guessing that it is giving you an error report, but you just haven't seen it. You can also enter commands in the console. It's pretty rad. ;D
06/27/2007 (2:50 pm)
function createTestObject()
{
%obj = new t2dStaticSprite()
{
scenegraph = sceneWindow2D.getSceneGraph();
class = player;
imageMap = "playerImageMap";
};
%obj.setPositionX(1);
%obj.setPositionY(1);
%obj.setSize(10);
}That code pretty much looks good, except for two things:
1. Try: %obj.setPosition(0, 0);
2. The default size is 10 so you don't have to call "setSize(10)" and even so, there are 2 arguments that you must pass in: %width then %height.
You may not be able to see the image, if your camera is off center or if your playerImage is not a valid image.
EDIT: You can access the console by pressing the tilde key, right below the escape key. (On my keyboard anyway) The console is very useful, I'm guessing that it is giving you an error report, but you just haven't seen it. You can also enter commands in the console. It's pretty rad. ;D
#13
Ah, okay. Actually, it's the other way around. I learned about it yesterday, I just didn't know what it was called. Thanks.
As for getting the function to work... It works perfectly when I call it from the console. But it still ignores calls from scripts, which is where I actually need it work.
Here's the script:
06/28/2007 (1:42 am)
Quote:EDIT: You can access the console by pressing the tilde key, right below the escape key. (On my keyboard anyway) The console is very useful, I'm guessing that it is giving you an error report, but you just haven't seen it.
Ah, okay. Actually, it's the other way around. I learned about it yesterday, I just didn't know what it was called. Thanks.
As for getting the function to work... It works perfectly when I call it from the console. But it still ignores calls from scripts, which is where I actually need it work.
Here's the script:
function createTestObject()
{
%obj = new t2dStaticSprite()
{
scenegraph = sceneWindow2D.getSceneGraph();
class = player;
imageMap = "playerImageMap";
};
%obj.setPosition(0, 0);
}
createTestObject();
#14
06/28/2007 (7:34 am)
Hmmm. Is that function in a different file than the game.cs? If so, then you must execute that file as well:exec("file path");
#15
And before anyone asks, all my scripts are in the same folder.
06/28/2007 (7:51 am)
Yes, there's an exec() command. Here's my game.cs://---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// startGame
// All game logic should be set up here. This will be called by the level builder when you
// select "Run Game" or by the startup process of your game to load the first level.
//---------------------------------------------------------------------------------------------
function startGame(%level)
{
exec("./player.cs");
exec("./creationTest.cs");
createTestObject();
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
//---------------------------------------------------------------------------------------------
// endGame
// Game cleanup should be done here.
//---------------------------------------------------------------------------------------------
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}And before anyone asks, all my scripts are in the same folder.
#16
You were missing the semicolon after the creation statement (remember that it's one line even though us humans like to split it into multiple) in the first example, and so was Tom Perry in his second example (very common mistake).
In any case, if you post all the code (so, add the creationTest file) I'll figure out what's wrong.
It seems most likely that it's a simple script error (semicolons or wrong scenegraph). Those will show up in the console when it's compiled too.
06/29/2007 (7:50 pm)
Check out the Basic Debugging section of the docs (in component tutorials I think) - it goes over using echo as a tool. It probably goes over the console too - but you already got that. So, I'd throw some echoes in there and figure out if the createTestObject function is getting run.You were missing the semicolon after the creation statement (remember that it's one line even though us humans like to split it into multiple) in the first example, and so was Tom Perry in his second example (very common mistake).
In any case, if you post all the code (so, add the creationTest file) I'll figure out what's wrong.
It seems most likely that it's a simple script error (semicolons or wrong scenegraph). Those will show up in the console when it's compiled too.
#17
Put
06/30/2007 (12:14 am)
It looks to me like you are calling the create function before scenewindow2d is created:Put
createTestObject();after
Canvas.setContent(mainScreenGui);so that it looks like this
Canvas.setContent(mainScreenGui); Canvas.setCursor(DefaultCursor); createTestObject();and it should work right off the bat.
#18
That was it. I've got it working now. Thanks.
Tom:
I already did. Look 5 posts up from this one.
It's okay, though. Like I said, Luke nailed the problem. Thanks anyway.
07/01/2007 (12:38 pm)
Luke:Quote:It looks to me like you are calling the create function before scenewindow2d is created:
That was it. I've got it working now. Thanks.
Tom:
Quote:In any case, if you post all the code (so, add the creationTest file)
I already did. Look 5 posts up from this one.
It's okay, though. Like I said, Luke nailed the problem. Thanks anyway.
#19
Oh well ;)
07/01/2007 (7:38 pm)
Haha - yeah. I smacked my forehead too when I saw that you already had - and when Luke nailed it.Oh well ;)
Torque 3D Owner Tom Perry
To create levels I sugest you use the level editor, its much easier to visualise! To create objects in script however is easily done.
%this.playerMissile = new t2dStaticSprite() { scenegraph = %this.scenegraph; class = playerMissile; missileSpeed=%this.missileSpeed; player = %this; };Thats an example of createing a missile used in the shooter tutorial on TDN. There are probably some other examples lying around also.
If your really keen on making a level from scratch try looking at the start-up functions for the level editor ;)