Casting object to interface

I think I might be missing something as my cast from an asset UObject to my LootItem interface is causing the following error in the ScriptInterface class:

‘void FScriptInterface::SetObject(UObject *)’ : cannot convert argument 1 from ‘ILootItem *’ to ‘UObject *’

As of current I have tried both

TScriptInterface<ILootItem> Item = Cast<ILootItem>(Assets[AssetIdx].GetAsset());

and

ILootItem* Item = Cast<ILootItem>(Assets[AssetIdx].GetAsset());

Both methods return the same error though. I have seen this done successfully on a tutorial on the wiki but cannot figure out why it is failing for me.

#Solution

"cannot convert argument 1 from 'ILootItem ' to 'UObject *'"

This means that your LootItem is not defined in the context you are trying to use it, so the compiler doesnt know what to do :slight_smile:

At the top of the cpp file involved, add this

#include "LootItem.h"

or whatever file your ILootItem definition is stored in

#:heart:

Rama

I have the include in the class trying to perform the cast already. The error itself is being thrown from the ScriptInterface class, so adding any sort of include there isn’t an option.

The only other piece of information that I haven’t included is that the Interface is declared in a module which I’m depending on. I’m not sure if that changes anything though.

The code for the interface:

#pragma once

#include "LootItem.generated.h"

UINTERFACE()
class LOOTSYSTEM_API ULootItem : public UInterface
{
	GENERATED_UINTERFACE_BODY()
	    
};

class ILootItem
{
	GENERATED_IINTERFACE_BODY()

	virtual FText GetItemName();
};

Hi DC,

Try to use InterfaceCast instead of Cast

Best regards, Gleb

Yields the exact same error. Plus InterfaceCast is now deprecated in 4.6

I’ve tried a few other ways of writing this out, still having no luck with it. The only thing I can figure is that because the interface is in a dependent module that something is going wrong?