(原) Godot快速恢复

原创文章,请后转载,并注明出处。

之所以标题叫快速恢复,是因为有一段时间没使用了,再加上版本的升级。所以需要快速过一遍。也就意味着以下信息不是完整的学习内容和顺序。

enum { COLD, WARM, HOT } 枚举
var score: init = 0
var my_int := 8
float、bool、String  
! && ||
~ << >> ^ | &=
next_state = "idle" if is_on_floor() else "fall"

函数指定返回类型:

func t(n) -> int:
	return n * 2
var x = [value] if [expression] else [value]    三元假设表达式

循环:

for n in 8:
    print(n)
for n in range(8):
    print(n)
for n in range(10,13):
    print(n)
for n in range(10,0,-1):
    print(n)
for ch in "Hello":
    print(ch)
for x in [3,6,8,9]:
    print(x)
var dict = { "x": 1, "y": 2, "z": 3 }
for key in dict:
    # Insert the key and value into a text string
    print("index: %s, value: %d" % [key, dict[key]])
while fuel > 0:
    speed += 0.12
    fuel -= 1

数组:

var a = Array()
var b = []
var c = ["a","b","c"]
a.append("Item 1")
print(b.size())
print(a[0])
c.shuffle() # 打乱数组'c'的值

字典:

var game = {}
var player = {
	"name": "Thor",
	"inventory": ["sword", "shield", "map"],
	"location": "Castellion",
	"energy": 67
}
# 添加
if game.empty():
	game["player"] = player
	game["score"] = 0
	game["dummy"] = null
	
# 删除
if game.has("dummy"):
	game.erase("dummy")
	
print(game.get("dummy", "Key not found!"))
	
if game.has_all(["player", "score"]):
	print(game["player"]["name"])

print(game.keys().size())
print(game.size())
print(player.values()[0])

类:

class_name Motorcycle extends Node2D  # 扩展类
# 添加属性
export(String) var make = "Kawasaki"
export(int) var cc = 900
export(Color, RGB) var color = ColorN("Ninja Green")

正则式:

var txt = "abc xyz abcdefg"
var regex = RegEx.new()
regex.compile("abc")

var result = regex.search(txt)
if result:
    print(result.get_string()) # prints abc

result = regex.search_all(txt)
if result:
    print(result) # prints an array of the search matches

简单教程 官方资料

更改Win的应用程序图标

创建包含各个尺寸长度的icon文件:magick convert icon.png -define icon:auto-resize=256,128,64,48,32,16 icon.ico
ImageMagick我是下载的便携版

后台加载

这里在ready加载一次,在press再加载一次,不知道是何意。仿佛是先进入加载线程进行请求,在后台先加载着备用。

const ENEMY_SCENE_PATH : String = "Enemy.tscn"

func _ready():
    ResourceLoader.load_threaded_request(ENEMY_SCENE_PATH)
    self.pressed.connect(_on_button_pressed)

func _on_button_pressed(): # Button was pressed
    var enemy_scene = ResourceLoader.load_threaded_get(ENEMY_SCENE_PATH)
    var enemy = enemy_scene.instantiate()
    add_child(enemy)

自包含模式

即不在系统其它目录中保存godot相关数据,而在当前目录editor_data下保存:在当前目录建立 .sc 文件或 sc 文件。

多线程

var thread: Thread

func _ready():
    thread = Thread.new()
    thread.start(_thread_function.bind("Wafflecopter"))


func _thread_function(userdata):
    print("I'm a thread! Userdata is: ", userdata)


func _exit_tree():
    thread.wait_to_finish()

等待信号与协程 wait

等待按键的抬起信号,再执行后续代码。可惜并不能实现:仅按下,不抬起(没在按钮上抬起)

func wait_confirmation():
    print("Prompting user")
    await $Button.button_up # Waits for the button_up signal from Button node.
    print("User confirmed")
    return true

如此,调用者就成为一个协程,需要等待

func request_confirmation():
    print("Will ask the user")
    var confirmed = await wait_confirmation()
    if confirmed:
        print("User confirmed")
    else:
        print("User cancelled")

@export

@export var number: int = 5
@export var number: int
@export var resource: Resource
@export var node: Node

分组:
@export_group("My Properties")
@export var number = 3
子组:
@export_subgroup("Extra Properties")
@export var string = ""
@export var flag = false
别名:(标题栏显示的名称)
@export_category("Main Category")
@export var number = 3
@export var string = ""

@export_category("Extra Category")
@export var flag = false

参数作为路径:
@export_file var f
参数作为目录:
@export_dir var f
作为文件路径,自带筛选器:
@export_file("*.txt") var f

作为全局文件系统中 PNG 文件的路径:
@export_global_file("*.png") var tool_image
全局文件系统中目录的路径:
@export_global_dir var tool_dir


多行上进行编辑的字段:
@export_multiline var text

限制范围:
@export_range(0, 20) var i

允许从 -10 到 20 的浮点数,并将值捕捉为 0.2 的倍数
@export_range(-10, 20, 0.2) var k: float

@export_range(0, 100, 1, "or_greater", "or_less")  ?
带图提示的浮点数:
@export_exp_easing var transition_speed
颜色:
@export var col: Color
颜色无alpha:
@export_color_no_alpha var col: Color
节点:
@export var node: Node
@export var some_button: BaseButton
资源:
@export var resource: Resource

@export var resource: AnimationNode
多选标记:
@export_flags("Fire", "Water", "Earth", "Wind") var spell_elements = 0
多选标记,带名称的值:
@export_flags("Self:4", "Allies:8", "Foes:16") var spell_targets = 0
枚举单选:
enum NamedEnum {THING_1, THING_2, ANOTHER_THING = -1}
@export var x: NamedEnum
@export_enum("Warrior", "Magician", "Thief") var character_class: int
@export_enum("Slow:30", "Average:60", "Very Fast:200") var character_speed: int
@export_enum("Rebecca", "Mary", "Leah") var character_name: String
@export_enum("Rebecca", "Mary", "Leah") var character_name: String = "Rebecca"
数组:
@export var a = [1, 2, 3]
@export var ints: Array[int] = [1, 2, 3]
@export var two_dimensional: Array[Array] = [[1.0, 2.0], [3.0, 4.0]]
2D导航图层的位标志:
@export_flags_2d_navigation var navigation_layers: int
2D物理图层的位标志:
@export_flags_2d_physics var physics_layers: int
2D渲染图层的位标志:
@export_flags_2d_render var render_layers: int

其它@

就緒时分配:
@onready var character_name: Label = $Label
@tool() 脚本标记为工具脚本,允许编辑器加载和执行

函数

ceil() 向上舍入
floor() 向下舍入
round() 四舍五入
clamp() 夹紧,返回两个值的中间变体
deg_to_rad() 角度转弧度
hash() 变体的哈希值
是否为正无穷大或负无穷大 is_inf()
是否为有限值 is_finite()
是否NaN is_nan()
线性插值 lerp()
最大值 max()
最小值 min()
按数移动 move_toward()
pingpong()?
富文本输出 print_rich()
不换行输出 printraw()
多个输出间带间格 prints()
多个输出间带制表符 printt()
随机数 rand_from_seed()
随机浮点数 randf_range(0, 20.5)
随机整数 randi()
指定范围随机整数 randi_range(-10, 1000)
用时间作为随机种 randomize()
手动随机种 seed()
变量类型 typeof()
变量值限定在最大与最小之间 wrap()

相关文章