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

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

godot - Change the variable if there is an int input

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 = ''

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...