Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

My objective is, if I type "points" it will show me the content of the variable but if I add an int after it, it will change the value of the variable

This is what I have right now:

func add_message(text):
    chatLog.bbcode_text += '
'
    chatLog.bbcode_text += text

func text_entered(text):
    if text == "points":
        chatLog.bbcode_text = ''
        add_message("Current value of [color=yellow]Points: [/color]" + str("[color=fuchsia]" + str(points) + "[/color]"))
        inputField.text = ''

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

You can take the input text and split it into words with split. Then take them one by one:

func text_entered(text):
    var tokens = text.split(" ");
    var count = tokens.size();
    if count == 0:
        # (empty input)
        return; # or whatever

    if count > 0 and tokens[0] == "points":
        if count > 1 and tokens[1].is_valid_integer():
            # points int
            var integer = tokens[1].to_int();
            points = points + integer; # or whatever
        elif count > 1:
            # points invalid_int
            pass; # or whatever
        else:
            # points
            pass; # or whatever
    else:
        pass; # not_points

In the above code we are using is_valid_integer to verify that the current word is an integer, and if so we can convert it with to_int.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...