Godot 遊戲引擎指南/關鍵詞
- 關鍵詞
Godot 中的關鍵詞是 GDScript 中使用的特殊單詞。不幸的是,其中一些在 Godot 的類文件中沒有詳細記錄。
await 關鍵字將等待傳遞的訊號被髮出。
print(1) # Creates a 1 second timer await get_tree().create_timer(1).timeout print(2)
break 關鍵字結束一段迴圈程式碼。對於“永遠”迴圈很有用
var counter = 0 while true: counter += 1 if counter == 15: break
這將在程式碼執行 15 次後結束迴圈。
continue 關鍵字在迴圈內部使用。類似於 break,continue 跳過迴圈中的單個執行。就像中斷它一樣,但它不是結束整個迴圈,而是隻跳過單個迭代。
for num in range( 1,5 ): if num == 4: continue print( num )
以上執行 5 次。Num 是 1、2、3、4 然後 5。如果 Num 是 4,它“繼續”迴圈。否則它會列印。所以 4 沒有被列印,但 1、2、3 和 5 被列印了。
const 關鍵字可以在 var 的位置使用來建立一個常量,一個不能更改的值。
與變數不同,常量可以在 靜態函式 中使用。
enum DrawMode ={
PEN,
FILL,
ERASER,
LINE,
BOX,
SHADING,
CONTRAST,
BLEND
}
上面的程式碼是我曾經在 Godot 中製作的影像編輯器中的程式碼片段。它顯示了可能的繪製模式,例如線和填充,作為命名的整數,以便更輕鬆地除錯程式碼。
不要與匯出混淆。@export 關鍵字是一個非常強大的關鍵字。
它允許在 Inspector 面板中編輯值,並且它會按例項儲存更改。
對於基本的,嘗試 @export var max_health:int。
您甚至可以使用模板和範圍
# This allows a range of 0 to 100 with a step of 2 @export_range( 0, 100, 2 ) var number := 50 # This allows linking a file path @export_file() var file_path :String # This asks for a file path that ends with ".json" or ".md" @export_file(".json", ".md" ) var specific_extension_file_path :String # You can also try "String" with this one @export_enum( Number1, Number2, Number3 ) var dropdown_menu_int := 0 # This creates a slider to change the float with a step of 0.0001 @export ( 0, 1, 0.0001 ) var float_with_range := 0.5
extends 關鍵字告訴 Godot 指令碼繼承了給定物件的屬性。
它必須位於指令碼的最頂部。只有 tool 可以放在它之前。
for 關鍵字在給定值的每個專案之後執行它後面的縮排程式碼塊。
for i in ["Hello", "I", "am", "human", 1, 2, 3]: print( i )
通常在 if 語句 中使用。如果它前面的值在它後面的值內部,則它評估為真。它可以檢查
- 如果某個值在陣列中
- 如果某個鍵在字典中
- 如果一段文字包含另一段文字
- 如果某個物件包含具有相同名稱的屬性或常量
以及其他一些東西。"h" in "Hello" 將為假。“Hello” 中沒有小寫“h”。如果您希望進行不區分大小寫的搜尋,請在兩個值上呼叫 to_lower() 以將每個字母轉換為小寫(或 to_upper() 以轉換為大寫 - 全部大寫)。
通常在 if 語句中使用。它檢查它前面的值是否為它後面的型別。
print( "Hello" is String ) # True print( 52434 is int ) # True print( bool( "52434" ) is bool ) # True print( load( "res://icon.png" ) is StreamTexture ) # True print( load( "res://icon.png" ) is Texture ) # True
好吧,load 在技術上是在全域性作用域中描述的。但是,它被定義為一個關鍵字,所以如果它不在此處就沒有意義。
它在被呼叫時從您的檔案系統載入檔案。它接受一個引數:一個檔案路徑。
檔案路徑必須相對於您的專案資料夾或玩家儲存資料夾,並且不能指向外部!
有效示例
load( "res://assets/weapons/sword.png" ) load( "res://entities/chicken_egg.tscn" ) load( "user://custom_entities/mutant_monster.tscn" )
res 指向您的專案資料夾。user 指向玩家的儲存資料夾。這通常是%appdata%/roaming/godot/app_userdata/<project_name>,除非 ProjectSetting Config/Use Custom User Dir 和 Config/Custom User Dir Name 設定,在這種情況下,它是%appdata%/roaming/<ProjectSettings:Config/Custom User Dir Name>。

