hello,
first of all : All of my declared functions (in the headers) are also defined (in the cpp files) . my templates are all defined in the headers.
I assume that the linker doesnt find the files even though the auto completion does.
to the problem:
i get a LNK2019 error by calling the constructor of the "UiTransform2dComponent" class, but the constructor is defined in the cpp file. if i remove the line
CreateComponentNew<UiTransform2dComponent>(buttonElem);
it will compile fine.
AZ::Entity* CreateUIElements::CreateButtonNew(const char* name, bool atRoot, AZ::EntityId parent, UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets, const char* text, ColorF baseColor, ColorF selectedColor, ColorF pressedColor, ColorF textColor)
{
AZ::Entity* buttonElem = nullptr;
if (atRoot)
{
EBUS_EVENT_ID_RESULT(buttonElem, parent, UiCanvasBus, CreateChildElement, name);
}
else
{
EBUS_EVENT_ID_RESULT(buttonElem, parent, UiElementBus, CreateChildElement, name);
}
{
AZ::EntityId buttonId = buttonElem->GetId();
// create components for button elem
//AZ::Component* component = buttonElem->CreateComponent<UiTransform2dComponent>();
//buttonElem->CreateComponent<UiTransform2dComponent>();
CreateComponentNew<UiTransform2dComponent>(buttonElem);
//UiTransform2dComponent* t = new UiTransform2dComponent();
//CreateComponentNew<UiImageComponent>(buttonElem);
//CreateComponent<UiButtonComponent>(buttonElem);
buttonElem->Init();
buttonElem->Activate();
CRY_ASSERT_MESSAGE(UiTransform2dBus::FindFirstHandler(buttonId), "Transform2d component missing");
EBUS_EVENT_ID(buttonId, UiTransform2dBus, SetAnchors, anchors, false, false);
EBUS_EVENT_ID(buttonId, UiTransform2dBus, SetOffsets, offsets);
EBUS_EVENT_ID(buttonId, UiImageBus, SetColor, baseColor);
EBUS_EVENT_ID(buttonId, UiButtonBus, SetSelectedColor, selectedColor);
EBUS_EVENT_ID(buttonId, UiButtonBus, SetPressedColor, pressedColor);
string pathname = "Textures/Basic/Button_Sliced_Normal.sprite";
ISprite* sprite = gEnv->pLyShine->LoadSprite(pathname);
EBUS_EVENT_ID(buttonId, UiImageBus, SetSprite, sprite);
EBUS_EVENT_ID(buttonId, UiImageBus, SetImageType, UiImageInterface::ImageType::Sliced);
}
{
// create child text element for the button
AZ::Entity* textElem = nullptr;
EBUS_EVENT_ID_RESULT(textElem, buttonElem->GetId(), UiElementBus, CreateChildElement, "ButtonText");
AZ::EntityId textId = textElem->GetId();
//CreateComponent<UiTransform2dComponent>(textElem);
//CreateComponent<UiTextComponent>(textElem);
textElem->Init();
textElem->Activate();
CRY_ASSERT_MESSAGE(UiTransform2dBus::FindFirstHandler(textId), "Transform component missing");
EBUS_EVENT_ID(textId, UiTransform2dBus, SetAnchors, UiTransform2dInterface::Anchors(0.5, 0.5, 0.5, 0.5), false, false);
EBUS_EVENT_ID(textId, UiTransform2dBus, SetOffsets, UiTransform2dInterface::Offsets(0, 0, 0, 0));
EBUS_EVENT_ID(textId, UiTextBus, SetText, text);
EBUS_EVENT_ID(textId, UiTextBus, SetTextAlignment, IDraw2d::HAlign::Center, IDraw2d::VAlign::Center);
EBUS_EVENT_ID(textId, UiTextBus, SetColor, textColor);
EBUS_EVENT_ID(textId, UiTextBus, SetFontSize, 24.0f);
}
return buttonElem;
my header file:
#include <LyShine/Bus/UiButtonBus.h>
#include <LyShine/Bus/UiTextInputBus.h>
#include "../LyShine/UiTransform2dComponent.h"
#include "../LyShine/UiImageComponent.h"
#include "../LyShine/UiButtonComponent.h"
#include "../LyShine/UiTextComponent.h"
#include "../LyShine/UiTextInputComponent.h"
#ifndef CreateUIElements_H
#define CreateUIElements_H
class CreateUIElements {
public :
template <typename T>
void CreateComponentNew(AZ::Entity* entity) {
//T* component = entity->CreateComponentNew<T>();
AZ::Component* component = entity->CreateComponent<T>();
//AZ::Component* component = entity->CreateComponent<T>();
//CRY_ASSERT_MESSAGE(component, "Failed to create component");
};
AZ::Entity* CreateButtonNew(const char* name, bool atRoot, AZ::EntityId parent, UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,const char* text, ColorF baseColor, ColorF selectedColor, ColorF pressedColor, ColorF textColor);
// AZ::Entity* CreateText(const char* name, bool atRoot, AZ::EntityId parent, UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets, const char* text, ColorF textColor, IDraw2d::HAlign hAlign, IDraw2d::VAlign vAlign);
// AZ::Entity* CreateTextInput(const char* name, bool atRoot, AZ::EntityId parent, UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets, const char* text, const char* placeHolderText, ColorF baseColor, ColorF selectedColor, ColorF pressedColor, ColorF textColor, ColorF placeHolderColor);
};
#endif
part of the entity header. creating the new component causes the LNK error.
template<class ComponentType>
ComponentType* CreateComponent()
{
ComponentType* component = new ComponentType();
if (component)
{
AddComponent(component);
}
return component;
}
and here is definition of the constructor for the UiTransform2dComponent
UiTransform2dComponent::UiTransform2dComponent()
{
m_pivot = (Vec2(0.5f, 0.5f));
m_rotation = (0.0f);
m_scale = (Vec2 (1.0f, 1.0f));
m_scaleToDevice = (false);
}
doing this will throw an LNK error too:
UiTransform2dComponent* t = new UiTransform2dComponent()
the error message:
Error 560 error LNK2019: unresolved external symbol "public: __cdecl UiTransform2dComponent::UiTransform2dComponent(void)" (??0UiTransform2dComponent@@QEAA@XZ) referenced in function "public: class UiTransform2dComponent * __cdecl AZ::Entity::CreateComponent<class UiTransform2dComponent>(void)" (??$CreateComponent@VUiTransform2dComponent@@@Entity@AZ@@QEAAPEAVUiTransform2dComponent@@XZ) c:\Amazon\Lumberyard\1.1.0.0\dev\Solutions\LumberyardSDK.depproj\CreateUIElements.cpp.104.obj _WAF_