Game Development Community

Level/Map specific scripting

by Jemand den es nicht gibt · in Torque Game Builder · 05/12/2006 (5:20 am) · 5 replies

Hey guys!

When making games with TGE/T2D you've got two kinds of scripting: Game play scripts and very specific per-level scripts(content!). First one is the general game mechanic that is the same throughout the whole game, for example KI, collision response and so on. The second one though is one layer ABOVE. For example, a trigger that spawns some enemies or starts a scripted sequence.

Now how would you go and seperate those two in a nice and clean fashion? The only idea I could come up with was to create an extra script for every level or add the needed functions into the level file. Or even create a content script package for every level.

Has anybody ever had to do something similiar before? :)

#1
05/12/2006 (7:41 am)
Well, the cleanest way would really be if every level file contained a package that automatically gets activated when the level is loaded and deactivated when the level is unloaded. The problem is, everytime you save your level in the editor, your scripts will be overwritten.

A two file approach is a little tedious but would work fine, I guess.

In GUI files you can add script after the
//--- OBJECT WRITE END ---
tag. If that was true level files too that would be a very slick solution. It isn't unfortunatly.
#2
05/12/2006 (7:44 am)
Yeah, but that would be a nifty thing to add. ;)
#3
05/12/2006 (8:35 am)
Perhaps I am reading this wrong or simply not understanding, but aren't these things already seperated? You have your level scripts with content like triggers (.t2d files) and then gameplay scripts (.cs files). If you want level specific functions without having a seperate script for each file, you could give the objects in your level a different class namespace - playerLevel1::doThis(), playerLevel2::doThis(), etc.
#4
05/12/2006 (8:36 am)
And here you go:

Open tools/levelEditor/scripts/levelManagement.ed.cs and exchange this code at about line 150
%fo = new FileObject();
      %fo.openForWrite($currentLevelFile);
      %fo.writeLine(%beginMessage);
      %fo.writeObject(%scenegraph, "%levelContent = ");
      %fo.writeLine(%endMessage);
      %fo.close();
      %fo.delete();
with this code:
%beginMessage = "//--- LEVEL WRITE BEGIN ---";
   %endMessage = "//--- LEVEL WRITE END ---";

   if( !isFile( $currentLevelFile ) )
   {
      %fo = new FileObject();
      %fo.openForWrite($currentLevelFile);
      %fo.writeLine(%beginMessage);
      %fo.writeObject(%scenegraph, "%levelContent = ");
      %fo.writeLine(%endMessage);
      %fo.close();
      %fo.delete();
   }
   else
   {
      %tempFileName = $currentLevelFile @ ".tmp";
      while( isFile( %tempFileName ) )
         %tempFileName = %tempFileName @ ".tmp";
      
      pathCopy( $currentLevelFile, %tempFileName );
      
      %tempFile = new FileObject();
      %tempFile.openForRead( %tempFileName );
      
      %newFile = new FileObject();
      %newFile.openForWrite($currentLevelFile);
      
      while( !%tempFile.isEOF() )
      {
         %buffer = %tempFile.readLine();
         if( %buffer $= %beginMessage )
            break;
         %newFile.writeLine(%buffer);         
      }
      
      %newFile.writeLine(%beginMessage);
      %newFile.writeObject(%scenegraph, "%levelContent = ");
      %newFile.writeLine(%endMessage);
      
      while(!%tempFile.isEOF())
      {
         %buffer = %tempFile.readLine();
         if( %buffer $= %endMessage )
            break;
      }
      
      while(!%tempFile.isEOF())
      {
         %newFile.writeLine( %tempFile.readLine() );
      }
      
      %tempFile.close();
      %tempFile.delete();
      
      fileDelete( %tempFileName );
      
      %newFile.close();
      %newFile.delete();
   }

That should do the trick. From my little testing it works fine. Only for existing levels, you would have to add the //--- LEVEL WRITE BEGIN --- and //--- LEVEL WRITE END --- tags yourself. If you don't do that, the level code will be duplicated. Otherwise you can do what you want outside the tags.

I'll post a link to this in the suggestions forum :)

-Michael
#5
05/12/2006 (9:35 am)
Please do Michael. =)