如果您在執行時使用 load 來載入檔案,您需要將
ProjectSettings.editor/export/convert_text_resources_to_binary 設定為 false,否則檔案可能無法在匯出的構建中按預期載入。master 關鍵字是 5 個多人遊戲關鍵字系列的一部分。如果使用它,它將使只有“master”呼叫函式。
remote master func take_damage( damage ): health -= damage rpc( "set_health", health )
您不希望在 puppet 上呼叫它,以幫助防止錯誤。
@onready
[edit | edit source]@onready 關鍵字位於 var 關鍵字之前。它允許使用動態數字或透過節點路徑獲取節點,並且在 _ready() 函式呼叫之前設定。
@onready var sword_node = $hand/sword
pass
[edit | edit source]pass 關鍵字用作空白程式碼行。在 "if" 語句或 "for" 語句之後或在函式宣告之後使用它,以作為 "程式碼塊"(以消除錯誤),而無需執行任何操作。這被稱為 "空" if 語句或 "存根函式"。
if true: pass
preload
[edit | edit source]參見 load。該檔案可以相對於呼叫它的指令碼或 "res"。指令碼載入時資源就會載入,防止遊戲執行時出現停頓。但是,檔案 **必須** 存在,否則會報錯,並且路徑 **必須** 是常量(即:不可改變)。
res://game/game.gd
preload( "entities/thing.tscn" )
或
preload( "res://game/entities/thing.tscn" )
puppet
[edit | edit source]puppet 關鍵字是五個多玩家關鍵字之一。在 "func" 之前但 "remote" 之後使用它。它使函式僅呼叫 非主控方。
remote puppet func set_health( v ): health = v
為了防止玩家作弊,你不會想將其呼叫到 主控方。
remote
[edit | edit source]在 "func" 關鍵字之前使用 remote 關鍵字,允許在呼叫 "rpc"、"rpc_id"、"rpc_unreliable" 或 "rpc_unreliable_id" 時,該函式在多玩家模式下被其他對等節點遠端呼叫。小心作弊和安全威脅!
func hit( ): health -= 1 rpc( "player_hit", health ) remote func player_hit( hp ): if get_tree().get_rpc_sender_id( ) != get_network_master( ): return # This means a player is trying to cheat health = hp
參見 puppet 和 master 瞭解更多資訊。使用 remotesync 在本地以及透過網路呼叫函式。
警告:如果你沒有新增檢查來阻止惡意攻擊者,惡意駭客可以使用你的遊戲來檢索或刪除資料,或者玩家可以作弊。除非你知道自己在做什麼,否則永遠不要將 remote 新增到刪除或建立檔案的函式中。
remotesync
[edit | edit source]參見 remote。此關鍵字還會在本地呼叫函式,而不是隻透過網路呼叫它們。非常適合告訴其他對等節點以及主控方角色已經跳躍。
return
[edit | edit source]return 關鍵字用於結束函式。對於被呼叫並 "返回" 值的函式,return 關鍵字是目標位置。
func get_some_value( ): return "some string"
它還結束函式呼叫,因此可以在 if 語句中使用它來跳過函式的其餘部分。它不需要返回值,預設情況下返回 null。
static
[edit | edit source]通常,要呼叫物件的函式,你需要它的例項。如果在 "func" 關鍵字之前使用 static 關鍵字,你將能夠從非例項中呼叫它。但是,你只能使用其他靜態函式或全域性範圍內的函式。你不能使用成員 變數。
在下面的例子中,Weapon 是一個自定義的 Resource。
- Weapon.gd
extends Resource class_name Weapon export var damage = 5 const DAMAGE = 5 static func get_damage( ): # Cannot use an outside variable here return DAMAGE # Can use a constant though
Weapon.new( ).get_damage( ) 變成了 Weapon.get_damage( ),如果要查詢值,可以節省記憶體和 CPU 資源。
@tool
[edit | edit source]@tool 關鍵字告訴 Godot 指令碼在編輯器中執行。這類指令碼通常被稱為工具指令碼。如果指令碼只在指令碼編輯螢幕中開啟,它將不會執行。它必須附加到場景中的節點,或者具有該指令碼的節點必須在編輯器中。
它必須始終放置在 extends 關鍵字之前。如果覺得方便,可以將其放置在與 extends 相同的程式碼行上。
如果你有一個工具指令碼,它繼承的指令碼以及繼承它的指令碼將不會執行,除非它們也是工具指令碼。所有節點本身都在編輯器中執行,但前提是它們被放置到帶有編輯器外掛的介面中。
var
[edit | edit source]var 關鍵字建立變數。當它們離開作用域時,它們會自動從記憶體中釋放(例如,縮排的程式碼塊取消縮排)。
var global_varible := 1 func _ready(): some_function(7) func some_function(argument): print(argument) print(global_variable) var temporary_variable = 2 print(temporary_variable) if true: var another_variable = 3 print(another_variable) print(temporary_variable) print(another_variable) # Error: the identifier "another_variable" isn't declared in the current scope, since it is not declared in an lower indentation.
while
[edit | edit source]while 關鍵字在它之後的 if 語句不再為真之前,執行它之後縮排的程式碼塊。如果它一開始是假的,它將不會執行。
var i = 0 while i < 50: print(i) i += 1

