Main Menu Loading Levels Tutorial
by Thomas Stankevich · in Torque Game Builder · 05/03/2008 (9:37 am) · 21 replies
Here's a simple tutorial on how to make a main menu and have it load a level.
1. First thing is to set up 2 different levels that you want to access.
2. Open up the GUI Builder by going to Project > GUI Builder or by pressing F10.
3. While in the GUI Builder go to File > New GUI. Name this Menu and press OK.
4. Hit the Toggle Palette button to open up the Control Palette then click and drag GUIButtonCtrl onto the stage.
5. Close the Control Palette and make sure your button is selected by clicking off of it then back on it.
6. On the bottom right-hand side there is a properties palette. Scroll down under GUIButtonBaseCtrl and type "New Game" next to the text property. This will display the text that we want on our button. Then scroll up to GUI Control and next to Command type:
mainMenuGUI.newGame();
This will set what function is called when the button is clicked.
7. That's it for the GUI editor so save your GUI as mainMenu.gui and make sure it's in the game/gui folder.
8. Next go to the game folder and open up the main.cs file. In the InitializeProject function type
exec("~/gui/mainMenuGUI.gui");
This will let the game know that our menu is around.
9. Open up the game.cs file and type
Canvas.pushDialog(mainMenuGUI);
in the startGame function.
Then type in
%level = "game/data/levels/menu.t2d";
this line will always load the menu level first.
Also while you have it open type in
exec("./menuCommands.cs");
10. Make the file menuCommands.cs and past the function
function mainMenuGUI::newGame()
{
%level = "game/data/levels/level.t2d";
Canvas.popDialog(mainMenuGUI);
sceneWindow2D.loadLevel(%level);
}
11. Run Game
1. First thing is to set up 2 different levels that you want to access.
2. Open up the GUI Builder by going to Project > GUI Builder or by pressing F10.
3. While in the GUI Builder go to File > New GUI. Name this Menu and press OK.
4. Hit the Toggle Palette button to open up the Control Palette then click and drag GUIButtonCtrl onto the stage.
5. Close the Control Palette and make sure your button is selected by clicking off of it then back on it.
6. On the bottom right-hand side there is a properties palette. Scroll down under GUIButtonBaseCtrl and type "New Game" next to the text property. This will display the text that we want on our button. Then scroll up to GUI Control and next to Command type:
mainMenuGUI.newGame();
This will set what function is called when the button is clicked.
7. That's it for the GUI editor so save your GUI as mainMenu.gui and make sure it's in the game/gui folder.
8. Next go to the game folder and open up the main.cs file. In the InitializeProject function type
exec("~/gui/mainMenuGUI.gui");
This will let the game know that our menu is around.
9. Open up the game.cs file and type
Canvas.pushDialog(mainMenuGUI);
in the startGame function.
Then type in
%level = "game/data/levels/menu.t2d";
this line will always load the menu level first.
Also while you have it open type in
exec("./menuCommands.cs");
10. Make the file menuCommands.cs and past the function
function mainMenuGUI::newGame()
{
%level = "game/data/levels/level.t2d";
Canvas.popDialog(mainMenuGUI);
sceneWindow2D.loadLevel(%level);
}
11. Run Game
About the author
#2
07/12/2008 (7:24 pm)
Thanks this was a great help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#3
I'm gonna be writing several plans about documentation from now on, and I'm very excited to start spotlighting community members who are going the extra mile to help others out with tutorials.
07/12/2008 (9:20 pm)
@Kevin - There is a new place as well. On top of submitting your tutorials to TDN and GG resources, feel free to send them to me and I'll make sure they get the exposure the deserve =)I'm gonna be writing several plans about documentation from now on, and I'm very excited to start spotlighting community members who are going the extra mile to help others out with tutorials.
#4
07/13/2008 (5:07 am)
Awesome, Michael! I'm working on a pong tutorial that should be done before the end of summer. I'll send it to you when it is finished.
#5
09/18/2008 (5:34 am)
Oh man, this has just saved me lots of work :D Thanks!!
#6
09/18/2008 (7:21 am)
Great little tut!
#7
pushdialog(): Invalid control: mainMenuGUI
This tutorial was exactly what I was looking for but it doesn't seem to be working for me.
Any ideas?
05/31/2009 (9:57 pm)
Is anyone else getting this error:pushdialog(): Invalid control: mainMenuGUI
This tutorial was exactly what I was looking for but it doesn't seem to be working for me.
Any ideas?
#8
I was thinking that called the file name.
Thanks for this amazingly simple tutorial! I looked everywhere for something like this. This is the first resource that worked.
05/31/2009 (10:13 pm)
Ah it looks like you have to change the name of the button control to mainMenuGUI for it to register when you call Canvas.pushDialog(mainMenuGUI);I was thinking that called the file name.
Thanks for this amazingly simple tutorial! I looked everywhere for something like this. This is the first resource that worked.
#9
Using PC, WindowsXP, GB 1.7.4
Console doesnt show any errors
I did almost all tutorials had no problems, but these mini GUI tutorials
dont work at all for me, none of them. Just trying to get a simple GUI main Menu button appear and work (obviously I'm doing smth wrong)
My main.cs
function initializeProject()
{
// Load up the in game gui.
exec("~/gui/mainScreen.gui");
exec("~/gui/mainMenuGUI.gui");
// Exec game scripts.
exec("./gameScripts/game.cs");
// This is where the game starts. Right now, we are just starting the first level. You will
// want to expand this to load up a splash screen followed by a main menu depending on the
// specific needs of your game. Most likely, a menu button will start the actual game, which
// is where startGame should be called from.
startGame( expandFilename($Game::DefaultScene) );
}
function shutdownProject()
{
endGame();
}
function setupKeybinds()
{
new ActionMap(moveMap);
//moveMap.bind("keyboard", "a", "doAction", "Action Description");
}
My game.cs
function startGame(%level)
{
exec("./menuCommands.cs");
Canvas.pushDialog(mainMenuGUI);
%level = "game/data/levels/menu.t2d";
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}
Thank you for any help
11/09/2009 (2:45 pm)
I doesnt work for me at all , I simply get a black screen :( doing it all by the book step by step Using PC, WindowsXP, GB 1.7.4
Console doesnt show any errors
I did almost all tutorials had no problems, but these mini GUI tutorials
dont work at all for me, none of them. Just trying to get a simple GUI main Menu button appear and work (obviously I'm doing smth wrong)
My main.cs
function initializeProject()
{
// Load up the in game gui.
exec("~/gui/mainScreen.gui");
exec("~/gui/mainMenuGUI.gui");
// Exec game scripts.
exec("./gameScripts/game.cs");
// This is where the game starts. Right now, we are just starting the first level. You will
// want to expand this to load up a splash screen followed by a main menu depending on the
// specific needs of your game. Most likely, a menu button will start the actual game, which
// is where startGame should be called from.
startGame( expandFilename($Game::DefaultScene) );
}
function shutdownProject()
{
endGame();
}
function setupKeybinds()
{
new ActionMap(moveMap);
//moveMap.bind("keyboard", "a", "doAction", "Action Description");
}
My game.cs
function startGame(%level)
{
exec("./menuCommands.cs");
Canvas.pushDialog(mainMenuGUI);
%level = "game/data/levels/menu.t2d";
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
new ActionMap(moveMap);
moveMap.push();
$enableDirectInput = true;
activateDirectInput();
enableJoystick();
sceneWindow2D.loadLevel(%level);
}
function endGame()
{
sceneWindow2D.endLevel();
moveMap.pop();
moveMap.delete();
}
Thank you for any help
#10
11/10/2009 (3:03 am)
@Vladislav - Your problem is probably the same that Randy (the post just above you) had. I haven't tried this tutorial, but it looks like the GuiControl itself hasn't been named (which you should name it "mainMenuGUI").
#11
11/10/2009 (4:57 am)
I did and still get a black screen, no errors whatsoever in the console, it says everything loads ok , and yet nothing. What could be wrong
#12
You have to put these lines
exec("./menuCommands.cs");
Canvas.pushDialog(mainMenuGUI);
%level = "game/data/levels/menu.t2d";
Exactly beneath lines
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
Now I got it work only that the New Game Button is streched all over the screen with the text New Game sitting in the middle of the enormous button. What would cause it ?
11/10/2009 (6:27 am)
Ok guys I got some significant progress:You have to put these lines
exec("./menuCommands.cs");
Canvas.pushDialog(mainMenuGUI);
%level = "game/data/levels/menu.t2d";
Exactly beneath lines
Canvas.setContent(mainScreenGui);
Canvas.setCursor(DefaultCursor);
Now I got it work only that the New Game Button is streched all over the screen with the text New Game sitting in the middle of the enormous button. What would cause it ?
#13
Great tut Thomas!
11/10/2009 (1:22 pm)
ok I sorted it out, there was a typo as usual in these kind of things. Cant be too carefull :) Nonetheless thank you all for the help Great tut Thomas!
#14
So.. what would cause the button to be massive? I'm having this problem too. How did you fix yours?
03/30/2010 (11:12 pm)
Hi there all.So.. what would cause the button to be massive? I'm having this problem too. How did you fix yours?
#15
im trying to do this tut
and im new to the TGB
how can I change button control to mainMenuGUI ?
07/19/2011 (4:49 am)
Hiim trying to do this tut
and im new to the TGB
how can I change button control to mainMenuGUI ?
#16
on step 7. That's it for the GUI editor so save your GUI as mainMenu.gui and make sure it's in the game/gui folder.
8. Next go to the game folder and open up the main.cs file. In the InitializeProject function type
exec("~/gui/mainMenuGUI.gui");
in step 7 the name we saved is different than on step 8 which we used to load it
07/20/2011 (2:51 am)
ok I found the problemon step 7. That's it for the GUI editor so save your GUI as mainMenu.gui and make sure it's in the game/gui folder.
8. Next go to the game folder and open up the main.cs file. In the InitializeProject function type
exec("~/gui/mainMenuGUI.gui");
in step 7 the name we saved is different than on step 8 which we used to load it
#17
Error loading level game/data/levels/menu.t2d
09/05/2011 (10:04 pm)
Okay sorry to be a noob. I'm not new to programming but I am new to TGB. At what point should menu.t2d have been created? That definetly doesn't appear in the file path specified. Can anyone point me in the right direction. I searched far and wide for a tutorial like this now I just wanna get it to work :)Error loading level game/data/levels/menu.t2d
#18
09/07/2011 (3:13 pm)
@Jaron - create a new level and save it as menu.t2d. Create your mainMenu in the menu scene and you should be good.
#19
09/09/2011 (6:00 am)
hello everyone, nice taturial, i got it to work, but the button is stretced all over the screen, how can this be solved?
#20
Having the same problem.. "/
11/01/2011 (6:17 am)
So.. what would cause the button to be massive? Having the same problem.. "/
Torque Owner Kevin James