0

I'm trying to simulate a controller via python but having issues with timing.

I've successfully implemented reading the inputs into a dictionary, and then collecting all of them into a list. I know that the game(in Unity) doesn't recognise inputs faster than 0.2s. Using the following function, I've managed write at 0.2s with an error of less than 0.001s with the following function:

def feed(line: list, interval: float = 0.1.99):
    start = time_ns()
    wait_ns = interval * 10**9
    for moment in list:
        while (time_ns() < start + wait_ns):
            pass
        for key, val in moment:
            controller.set_value(key, val) #this feeds the keys to the game via pyxinput.

I'm sampling the input using a similar function as above. But yet, I'm not getting the correct behaviour, it seems to be a timing problem. I tried reading the synthetic inputs, and they're being input with the correct timing, i.e. the interval that I pass in(and the aforementioned error).

For reference, the following, hard coded input works correctly.

controller.set_value('AxisLy', -1) #push the stick down
sleep(0.2)
controller.set_value('AxisLy', 0) #return it to neutral

I'm at my wit's end after fighting with this for the past few days. So, any ideas on what could be going wrong, or how I may debug it?

4amvim
  • 115
  • 1
  • 9

1 Answers1

0

Instead of hard coding in a sleep interval, try using Invoke() or a coroutine. I find them very simple, useful and easy to understand when you're wanting to wait a certain period of time. Alternatively, use Time.time to track the start time of a button press, and then check if (Time.time >= startTime + interval) for system clock time.

Sorry for the C#, it's what i'm more familiar with. But you should be able to get the gist of it.

  • I don't mind the C#. But (a) rest of my framework is in Python and (b) I don't have the source for the game. Given (b), is there a way to use Mono functions (preferably via Python, but I'll be grateful for any way) ? Thank You for the new thread of thought though, much appreciated by by dimnished wits :) – 4amvim Oct 29 '20 at 01:25