类型推断
如果编写冒号,Godot将尝试推断类型,但省略了类型:
1 | var life_points := 4 |
数组
1 | var array = [10, "hello", 40, 60] # Simple, and can mix types |
在动态类型语言中,数组也可以与其他数据类型(如列表)同时使用:
1 | var array = [] |
或无序集:
1 | var a = 20 |
字典
字典示例:
1 | var d = {"name": "John", "age": 22} # Simple syntax |
字典也是动态的,可以在任何地方添加或删除键,成本很低:
1 | d["mother"] = "Rebecca" # Addition |
遍历
支持数组,字典和字符串的遍历。
1 | for s in strings: |
容器数据类型(数组和字典)是可重复的。字典允许迭代键:
1 | for key in dict: |
也可以使用索引进行迭代:
1 | for i in range(strings.size()): |
range()函数可以接受3个参数:
1 | range(n) # Will go from 0 to n-1 |
正向遍历
1 | for i in range(10): |
反向遍历
1 | for i in range(10, 0, -1): |
字符串
单个占位符
1 | # Define a format string with placeholder '%s' |
方式2
1 | # Define a format string |
多个占位符
1 | var format_string = "%s was reluctant to learn %s, but now he enjoys it." |
混合
1 | "Hi, {0} v{version}".format({0:"Godette", "version":"%0.2f" % 3.114}) |
类与对象
实例化
1 | AudioStreamPlayer.new() |
类型转换
1 | Sprite mySprite = GetNode("MySprite") as Sprite; |