Jump to content

C++ Component System


Josh
 Share

Recommended Posts

C++ component system integration with editor is finished. The best way to learn is to look at the sample components in Source/Components.

To use a component include the header file. If you want components to be loaded automatically from a map, you need to call RegisterComponent() so the engine knows it exists:

#include "UltraEngine.h"
#include "Components/Motion/Mover.hpp"
#include "Components/Player/CameraControls.hpp"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{     
    RegisterComponent<Mover>();
    RegisterComponent<CameraControls>();

Don't forget the component name, since this is how it is identified:

    Mover()
    { 
        name = "Mover";
    }

The Copy method is also required. In most cases you can just use this code, which will just copy all the object values:

    virtual shared_ptr<Component> Copy()
    {
        return std::make_shared<Mover>(*this);
    }

When loading a component from a map, the engine will automatically call the Load method:

    virtual bool Load(table& properties, shared_ptr<Stream> binstream, shared_ptr<Scene> scene, const LoadFlags flags)
    {
        if (properties["movementspeed"].is_array() and properties["movementspeed"].size() == 3)
        {            
            movementspeed.x = properties["movementspeed"][0];
            movementspeed.y = properties["movementspeed"][1];
            movementspeed.z = properties["movementspeed"][2];
        }
        if (properties["rotationspeed"].is_array() and properties["rotationspeed"].size() == 3)
        {            
            rotationspeed.x = properties["rotationspeed"][0];
            rotationspeed.y = properties["rotationspeed"][1];
            rotationspeed.z = properties["rotationspeed"][2];
        }
        if (properties["globalcoords"].is_boolean()) globalcoords = properties["globalcoords"];
        return true;
    }

New methods have been added. UpdateMatrix() will be called anytime the entity moves or rotates. Listen() and ProcessEvent() can be used to respond to GUI and other events:

	bool Component::ProcessEvent(const Event& e)
	{
		switch (e.id)
		{
			case EVENT_KEYDOWN:
				break;
		}
		return true;
	}

 

  • Like 3

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I can't seem to call RegisterComponent from the engine API, it states it's undefined. 

I just copied and pasted the function in my code, and it works.

Made it work, it was my implementation as I don't want to register everything within the main function. 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

I just created a file called "RegisterComponents.cpp" and I have it in my Components folder. Then I call the function in my Program class. 

#pragma once
#include "UltraEngine.h"
#include "RegisterComponents.h"

// Components
#include "Motion/Mover.hpp"
#include "Player/CameraControls.hpp"
#include "Player/SettingsListener.hpp"

namespace UltraEngine::Game
{
	void RegisterComponents()
	{
		RegisterComponent<Mover>();
		RegisterComponent<CameraControls>();
		RegisterComponent<SettingsListener>();
	}
}

Everything works as expected. My only issue right now is that the editor can't load existing ultra maps which makes further developing this annoying, but I expect this to be fixed eventually. 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...