Skip to content

Defining new object

Matěj Štágl edited this page Nov 30, 2018 · 4 revisions

In Simplex, all objects must be defined under package/objects/ folder, while there can be specified any number of subfolders within objects. Thus package/objects/actors/enemy1 is a valid place for an object. Please note that location of object in these subfolders has no impact on where the object will be shown in the room editor.

To add a new object right click objects folder and from the context menu select add->class.

You will be prompted to input a name, which should not contain spaces nor any special characters. It can include .cs as a suffix but it's not required, system will add it automatically. After filling name, confirm dialog by clicking Add button.

Once class is created, basic code is generated.

using System;
using System.Collections.Generic;
using System.Text;

namespace SimplexResources.Objects // Your namespace, this will differ
{
    class MyObject // Name of your object
    {
    }
}

First step is to inherit GameObject which will allow for use of all object variables, like speed, direction or spriteIndex.

using System;
using System.Collections.Generic;
using System.Text;
using SimplexCore; // Namespace of engine classes, won't work without this

namespace SimplexResources.Objects : GameObject // : is a symbol for inheritance, GameObject is a class provided by Simplex
{
    class MyObject
    {
    }
}

Once class is a Gameobject child, we need to make it public so all other objects can see it:

using System;
using System.Collections.Generic;
using System.Text;
using SimplexCore;

namespace SimplexResources.Objects : GameObject
{
    public class MyObject // Simply prepend class keyword with "public"
    {
       // Place for your code is here
    }
}

In the last step, which is optional, we will specify resource tree location for our new object. If you omit this step, it will be located under the root Objects node.

using System;
using System.Collections.Generic;
using System.Text;
using SimplexCore;

namespace SimplexResources.Objects
{
    class MyObject : GameObject
    {
        // This is contructor event of an object. It's defined as public ObjectName() {}
        // Contructor is used to set object variables rather than instance variables.
        public MyObject()
        {
            // EditorPath is a special variable pointing object's position in the resource tree
            // It shouldn't start nor end with /
            EditorPath = "SampleFolder/Subfolder";         
        }
    }
}

Voilà, our new object is created! Next step is to add some code to the object, we will take a look at that here.

Back to getting started

Clone this wiki locally