ebiten icon indicating copy to clipboard operation
ebiten copied to clipboard

1 frame delay in AppendInputChars when compared to inpututil

Open tinne26 opened this issue 4 years ago • 0 comments

AppendInputChars seems to be notifying new key presses one frame later than what can be achieved with inpututil.IsKeyJustPressed, which can create conflicts when using the two and makes typing a bit laggier than it should be.

Take the following example program:

package main
import "github.com/hajimehoshi/ebiten/v2"
import "github.com/hajimehoshi/ebiten/v2/inpututil"
import "log"

func main() { ebiten.RunGame(&GameButNotReally{ make([]rune, 666) }) }
type GameButNotReally struct { myName []rune }
func (self *GameButNotReally) Layout(int, int) (int, int) { return 400, 300 }
func (self *GameButNotReally) Draw(*ebiten.Image) { /* not an artist */ }
func (self *GameButNotReally) Update() error {
   preLen := len(self.myName)
   self.myName = ebiten.AppendInputChars(self.myName)
   if len(self.myName) != preLen {
      log.Printf("my name is: " + string(self.myName))
      return nil
   }

   // custom handling for random keys
   if inpututil.IsKeyJustPressed(ebiten.KeyA) {
      log.Printf("play audio")
      return nil
   }
   if inpututil.IsKeyJustPressed(ebiten.KeyF) {
      log.Printf("switch fullscreen")
      ebiten.SetFullscreen(!ebiten.IsFullscreen())
      return nil
   }
   return nil
}

If we run it and type "arf", the result is the following:

2022/04/04 12:10:39 play audio
2022/04/04 12:10:39 my name is: a
2022/04/04 12:10:39 my name is: ar
2022/04/04 12:10:39 switch fullscreen

While one would expect "arf" to be fully detected first, and only then fullscreen being applied. Notice also that the "f" key is never detected by AppendInputChars (maybe as a side effect of the frame delay and the interruption by the fullscreening, hard to tell).

tinne26 avatar Apr 04 '22 10:04 tinne26