#Perform InputAction's performed callbacks via script

1 messages · Page 1 of 1 (latest)

thorn bison
#

Hi guys. I have an InputAction and I want to trigger its performed callbacks via script.
This is an attemp using reflection :
var performedField = typeof(InputAction).GetField("m_OnPerformed", BindingFlags.Instance | BindingFlags.NonPublic); // Invoke each callback in the array using reflection var performedCallbackArray = performedField.GetValue(_inputAction); var performedInlinedArray = performedCallbackArray.GetType().GetField("m_Callbacks", BindingFlags.Instance | BindingFlags.NonPublic); var callbacks = performedInlinedArray.GetValue(performedCallbackArray) as IEnumerable<Action<InputAction.CallbackContext>>; foreach (var callback in callbacks) { callback.Invoke(default); }

But it doesn't work. The list of callbacks results empty

opaque fox
#

Oh jesus christ why are you using reflection monkaPray

thorn bison
mighty fox
#

Not sure what reflection is.
But if you just want a callback that can talk to another script:
https://youtu.be/OuZrhykVytg

📝 C# Basics to Advanced Playlist https://www.youtube.com/playlist?list=PLzDRvYVwl53t2GGC4rV_AmH7vSvSqjVmz
🌍 Get my Complete Courses! ✅ https://unitycodemonkey.com/courses​
👍 Learn to make awesome games step-by-step from start to finish.
🎮 Get my Steam Games https://unitycodemonkey.com/gamebundle

✅ Let's check out what are C# Events, how we can ...

▶ Play video
thorn bison
#

Thanks @mighty fox 😉 but it's not what I'm looking for. I have an InputAction that is bound to the SPACE keyboard button.
In different part of my code, I register different callback methods to it doing:
_inputAction.performed += callbackName;
Pressing the SPACE keyboard button everything works fine. But now I want to trigger the _inputAction.performed callbacks via script (without pressing the SPACE button.)
Someone knows how can I do that?

opaque fox
opaque fox
#

This is such big XY

astral streamBOT
#

The XY problem is asking about your attempted solution rather than your actual problem. This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

  • You want to do X
  • You don't know how to do X, but think you can solve it if you can just about manage to do Y
  • You don't know how to do Y either, so you ask for help with Y
  • Others try to help you with Y, but are confused because Y seems like a strange problem to want to solve
  • After much interaction and wasted time, it finally becomes clear that you really want help with X, and that Y wasn't even a suitable solution for X
    Source: http://xyproblem.info/

Please include information about a broader picture along with any attempted solution, including solutions you have already ruled out and why.

thorn bison
#

I found the problem.
[FIRST PART]
I give an example. Let's say I have an InputAction to shoot that is bound to the SPACE keyboard key.

In my code I assigned many callback methods to the shoot action by taking the InputAction from the C# generated classes. Let's say I have an InputActionAsset named "MyGameInputs" (with the option "Generate C# Class" checked) and I do Something like:
_shootInputAction = MyGameInput.Inputs.Shooting.Shoot; _shootInputAction.performed += makeDamage; _shootInputAction.performed += reduceAmmo; ...and so on....

Now I want to trigger the "shoot" action not only by pressing the SPACE keyboard button, but also by clicking a UI button "Click here to SHOOT".
So, when the player clicks the button, I want to perform all the callbacks that are triggered when the player press the SPACE keyboard button (makeDamage, reduceAmmo, etc...). In other words I want a way to simulate pressing the SPACE keyboard button via script.
I thought that, instead of calling manually all the methods associated to _shootInputAction.performed (let's say there are 100 methods registered in 20 different scripts and I don't know all of them), a smart way to do this was simply to call all the callbacks registered to the _shootInputAction.performed