jakub.hubacek
Topic Author
Posts: 2
Joined: 05 Jan 2021, 12:27

Using Delegate Commands with Noesis in UE4

04 Feb 2021, 22:21

Hello.
We are trying to implement DelegateCommand with Unreal Engine. I know we can use UFUNCTION but I would like to have option to extract whole action into another class (eg AddItemDelegateCommand).

Here is example of view model implementation in c#. This is roughly something we aim for.
public class TestViewModel : ViewModelBase {
    public String name{ get; private set; }
    public DelegateCommand  DelegateCommand { get; private set; }

    public DelegateCommandsViewModel() {
        DelegateCommand1 = new DelegateCommand(
            () => MessageBoxService.Show("This can be in "));
    }
}
And here is our C++ variant of view model in UE4.
UCLASS(Blueprintable)
class UTestViewModel : public UBaseViewModel
{
	GENERATED_BODY()

public: // Properties

	UPROPERTY(BlueprintReadWrite)
	FString name;

	//first variant - this works fine
	UFUNCTION()
	void ButtonPressedCommand1();
	
	//second variant - this would be also handy
	UFUNCTION() // or UPROPERTY()?
	UButtonPressedCommand buttonPressedCommand2; // ButtonPressedCommand implements Noesis::ICommand 
	
	//third varinat - this is what we aimed for
	UFUNCTION() // or UPROPERTY()?
	UDelegateCommand buttonPressedCommand3;
};
UDelegateCommand is our attempt to implement DelegateCommand for UE4.
Inspiration from this documentation page: https://www.noesisengine.com/docs/Gui.C ... omCommands

UDelegateCommand looks like this:
UCLASS()
class UDelegateCommand final : public UObject, public Noesis::BaseCommand
{
	GENERATED_BODY()

public:

	typedef Noesis::Delegate<void(BaseComponent*)> ExecuteFunc;
	typedef Noesis::Delegate<bool(BaseComponent*)> CanExecuteFunc;

	UDelegateCommand() {};

	UDelegateCommand(const ExecuteFunc& ExecuteMethod);

	UDelegateCommand(const ExecuteFunc& ExecuteMethod, const CanExecuteFunc& CanExecute);

	bool CanExecute(BaseComponent* Param) const override;

	void Execute(BaseComponent* Param) const override;

	void SetExecuteFunc(const ExecuteFunc& Method);

private:
	ExecuteFunc MethodToExecute;
	CanExecuteFunc CanExecuteMethod;
};
But we are not able to set execute function correctly via MakeDelegate.

Is this even possible with NoesisGUI for UE4? Maybe we are doing something wrong, I can share more snippets.

It would be great to have working example to look into, for second or third variant.

Can you please point me in the right direction to look? :)

Thank you,
Jakub Hubacek
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Using Delegate Commands with Noesis in UE4

05 Feb 2021, 11:12

Hi Jakub,

What is the purpose of extracting the functions to another class? In theory you would want to execute functions of the same view model from that DelegateCommand.

Right now we only support UFUNCTION (with 0 or 1 parameters) to be used as a Command property. And you can specify a pair of functions to have both Execute and CanExecute callbacks:
UFUNCTION()
void ButtonPressed();

UFUNCTION()
bool CanExecuteButtonPressed();
Could that work for you?
 
jakub.hubacek
Topic Author
Posts: 2
Joined: 05 Jan 2021, 12:27

Re: Using Delegate Commands with Noesis in UE4

05 Feb 2021, 14:20

I was thinking about extracting executive logic into another class which could be reused in another view model if suitable.
Solution you are providing works for us too :) I was just interested if there is another approach.

Thank you
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Using Delegate Commands with Noesis in UE4

05 Feb 2021, 17:02

I guess you can still reuse some logic encapsulated in another class by accessing it from the view model functions.

I'll mark this as solved then.

Who is online

Users browsing this forum: Ahrefs [Bot], camimamedov and 61 guests