Jump to content

Component's Start Function


reepblue
 Share

Go to solution Solved by Josh,

Recommended Posts

I'm finding that a component's start function isn't called when loaded from a map. I'm attaching multiple components in my case, but it should work regardless. I need it to bind my listen events. 

#pragma once
#include "UltraEngine.h"

using namespace UltraEngine;

class CameraControls : public Component
{
    bool freelookstarted{ false };
	Vec3 freelookmousepos;
	Vec3 freelookrotation;
	Vec2 lookchange;
public:
	float mousesmoothing = 0.0f;
	float mouselookspeed = 1.0f;
	float movespeed = 4.0f;

	CameraControls()
	{
		name = "CameraControls";
	}

	virtual void Start()
	{
		Print("Put a break on me!")
	}

	virtual void Update()
    {
		auto window = ActiveWindow();
		if (window == NULL) return;
		if (!freelookstarted)
		{
			freelookstarted = true;
			freelookrotation = entity->GetRotation(true);
			freelookmousepos = window->GetMouseAxis();
		}
		auto newmousepos = window->GetMouseAxis();
		lookchange.x = lookchange.x * mousesmoothing + (newmousepos.y - freelookmousepos.y) * 100.0f * mouselookspeed * (1.0f - mousesmoothing);
		lookchange.y = lookchange.y * mousesmoothing + (newmousepos.x - freelookmousepos.x) * 100.0f * mouselookspeed * (1.0f - mousesmoothing);
		if (Abs(lookchange.x) < 0.001f) lookchange.x = 0.0f;
		if (Abs(lookchange.y) < 0.001f) lookchange.y = 0.0f;
		if (lookchange.x != 0.0f or lookchange.y != 0.0f)
		{
			freelookrotation.x += lookchange.x;
			freelookrotation.y += lookchange.y;
			entity->SetRotation(freelookrotation, true);
		}
		freelookmousepos = newmousepos;
		float speed = movespeed / 60.0f;
		if (window->KeyDown(KEY_SHIFT))
		{
			speed *= 10.0f;
		}
		else if (window->KeyDown(KEY_CONTROL))
		{
			speed *= 0.25f;
		}
		if (window->KeyDown(KEY_E)) entity->Translate(0, speed, 0);
		if (window->KeyDown(KEY_Q)) entity->Translate(0, -speed, 0);
		if (window->KeyDown(KEY_D)) entity->Move(speed, 0, 0);
		if (window->KeyDown(KEY_A)) entity->Move(-speed, 0, 0);
		if (window->KeyDown(KEY_W)) entity->Move(0, 0, speed);
		if (window->KeyDown(KEY_S)) entity->Move(0, 0, -speed);
    }

	//This method will work with simple components
	virtual shared_ptr<Component> Copy()
	{
		return std::make_shared<CameraControls>(*this);
	}

	virtual bool ProcessEvent(const Event& e)
	{
		return true;
	}

	virtual bool Load(table& properties, shared_ptr<Stream> binstream, shared_ptr<Scene> scene, const LoadFlags flags)
    {
        if (properties["mousesmoothing"].is_number()) mousesmoothing = properties["mousesmoothing"];
        if (properties["mouselookspeed"].is_number()) mouselookspeed = properties["mouselookspeed"];
        if (properties["movespeed"].is_number()) movespeed = properties["movespeed"];
		return true;
    }
	
    virtual bool Save(table& properties, shared_ptr<Stream> binstream, shared_ptr<Scene> scene, const SaveFlags flags)
    {
		properties["mousesmoothing"] = mousesmoothing;
		properties["mouselookspeed"] = mouselookspeed;
		properties["movespeed"] = movespeed;
		return true;
    }
};

 

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

@Josh

Ok, this is weird. The CameraControls.hpp works fine but today I made a brand-new component and although the constructor is being called, the other functions aren't.

#pragma once
#include "UltraEngine.h"
using namespace UltraEngine;

class FPSCamera : public Component
{
public:
	FPSCamera()
	{
		name = "FPSCamera";
	}

	~FPSCamera()
	{
		name = "";
	}

	//This method will work with simple components
	virtual shared_ptr<Component> Copy()
	{
		return std::make_shared<FPSCamera>(*this);
	}

	virtual void Start()
	{
		Print("Debug: " + name);
	}

	virtual void Update()
	{
		Print("Debug: " + name);
	}

};

And yes, I do have it being registered with everything else.

#pragma once
#include "UltraEngine.h"

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

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

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

In code the component works. Do you have a JSON file and a map that uses it?

#include "UltraEngine.h"
#include "Components/Player/FPSCamera.hpp"

using namespace UltraEngine;

int main(int argc, const char* argv[])
{
    //Get the displays
    auto displays = GetDisplays();

    //Create a window
    auto window = CreateWindow("Ultra Engine", 0, 0, 1280, 720, displays[0], WINDOW_CENTER | WINDOW_TITLEBAR);

    //Create a world
    auto world = CreateWorld();

    //Create a framebuffer
    auto framebuffer = CreateFramebuffer(window);

    //Create a camera
    auto camera = CreateCamera(world);
    camera->SetClearColor(0.125);
    camera->SetFov(70);
    camera->SetPosition(0, 0, -3);

    //Create a light
    auto light = CreateBoxLight(world);
    light->SetRotation(35, 45, 0);
    light->SetRange(-10, 10);

    //Create a box
    auto box = CreateBox(world);
    box->SetColor(0,0,1);

    //Entity component system
    auto component = box->AddComponent<FPSCamera>();
    //component->rotationspeed.y = 45;

    //Main loop
    while (window->Closed() == false and window->KeyDown(KEY_ESCAPE) == false)
    {
        world->Update();
        world->Render(framebuffer);
    }
    return 0;
}

 

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

2 hours ago, Josh said:

I tried loading a map with a Mover component, and although the rotation value I set was not being loaded, the Update method was being called correctly.

I noticed this too. Something is up with map loaded components.

Yes, I set up my new component with an empty json script and although the constructor was called, none of the functions were.

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

21 minutes ago, reepblue said:

I noticed this too. Something is up with map loaded components.

Yes, I set up my new component with an empty json script and although the constructor was called, none of the functions were.

I uploaded the fix for the property name issue a couple hours ago.

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

  • Josh locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...