Click or drag to resize
Source Code Editor

This window allows you to edit most of the code that is used in compiling an executable file representing the game. The only code that can't be edited here is code that is generated based on data provided to the IDE such as sprite states, tile categories and the layer tiles. Most of the real work, however, is performed by the code files listed in the "Source Code" folder of the project tree, which can all be edited in the code editor.

Source Code Editor

Each file listed in the project tree's source code folder is in fact stored within the project (embedded in the project's SGDK2 file). That means that you can customize and change this code as much as you like and it will always apply to that project and only that project.

There are two primary kinds of source code files that can be listed in the Source Code folder: built-in code and custom code. The term "built-in" does not mean that it can't be changed, but rather that it is provided by default, and usually is required in some form by the hidden code that is generated when the project compiles. When possible it's best to implement/provide customizations in the form of a custom code object rather than by changing the code in a built-in code file because it's more likely that a custom code object can easily be ported from one project to another. Consider, for example, two independent customizations that support some customized sprite behavior. If they are both implemented as rules coded right into SpriteBase.cs, it will be slightly more difficult for a project designer to import both these customizations. The project can import one customized version of SpriteBase.cs, but then when the other version of SpriteBase.cs is imported, it will overwrite the first customized version. But if each customization is implemented in its own custom code file with its own name, then both those files can be imported independently with less likelihood of interference.

Code files can be arranged hierarchically in the Source Code tree to represent dependencies. For example, sound effects are implemented as custom-code objects that have a dependence on a file called "fmodbase.cs", which in turn has a dependence on "fmod.cs", which in turn has a dependence on "fmodex.dll". This dependence is remembered within the exported files, so when a sound effect that depends on fmodbase.cs is imported, that file is also imported as are the others. To create your own dependencies, just make sure to select the right file within the Source Code tree before adding a new file. The new file will be created beneath the selected file, indicating that the new file relies on features provided by the "parent".

Custom code objects can further be divided into two more types: External binary references and Embedded objects. If you create a code object that ends with ".dll" SGDK2 will assume that you are referring to an external DLL file that resides in the directory where the SGDK2 project file resides or where SGDK2IDE.exe resides. When the project is compiled, the specified DLL filename will be copied from one of those locations (assuming it exists) into the output folder where the project executable resides. This is to support 3rd-party libraries such as FMOD that you may want to integrate into your project. Files that do not end with ".dll" must end with ".cs" indicating that they are C# ("c-sharp") code files. ".cs" is the default extention if none is specified.

Finally, the embedded objects can have two components: plain text source code, and embedded binary data. Sound effects are a good example of custom objects that utilize both of these. There is a small bit of code that handles the particular sound effect, linking it to the rest of the project, and then there is the binary data which usually comes from an MP3, OGG or MOD file (any type supported by FMOD may be used). The binary data is managed with menu commands that are available when the code editor is editing a custom code object.

File Menu

The file menu contains commands related to the code editor because the code editor works with objects that are like files embedded in the project.

Rename Custom Code Object

This command is available only for custom code objects, not standard code. Rename the code object/file currently being edited. The name must follow the rules for file naming because during the process of compiling the project, a file by this name will be created. The name must also be unique among all code files in the project.

Specify Folder Name

Specify a folder name for the code object currently being edited. The folder may be delimited by backslashes to nest the object within multiple levels of folders. The folder will become the parent of the code object in the project tree only. It does not affect other uses of the code object.

Edit Menu

This menu provides common commands for text editing

Undo

Undo the last typing, deleting or text replacement done in the text editor. Multi-level undo is supported, so repeated undo commands will continue to undo older changes. There is a limit of about 1 MB, so if more than 1 MB worth of changes have been made, any changes before 1 MB will be forgotten.

Redo

Redo the last operation that was undone. Multi-level redo is supported, so if there have been multiple undo commands executed, they can all be re-done. Once the text is manually changed, however, no more redo commands can be executed.

Find...

Displays a window with a simple interface supporting common features for finding text within the code.

Find Next

Find the next occurrence of the last text that was searched with the "Find" command.

Replace

Displays a window with a simple interface supporting common features for finding and replacing occurrences of specific text.

Go To Line...

Jump to a specific line number in the code. This is useful if an error occurred while compiling the project. It will jump the the beginning of the specified line number. (You can also see the current line number on the status bar.)

