AndersMalmgren / FreePIE

Programmable Input Emulator
650 stars 144 forks source link

MIDI to vJoy #110

Closed PWBENNETT closed 7 years ago

PWBENNETT commented 7 years ago

I think my issue is due to a lack of Python knowledge on my behalf. I'm trying to pass vJoy[0].x as an argument to a function, but it's receiving vJoy[0].x as call-by-value instead of call-by-reference.

https://gist.github.com/PWBENNETT/fffbce4aaccf7c7bc85f3fb13c61990d

Presumably, what I'm looking for is something to do with the id() function and whatever the inverse of the id() function is.

A little help on the proper API would be nice. Thanks in advance.

PWBENNETT commented 7 years ago

I tried doing something like https://gist.github.com/PWBENNETT/28ee05249d3da1de1b083075380148f4 with .set, .update, .setitem, and several other method names, as well (those variants all crash instead of running and failing)

MarijnS95 commented 7 years ago

vJoy[0].x is a simple floating point value, not a reference. You were close, the function you're looking for is setattr(object, name, value):

setattr(vJoy[k[2]], k[3], vJoy[0].axisMax * midi[k[0]].data.buffer[1] / 127)

A (lambda) function could be used as well, so that the update function works completely separate and doesn't need to know that you're setting a vJoy axis, making it more generic and reusable. Such a thing would look like this:

knob(1, 48, lambda v: vJoy[0].x = v)

(A lambda function is an anonymous function, which makes it easy to write inline functions without def'ing them and then passing the name to a function.)

And in your update function:

 k[2](vJoy[0].axisMax * midi[k[0]].data.buffer[1] / 127)

(It's up to you to remove the vJoy[0].axisMax dependency :wink:)

PWBENNETT commented 7 years ago

Awesome! Thanks!

https://gist.github.com/PWBENNETT/28ee05249d3da1de1b083075380148f4 is what ended up working for me.