Right now, the way that way that we have to bind delegates involves calling a bind delegate event dispatcher, and then linking that directly to the execution node of the delegate, from which flows all of the code executed in the delegate. This creates a number of undesirable behaviors:
You can’t bind delegates in the construction script.
Procedurally binding delegates becomes very, very messy involving a secondary layer of events to bind/unbind events and a layer of function calls.
The additional complexity increases the likelihood of bugs, and makes the code less readable.
The Question:
Is there a better way to bind delegates that:
Does not require the additional layer of functions and delegates to set delegates
Can be called from the construction graph.
The Suggestion
If the answer to the above is “No”, perhaps Epic could implement a event stub for use in binding. Essentially, the stub creates the event signature, but has no executable pin. The event, with its executable, would then be propagated to the event graph as an empty event node.
Benefits:
Events could be bound in the construction script, which would make it propagate to all child objects.
Make blueprints more readable by allowing better positioning of event nodes by allowing them to be detached from the OnBeginPlay event chain.
Removes the need for an additional layer of event nodes to bind/unbind the events that you actually want.
Would allow for events to be bound in functions, but defined in the event graph.
im not sure the answer to your question,
but in most cases where you could use a delegate to solve a problem, you can usually use overridden virtual functions or switch cases instead.
so instead of binding attack() to one of many possible attack functions, you have your object contain a weapon pointer, and the subclass of weapon controls which overridden attack function is called.
another easy to read option is to use a switch case inside of a function. then changing its behavior is as simple as setting an integer or string.
i usually avoid delegates for easier to read code, but i would love to know of any circumstance that actually requires using delegates.
Some things, such as getting mouse clicks on objects or processing key strokes with an object targeted are better done with delegates. For example, I want a context menu to pop up when the user right clicks on an object. I build the context menu on the object itself, and pass it back to the player, but something actually has to catch that event in the first place. So, I have an OnRightClick delegate that I created through an interface. And yes, I know that many things could be taken out of delegates and put into functions instead, and that is what I do, most of the time.
That doesn’t solve the problem, however. I would like to be able to bind events in the construction script, and right now you can’t. I would also like to be able to bind event inside of functions, and right now you can’t. I am generally able to find ways to work around it, but finding ways to work around problems instead of fixing what causes the problem is not really the best solution.
Thanks for the reply though. Generally speaking I absolutely agree with you.
A proper event delegate and dispatching system would be one of the best methods for cleaning up code - custom events try to do that to some extent. It really doesn’t make a lot of sense why UE doesn’t provide a delegate variable type. At that point getters/setters would allow for separating out the processing of event into different EventGraphs making for some very clean blueprint code.
Post feature request in feedback forum since there no other way to bind then what you see.
You can’t bind in construction script due to fact that it’s running in editor-time and binding anything might cause strange behaviors of actor in editor, BeginPlay should be used, event executed when actor is fully gameplay ready.
I am a little confused on your question, but I will do what I can.
The event/delegate system in UE4 is fairly deliberate in its design. That said using delegates effectively may not be the most transparent thing in the world depending on how much exposure you have had to event/delegates in the past.
You may find this article both helpful and expositive. Delegates | Unreal Engine Documentation
You are definitely able to bind events in both functions and in the construction script, the best way to do this is to understand the ‘function signature’ that is required by the delegate you are trying to bind. If you have an event or function anywhere in your code that matches that signature, you can bind to it at your leisure, wherever you like. In blueprints this is done with the ‘create event’ node, and in c++ it is as simple as BindUObject().
I hope this helps, let me know if you have further questions.
-Spiris
I just want to create an even that will change something on a key press in construction. I don’t understand why this is hard. Why can’t I find a way to bind a key? I feel stupid because I’m not able to do something so fundamental please help me someone thanks.