Game Development Community

Gui N00B, need help.

by Mark Grooby · in Torque Game Builder · 09/03/2006 (6:32 am) · 8 replies

So, I have made buttons called 1 Player, 2 Players, and Quit. for the quit button, the command is quit();
I just want to know, what command would I use to change the level I am on. I have twoplayer.t2d and oneplayer.t2d and title.t2d. The game starts on title.t2d. How do I make it, that when i click 1 Player, it changes to oneplayer.t2d etc,. I'm new to torque TGB and need help.
Thnx in advance.

-Mark Grooby

#1
09/03/2006 (1:29 pm)
@Mark, for simplicity, it would probably be best if you wrapped it into a function.

The command for '1 Player' could be either 'startOnePlayer();' or 'newGame(1);'
The command for '2 Player' could be either 'startTwoPlayer();' or 'newGame(2);'

In your startgame, you'd have a sceneWindow2D.loadLevel() call -- similiar to the one that is in the game.cs

// default game.cs
function startGame(%level)
{
   exec("~/gui/BlogReaderGui.gui");
   exec("./blog.cs");
   // Set The GUI.
   Canvas.setContent(mainScreenGui);
   Canvas.pushDialog(BlogReaderGui);
   Canvas.popDialog(LBPlayLevelGui);
   Canvas.setCursor(DefaultCursor);
   
   moveMap.push();
   
   if( isFile( %level ) || isFile( %level @ ".dso"))
      sceneWindow2D.loadLevel(%level);
}

Rather then the '%level' parameter, and the if statement you'd have something like this:

function newGame(%players)
{
  if(%players == 2)
  {
    sceneWindow2D.loadLevel("~/data/levels/twoplayer.t2d");
  } else
  {
    sceneWindow2D.loadLevel("~/data/levels/oneplayer.t2d");
  }
}

In your game.cs, you'd replace the 'loadLevel(%level)' with 'loadLevel('~/data/levels/title.t2d')' if you intend for that to load first ... however, if my understanding is correct -- your title.t2d is not necessary, as you'd use a GUI for it instead ...
#2
09/03/2006 (11:23 pm)
Thanks alot. Another question... instead of putting the script in .cs files, could i just make the command for the button
sceneWindow2D.loadLevel("~/data/levels/oneplayer.t2d");
would that work? Thanx for the help.

-Mark Grooby

EDIT: I would just try it, but I'm using somebody else computer right now, and don't have TGB or my game with me.
#3
09/04/2006 (12:24 am)
Also, my title is necessary, because I want the title to be interactive, so players can get the hang of the movement before actually starting the game.
#4
09/04/2006 (6:33 am)
@Mark, you could, but it's probably not the best of idea's ...
#5
11/23/2006 (12:49 pm)
I know this is an old topic, but wanted to expand on David's reply for future readers. (And if David had a different reason than this, we'll never know, because he didn't clarify his answer. =;)

While you can put code inside the Command area of a button, in general it's better to *not* tie code like that into a GUI control because if you end up wanting to do more than one thing when the button is pressed, now you're kind of stuck. By calling a function in a script file, it's much easier to jump in and tweak something rather than modifying the GUI file.

Note: I'm a newbie TGB user but a *long-time* programmer, so my answer was created from programming experience in general, not TGB-specific experience. Once you paint yourself into a corner a few times you start to realize it's usually better to keep things as accessible and tweakable as possible.

I hope that helps.

Jay Jennings
#6
11/23/2006 (5:18 pm)
@Jay,

Not to sound snippy, but at what point did I say to tie code directly into the command? My suggestion to Mark was the same as yours, put the code for the functionality into a function, then call the function from the Command of the button --

The concept I suggested, is a general programming functionality -- the newGame(1) or startOnePlayer() would be the equivalent of setting that function as the callback handler for the button-press in any other GUI frameworks -- granted, you could use a different naming convention, such as "btnOnePlayer_Click();", but I dislike that naming convention for TorqueScript as most of your code is geared toward a very specific purpose -- including buttons :)
#7
11/26/2006 (1:00 am)
Sorry, David, I didn't make myself clear -- yes, I assumed you were saying to NOT tie code directly into the command, but you didn't come right out and say it. So for the sake of new people who might come along later, I was clarifying that point.

Mark says: Can I just include the following code in my button?

David says: You could, but not a good idea.

In my little pea-brain I heard some newbie come along, read that, and say, "Why isn't that a good idea?" I was just trying to help that non-existent (at this time) newbie to understand the reasoning behind your answer (as I understood the reasoning to be). =:)

Jay Jennings
#8
11/26/2006 (1:02 am)
@Jay -- not a problem, my follow-up was probably not even necessary -- was a long long day ... ;)