Embedded Data
This menu is only available when the code editor is editing a custom code object. Built-in code does not have any embedded data, or rather, the data associated with built-in code is generated by data provided to the IDE.
Load From File...

Use this command to load binary data from an external file and embed it as the binary data associated with the current code object. For example, to embed a sound effect, you could select an OGG file to embed.

Edit As Text...

If the binary data is in fact just a big chunk of text (perhaps an XML document containing dialog or some plain-text data used by your code) you can use this to edit the data in a text editor. Do not edit binary data (sounds etc.) as text.

Clear (Size)

This menu item displays the size of the embedded data and allows it to be deleted from the project.

Play with FMOD

Operating on the assumption that much of the binary data embedded represents sounds, this command will try to play the binary data as a sound using the FMOD library

Export to File

If data has been embedded into an SGDK2 file, and you no longer have the original file from which it was embedded, you can use this command to write the binary data to a file. This might be handy if you want to do more editing on the data in an external editor. The catch is that you have to know and remember the type of the file because the export feature does not know what extension to append to it. You have to specify the correct extension manually when exporting the file. The exported file retains no connection to the SGDK2 file, and must be re-embedded after editing.

Sound Effects Sample

Below is some sample code that resembles the code that most sound effects or pieces of music use. It relies on fmodbase.cs and its dependencies, so it cannot be used on its own, but serves as a good template for adding new sounds (alternatively, you can import any sound effect and just change the file name and the class name in the code, and replace the binary data with your own). Notice the code for "Volume". A different number between 0 and 1 can be specified here to affect the playback volume of the sound.

MySound
namespace CustomObjects
{
   public class MySound : FMODBase
   {
      private static MySound m_instance = null;

      public MySound()
      {
      }

      public static MySound Value
      {
         get
         {
            if (m_instance == null)
               m_instance = new MySound();
            return m_instance;
         }
      }

      protected override float Volume
      {
         get
         {
            return 0.75f;
         }
      }
   }
}
Accessing binary data sample

Sound effects store their data in a binary resource associated with the source code object. You can see how this data is accessed by looking at the Load function in fmodbase.cs, which is referenced by FMOD-based sounds in the sound library. For a simpler and more direct example of accessing the binary data associated with a source code object, see the code below. It demonstrates the process of loading the binary data into a byte array, converting it to plain text, and providing a function to output the text to the project's debug output.

Binary Data Sample Code
namespace CustomObjects
{
   public class Test
   {
      private string contentText = null;
      private static Test instance = new Test();

      public Test()
      {
         System.IO.Stream myContentStream =
            GetType().Assembly.GetManifestResourceStream(
            GetType().Name + ".bin");
         byte[] contentBytes = new byte[myContentStream.Length];
         myContentStream.Read(contentBytes, 0, contentBytes.Length);
         contentText = System.Text.Encoding.UTF8.GetString(contentBytes);
      }

      [System.ComponentModel.Description("Add content to debug output")]
      public static void DebugContent([System.ComponentModel.Editor("CustomObject", "UITypeEditor")] Test source)
      {
         Project.GameWindow.debugText.WriteLine(source.contentText);
      }

      public static Test DefaultInstance
      {
         get
         {
            return instance;
         }
      }
   }
}

Notice a few things:

  • There is an attribute on the "source" parameter to the DebugContent function:

    [System.ComponentModel.Editor("CustomObject", "UITypeEditor")]

    This attribute informs the IDE to list public static functions and properties from the CustomObject namespace that return the appropriate type of object to be passed in to this parameter.

  • The DefaultInstance is an example of just such a property, and will be listed in the dropdown list for this parameter.
  • In order for DebugContent to be listed as an available function, it must be public, static, and have the System.ComponentModel.Description attribute applied.
  • In order for the DefaultInstance to be available in the dropdown list for a parameter, the parameter type must match the return type of the property ("Test" in this case), and the property must be public and static.
  • Although only the static functions of a custom code object can be directly called by rule functions, there are many ways to use instance data from as many objects as you like, as demonstrated by the way FMODBase.cs refers to derived classes' binary data (notice that the GetType() function actually returns the derived type). The code above demonstrates creating one default instance that can be passed as a parameter to the static function, but any number of instances could exist or be dynamically created.