Smart direction key detection

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,848
First Language
English
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?
 

Sixth

Veteran
Veteran
Joined
Jul 4, 2014
Messages
2,162
Reaction score
823
First Language
Hungarian
Primarily Uses
RMVXA
There you go:

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[:p1][:f_left])
xx += 1 if self.press_ex?($system[:p1][:f_right])
return xx
end

def self.get_y
yy = 0
yy -= 1 if self.press_ex?($system[:p1][:f_up])
yy += 1 if self.press_ex?($system[:p1][: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)!
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Profile Posts

Don't forget, aspiring writers: Personality isn't what your characters do, it is WHY they do it.
Hello! I would like to know if there are any pluggings or any way to customize how battles look?
I was thinking that when you start the battle for it to appear the eyes of your characters and opponents sorta like Ace Attorney.
Sadly I don't know how that would be possible so I would be needing help! If you can help me in any way I would really apreciate it!
The biggest debate we need to complete on which is better, Waffles or Pancakes?
rux
How is it going? :D
Day 9 of giveaways! 8 prizes today :D

Forum statistics

Threads
106,049
Messages
1,018,547
Members
137,835
Latest member
yetisteven
Top