Run function when variable changes.

Is it possible to run a function when a variable gets changed?
Say a boolean goes from false to true. The moment it changes a function would be executed.

I’ve read a little bit on the wiki and i guess delegates could maybe work, otherwise I’m open for suggestions.

Usually I use getter/setter functions for vars (unless private within their own class). This way it makes things like this very easy, as you just drop your notify logic in the setter function.

Sadly I don’t know how to use property setter/getters for variables which are changed outside of udk’s own scripts.
There’s an array in Input.uc called PressedKeys which contains elements of keys being currently pressed. There are no references that add elements to it so I guess the engine itself handles that.

Is there no other way of making a function react to elements being added from an array like that?
The wiki pages for both event and delegates don’t really contain a lot of info regarding if it would be possible to use them for this, which is why I am asking.

May i ask what it is you are trying to achieve? There may be an easier way to do what you want.

The “Length” property of a dynamic array is applied when the array is updated, so there should be little to no overhead in checking if its length has changed in the Tick(). Although i still think there will be a more eloquent solution if i knew what it was you were trying to do.

Mainly just getting player input and using it for example having the player input numbers to a keypad or simulating a keyboard within the game. I know you can use bindings for it if you want to know if certain keys are being pressed but if you want to know if other keys are being pressed down that won’t work.

I’m currently using PlayerTick then checking if the dynamic array is being updated just like you wrote which works!

Try adding this to your PlayerInput class:



//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
//Captures all keypresses
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
function bool KeyInput(int ControllerId, name KeyName, EInputEvent IEvent, float AmountDepressed, optional bool bGamepad);


I don’t actually see in in the base UDK code, it’s been so long since I added. Can’t remember how it’s hooked up.

Edit: You need to add this to your PlayerInput default props:



OnReceivedNativeInputKey=KeyInput


Through looking at OnReceivedNativeInputKey I also found OnReceivedNativeInputChar which works even better for what I’m trying to do since it doesn’t get called after releasing the key.

Thanks for the help!