Page 1 of 1

Ptr<SomeNonNoesisClass>

Posted: 20 Apr 2018, 15:50
by GeorgeR
This doesn't work:
#include "StackSizeVisibilityConverter.h"

#include <NsCore/ReflectionImplement.h>
#include <NsCore/Boxing.h>
#include "ItemInstance.h"
#include "Item.h"
#include "NsGui/Enums.h"

using namespace Frontier::UI::Converters;
using namespace Noesis;

bool StackSizeVisibilityConverter::TryConvert(Noesis::BaseComponent* value, const Type* type, BaseComponent* parameter, Noesis::Ptr<BaseComponent>& result)
{
	NS_UNUSED(type, parameter);

	if (!Boxing::CanUnbox<Ptr<FItemInstance>>(value))
		result = Boxing::Box<Visibility>(Visibility::Visibility_Visible);

	auto itemInstance = Boxing::Unbox<Ptr<FItemInstance>>(value);

	result = Boxing::Box<Visibility>(itemInstance->GetItem()->bIsStackable && itemInstance->StackSize > 1
		? Visibility::Visibility_Visible
		: Visibility::Visibility_Collapsed);

	return true;
}

NS_IMPLEMENT_REFLECTION(StackSizeVisibilityConverter)
{
	NsMeta<TypeId>("Frontier.UI.Converters.StackSizeVisibilityConverter");
}
Apparently FItemInstance doesn't have AddReference. I tried unboxing to a regular pointer but that doesn't work either.

Re: Ptr<SomeNonNoesisClass>

Posted: 23 Apr 2018, 18:09
by jsantos
As in C#, only struct types can be boxed, you cannot box or unbox an object in the heap, because in that case the object is already a BaseComponent and you don't need boxing. Similarly you only can have Ptr<> for types inheriting from BaseRefCounted.

Not sure to understand why you need to do that. I am sure hcpizzi will give you a better answer than me.

Re: Ptr<SomeNonNoesisClass>

Posted: 23 Apr 2018, 18:22
by hcpizzi
What you're receiving is not a boxed FItemInstance, but a NoesisStructWrapper, which is a BaseComponent derived type that we use to wrap UStructs and make their properties accessible to Noesis, but it's not a BoxedValue, so CanUnbox/Unbox/Box won't work with them.

I could expose functions similar to those, as I have all the ingredients in place. However, as I mentioned in the bugtracker ticket you opened regarding two way bindings to UStructs, I'm in the process of revisiting that code and things may change. Nonetheless, there should be a way to get the UStruct from those BaseComponents, the same way you can get an UObject by using NoesisCreateUObjectForComponent. There's a NoesisCreateComponentForUStruct, but not the opposite. That would be the missing piece of the puzzle.

Re: Ptr<SomeNonNoesisClass>

Posted: 21 May 2018, 11:00
by hcpizzi
Hi George,

I've implemented a function named NoesisGetUStructFromComponent that will allow you to extract the USTRUCT from the BaseComponent wrapper.

Please, let us know if this fits what you need.