GDscript风格指南

(惯例感谢godot开发组~~·)

缩进

缩进类型:Tabs (编辑器默认)

缩进大小:4 (编辑器默认)

每个缩进级别必须大于包含它的代码块。

良好的:

for i in range(10): print("hello") 

糟糕的:

for i in range(10): print("hello") for i in range(10): print("hello") 

使用2缩进级别把延续的行和普通代码块区分开来。

良好的:

effect.interpolate_property(sprite, 'transform/scale', sprite.get_scale(), Vector2(2.0, 2.0), 0.3, Tween.TRANS_QUAD, Tween.EASE_OUT) 

糟糕的:

effect.interpolate_property(sprite, 'transform/scale', sprite.get_scale(), Vector2(2.0, 2.0), 0.3, Tween.TRANS_QUAD, Tween.EASE_OUT) 

空白行

Surround functions and class definitions with a blank line.

在函数内部使用一个空行来分隔逻辑部分。

扫描二维码关注公众号,回复: 4713295 查看本文章

一条语句一行

不要在一行上合并多个语句 。不要用单行条件语句 (除了三元运算符), C 程序员 !

良好的:

if position.x > width: position.x = 0 if flag: print("flagged") 

糟糕的:

if position.x > width: position.x = 0 if flag: print("flagged") 

避免不必要的圆括号

避免表达式和条件语句中的括号。除非对运算顺序有要求, 否则它们只会降低可读性。

良好的:

if is_colliding():
    queue_free()

糟糕的:

if (is_colliding()):
    queue_free() 

空格

总在运算符和逗号之后使用一个空格。避免在字典引用和函数调用,或创建 “列”时增加空格

良好的:

position.x = 5
position.y = mpos.y + 10 dict['key'] = 5 myarray = [4, 5, 6] print('foo') 

糟糕的:

position.x=5
position.y = mpos.y+10 dict ['key'] = 5 myarray = [4,5,6] print ('foo') 

决不要

x        = 100
y        = 100 velocity = 500 

命名约定

这些命名约定遵循 Godot 引擎风格。打破这些将使您的代码与内置的命名约定冲突, 这是丑陋的。

类与节点

使用PascalCase:extends KinematicBody

将类加载到常量或变量时:

const MyCoolNode = preload('res://my_cool_node.gd') 

函数与变量

使用 snake_case: get_node()

在虚拟方法(用户必须覆盖的函数)中,私有函数和私有变量之前加上一个下划线 (_) : func _ready()

信号

使用过去时态:

signal door_opened
signal score_changed

常量

使用CONSTANT_CASE,全部大写,用下划线 (_) 分隔单词

 const MAX_SPEED 200

猜你喜欢

转载自www.cnblogs.com/empist/p/10200362.html
今日推荐