Game Development Community

[resolved] Changing the Default scene within TGB

by Hitesh Patel · in iTorque 2D · 10/15/2009 (12:45 pm) · 10 replies

I want to change the default scene from within TGB script. I know you can manually change it in commonConfig.xml but is it possible thorugh script in TGB. My aim is to start at the level user exits. I have tried to use the following but doesnt seem to work. Any clues.

%xml = new ScriptObject() { class = "XML";};
%projectfile = "common/commonConfig.xml";


if( %xml.beginWrite( %projectFile ) ) 
 { 
  if (%xml.readClassBegin("TorqueGameConfiguration"))
  {
    %xml.writeField ("DefaultScene","~/data/levels/Level2.t2d");
    %xml.endWrite();
  }
 }



#1
10/15/2009 (1:02 pm)
Hitesh,

Its been a while since I looked at iTGB, so unless they changed it you cannot use torque stander file IO. Look at this resource to learn how to save and read stuff on the iphone.

www.garagegames.com/community/resources/view/17153

hope it helps
#2
10/15/2009 (2:32 pm)
Hitesh :)

The scenegraph of TGB normally runs in the SceneWindow object. For example :

new t2dSceneWindow(sceneWindow2D) {

This should exist already, if not let me know and i can show where else to look. For the above , the object it creates has a function called loadLevel, which you can specify a .t2d scene file to load.

You should see the following :

function t2dSceneWindow::loadLevel(%sceneWindow, %levelFile)

In the file : common/gameScripts/levelManagement.cs

This should get you on your way!
#3
10/15/2009 (4:13 pm)
Christopher and Sven,

Thanks for your replies.

Christopher, thanks for the link to the article. It will be really helpful. There is still something I am not sure how to do. It mentions “All you then need to do is integrate the two functions into your TorqueScript and create a single string of data that you send in to save, and parse out when you retrieve it!”.

Sven thanks for pointing out the loadlevel function. At present I am using that to load the next level when a level finishes. Just to make things clear here is an example of what I am looking for.

1. The user starts the game and it starts at level 1.
2. User finishes level 1 and it loads level2 using loadlevel and then level 3 and so on.
3. The user decides to exit at level 3.
4. When the user starts the game again, I want it to start at level3 .

This was the reason I was trying to update the xml file. Although the code above might be confusing. The string "~/data/levels/Level2.t2d" was just for test purpose to see if updates the xml file.

Actually the way I am going to do this is by passing a variable. I have added a dynamic field (nextlevel) in each level which states what the next level will be. Each level belongs to a class (alllevels). In my alllevels.cs file I do the following:

function alllevels::onLevelLoaded(%this)
{
   $CurrentLevel=%this;
}

Then I assign a variable and use it as follows:

%nextlevelstring = $Currentlevel.nextlevel

Then use this variable

%xml.writeField ("DefaultScene",%nextlevelstring);

Sorry for the long reply, I hope its clear and I am not doing the whole thing totally the wrong way.
#4
10/15/2009 (6:23 pm)
Hitesh,

What he is talking about, is getting everything that you need to save in your game such as player lives, hit points, score, level, etc. Put it into one large string then save to the iPhone. But what I would recommend is using the code provided by Stefan Eilers at the bottom of Calabrese resource. Note if you are using iTGB 1.3 this code should already be in it.

Here is a demo of how you could use it.

When the program starts, check if your level key has been saved
// This is the key used to save and read data form the iphone, declare it somewhere like main.cs
$Game::Key::Level = "PlayerLastLoadedLevel";	

if( loadGameDataFromDevice($Game::Key::Level) )
{
	// Do something or nothing here, its up to you
	echo("We have save game level data so we loaded the game at least once!");
}
else
{
	// We know this is the first time the application had loaded store data so we can grab it when the app is running
	echo("This is the first time we loaded the game!");
	
	// save the level from xml, so we can load it correctly
	saveGameDataToDevice(expandFilename($Game::DefaultScene), $Game::Key::Level); 
}

Now when ever your about to load a level you should call
%level = loadGameDataFromDevice($Game::Key::Level);
sceneWindow2D.loadLevel(%level);

When you just changed a level such as onLevelLoad, you should save the new level
%theCurrentLevel = "~data/Level2.t2d";
saveGameDataToDevice(%theCurrentLevel, $Game::Key::Level);

NOTE: I haven't tested this so it mostly likely have bugs.

I am not sure if I am being clear here, so let me know if you have any questions.
#5
10/15/2009 (6:57 pm)
Edit : the below is now for reference. If you implement the resource above, you have the the iphone "registry" at hand :)

As suggested before in a previous thread, its not advisable to be mucking around with the XML files TGB generates. The reason being - If you open the TBG app for any reason, the XML will be overwritten (and many other obvious reasons).

The way you want to go about it, is to just use a variable thats stored somewhere else. There are two options,

1) use the TGB system to write a file - BE CAREFUL! You CANNOT change files within the application bundle once the application is running. FULLSTOP. But there is an application documents path that apple offers to save files to - which works well.

2) use the iPhone "registry" which allows saving values to the users profile on the device.

Even if you do manage to change the files on a pc, the functionality wont work on the iPhone itself, as you cannot change your application "sandbox". The OS only allows accessing files in the sandbox, and writing files to the application documents directory.

For example :

%fileHandle = new FileObject();
%fileLocation = $Game::FileLocation;  

if(%fileHandle.openForWrite(%fileLocation))
{
   //Write some data here
  %fileHandle.writeLine(%quitLevel);

   //Clean up
  %fileHandle.close();
}
   //Free memory
  %fileHandle.delete();

The above sample shows how simple it can be. The FileObject also supports binary files, and reading files, etc etc. :)
#6
10/19/2009 (2:00 pm)
Thanks guys, I have successfully saved data to the iphone using the registry key solutions.
#7
10/19/2009 (2:17 pm)
Awesome. Glad that it worked out!
#8
10/21/2009 (3:13 pm)
Just one last question. How do i save more then one value. I tried using a different key i.e $Game::Key::LevelsCompleted. But it saved the same data as $Game::Key::LastPlayedLevel.
#9
10/21/2009 (3:51 pm)
I have three suggestion, they are really basic but lets make sure the issues are not over looked.

First, thing that came to mind are your values for $Game::Key::LevelsCompleted and $Game::Key::LastPlayedLevel are the same, or somewhere in code get set to be the same.

Second, make sure the data your saving are in fact different.

Third try clearing out your registry data. So delete the project from the simulator and/or iphone and try rebuilding it, or go to ~(user home)/ Library/Application Support/iPhone Simulator/User/Applications/(The number that apple has assigned to your application)/Library/Preferences and delete the plist files stored there.

Try those out and then see if you still have a problem.
#10
10/22/2009 (11:51 am)
Sorted, I had made a mistake in my code. Just needed to look at thoroughly after a good rest :). Thanks