Smart direction key detection

I received a report on the Full Input Keyboard script written by cidiomar which allows you to use the entire keyboard for your game instead of the default buttons.


The problem was related to direction keys.


Here are some examples of the problem:


Case 1


1. Create a new project


2. Move down


3. While holding the down button, press left or right



The expected behavior is you would move down, but when you press left or right, you would move left or right immediately in response to the keys


Now, install the full input keyboard script, and repeat the same experiment.


However, this time, you'll notice you can't move left or right WHILE moving down.


BUT, if you were to move left or right WHILE moving up, it does work as expected.


Case 2


Here's another behavior. Starting without any scripts,


1. Move down


2. While moving down, press the up button


The expected behavior is the character will stop.


However, with full input keyboard, this does not work.


Problem


There is a problem with the way dir4 (and consequently, dir8) have been implemented.


def self.dir4
return 2 if self.press?(DOWN)
return 4 if self.press?(LEFT)
return 6 if self.press?(RIGHT)
return 8 if self.press?(UP)
return 0
end


This means that if you're pressing down, the code will always return there.


This means if you were pressing down AND left, the code will still return 2, and therefore the game would say you're only moving down.


Now, if you were pressing UP, you could press left or right, and the code will return left or right as needed because they are checked before checking for the UP key.


So there are two issues here


1. Ace's default input direction updating takes into consideration changed buttons


2. If you were to press opposite directions at the same time, it's as if they don't move


A set of mutually exclusive conditional branches here doesn't seem enough.


How can this be improved to allow for the player to change directions while holding any direction button?
Can the solution be generalized to 8 directions?
There you go:

Spoiler
module Input

def self.pref_ax
@pref_ax = '' if @pref_ax.nil?
return @pref_ax
end

def self.pref_ax=(val)
@pref_ax = val
end

def self.dir8
@dir8 = 0 if @dir8.nil?
return @dir8
end

def self.dir8=(val)
@dir8 = val
end

def self.dir4
xx = self.get_x
yy = self.get_y
self.dir8 = self.num_dir(xx,yy)
if xx != 0 && yy != 0
if self.pref_ax == 'x'
yy = 0
else
xx = 0
end
elsif xx != 0
self.pref_ax = 'y'
elsif yy != 0
self.pref_ax = 'x'
end
return self.num_dir(xx,yy)
end

def self.get_x
xx = 0
xx -= 1 if self.press_ex?($system[😛1][:f_left])
xx += 1 if self.press_ex?($system[😛1][:f_right])
return xx
end

def self.get_y
yy = 0
yy -= 1 if self.press_ex?($system[😛1][:f_up])
yy += 1 if self.press_ex?($system[😛1][:f_down])
return yy
end

def self.num_dir(x,y)
if x != 0 || y != 0
return 5 - y * 3 + x
else
return 0
end
end

end



dir4 and dir8 included.


This is the exact same way how the default engine works (in all RM engines, I guess).


Don't ask me about that num_dir method, I would have never figured that out on my own. >.> 


I knew it worked something like that (with the axis stuff), because I saw it in another engine (either Unity or Construct, I think), but had no clue about the actual direction value math.


Fortunately, MV got the same method names, so it wasn't hard to look it up in the source code for that engine.


Note that I changed the actual input press checks to my own system button checks, so make sure to edit those with your own checks (depending on which input script you are using)!

Original thread: https://forums.rpgmakerweb.com/threads/smart-direction-key-detection.67120/