help with user text input

Dubbsta

Active Member
Joined
Jul 13, 2017
i cant figure out how to work input correctly. in the getchar example it never breaks it just keeps typing. im trying to get the number of frames or type names and use it. this is what my code looks like ...also how to do backspace/delete char thanks

Cerberus X:
Class Layer
 Field map:Map
 Field tileset:TileSet
 Field visible:Bool = True
 Field name:String
 Field frames:String
 Field last:Int
 Field dir:string
 Field filter:String = "Image Files:png,jpg,bmp"
 Field imgname:String
 Field numframes:int

             Method getimg:String( )
                 dir = RequestFile("select an image",filter)
                 last = dir.FindLast("\")+1
                 name = dir[last..]
                 Return name
             End
            
             Method getframes:int()
                 Local char:int = GetChar()
                     frames += String.FromChar(char)
                     'Return 1
                     return Int(frames)
             End
 
             Method createLayer:Void( )
                 imgname = getimg()
                numframes = getframes()
                If img <> Null
                
                map = New Map()
                 tileset = New TileSet("tiles/"+imgname ,numframes)
                
             End
              
              
             Method draw:Void()
                 If map <> Null
                 map.draw()
                 tileset.draw()
                End
                
             End
End
 

Holzchopf

Moderator
Joined
Jul 31, 2017
Location
Bern, Switzerland
Be aware that GetChar returns 0 if no keystrokes are in the queue. So you might reconsider your program flow (i.e. GetChar should be called in a loop, while createLayer and getimg only need to be called once).

Here's a mockup version of getframes - it's not a nice solution since the program is stuck in that method until you hit enter
Cerberus X:
Method getframes:int()
    Local char:Int
    Repeat
        char = GetChar()
        ' only add the char to the string when it's not 0
        ' (because otherwise a `~z` will be appended which is the "string ends here"
        ' character and everything you type lands behind that and won't ever be seen)
        ' to make things easier: only append chars with a code >= 32 (space) - below
        ' that are control chars
        If char >= 32 Then
            frames += String.FromChar(char)
        ' on backspace, remove one char
        ' (only if there's at least one char left)
        Elseif char = CHAR_BACKSPACE And frames.Length > 0 Then
            frames = frames[..-1]
        End
    Until char=CHAR_ENTER
    return Int(frames)
End
 

Dubbsta

Active Member
Joined
Jul 13, 2017
Pefect thanks holzchopf. I did keyhit key_enter.
Why is it bad the program is in method? What is a better? Thanks again

Edit: Createlayer is called on keyhit or icon click if that makes it better?
 
Last edited:

Holzchopf

Moderator
Joined
Jul 31, 2017
Location
Bern, Switzerland
Ah yes that makes it better! =)

You see, just putting that GetChar()-Routine into a loop halts the other functions of your program like rendering. Back in the Blitz-days there was a function called Input() which literally stopped your program until you hit enter. Which is not very nice if you want to be able to draw something while the user types in text. And by something I even mean "the text the user is typing". So writing a custom input routine, that sort of runs in parallel to the rest of your code, was always quite a common task.

generic input routine:
' flag to indicate whether the textfield listens to keystrokes or not
Global textfieldactive:Bool=False
' textfield value
Global textfieldstring:String=""

'--- in your main loop (actually in OnUpdate() ) ---

' activate textfield on approprate action, i.e:
If UserClicksInTextField() Then
    textfieldactive = True
End

' when textfield is active, let it treat keystrokes
Local char:Int = GetChar()
If textfieldactive Then
    If char >= 32 Then
        textfieldstring += String.FromChar(char)
    Elseif char = KEY_BACKSPACE And textfieldstring.Length > 0 Then
        textfieldstring = textfieldstring[..-1]
    ' only assign your target variable when the user means it (hits enter)
    Elseif char = KEY_ENTER Then
        whatevervar = textfieldstring
        WhatEverShouldHappenOnEnter()
        textfieldactive = False
    End
End
 
Top Bottom