I was dumb, and was printing lots of stuff each frame. Oops
Howdy guys, here’s a challenge for you.
I want to have a function that takes a single input in the form of a dictionary:
player_axis_dict = {'command_name':[[control_name, sensitivity, deadzone], [control_name, sensitivity, deadzone]]}
#Example:
player_axis_dict = {
'throttle':[['MOUSEX', 5.3, 0.01], ['JOY1AXIS0', 2.3, 0.01]]
'yaw':[['MOUSEY', 3.6, 0.01], ['JOY1AXIS1', 1.4, 0.01]]
As an output, it must be the command name followed by a value between -1 and 1:
{'throttle':0.5, 'yaw':0}
The value is calculated by something like:
axis_value = raw_value*sensitivity #raw value is also between zero and 1
if abs(axis_value) <= deadzone:
value = 0
axis_value = max(-1, min(1, value))
I started off with the mouse, and wrote some code that accomplished this. I then ran it through the profiler, and discovered it was eating 4% logic, with spikes up to 7% If I add joystick on top of this, it’ll be way to heavy.
So how can I make this more efficient?
Here’s the relevant code:
def get_input(cont):
global all_input
..... stuff with buttons ...
axis_input = get_axis_input(player_axis_dict)
def get_axis_input(axis_dict):
input_values = get_axis_values()
axis_values = {}
for axis in axis_dict:
#Deadzone and sensitivity:
axis_inf = axis_dict[axis]
name, sens, deadzone = axis_inf
raw_value = input_values.get(name, 0)
value = input_values.get(name, 0)*sens
if abs(value) <= deadzone:
value = 0
axis_values[axis] = max(-1, min(1, value))
return axis_values
def get_axis_values():
'''Returns a dictionary of {'axisname':offset}'''
axis_dict = {}
#Mouse Input
mouse_values = get_mouse_axis()
axis_dict['MOUSEX'] = mouse_values[0]
axis_dict['MOUSEY'] = mouse_values[1]
#Joystick Input
....nothing yet...
return axis_dict
def get_mouse_axis():
pos = bge.logic.mouse.position
x = round(pos[0]-0.5, 3)
y = round(pos[1]-0.5, 3)
bge.logic.mouse.position = (0.5, 0.5)
return [x,y]
How can I improve this?
For bonus points, how can I do joystick? I haven’t had a look at it yet though, so you don’t have to answer.
I was dumb, and was printing lots of stuff each frame. Oops