• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

help with user text input

Dubbsta

Active member
Joined
Jul 13, 2017
Messages
208
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:
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
 
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:
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
 
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:
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.

Code:
' 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
 
Back
Top Bottom