Use static typing in all demos (#1063)

This leads to code that is easier to understand and runs
faster thanks to GDScript's typed instructions.

The untyped declaration warning is now enabled on all projects
where type hints were added. All projects currently run without
any untyped declration warnings.

Dodge the Creeps and Squash the Creeps demos intentionally don't
use type hints to match the documentation, where type hints haven't
been adopted yet (given its beginner focus).
This commit is contained in:
Hugo Locurcio
2024-06-01 12:12:18 +02:00
committed by GitHub
parent 8e9c180278
commit bac1e69164
498 changed files with 5218 additions and 4776 deletions

View File

@@ -2,13 +2,14 @@ extends Control
const TEST_ITEM_SKU = "my_in_app_purchase_sku"
@onready var alert_dialog = $AlertDialog
@onready var label = $Label
@onready var alert_dialog: AcceptDialog = $AlertDialog
@onready var label: Label = $Label
var payment = null
var test_item_purchase_token = null
var payment: Object = null
var test_item_purchase_token := ""
func _ready():
func _ready() -> void:
if Engine.has_singleton("GodotGooglePlayBilling"):
label.text += "\n\n\nTest item SKU: %s" % TEST_ITEM_SKU
@@ -42,21 +43,23 @@ func _ready():
show_alert('Android IAP support is not enabled.\n\nMake sure you have enabled "Custom Build" and installed and enabled the GodotGooglePlayBilling plugin in your Android export settings!\nThis application will not work otherwise.')
func show_alert(text):
func show_alert(text: String) -> void:
alert_dialog.dialog_text = text
alert_dialog.popup_centered_clamped(Vector2i(600, 0))
$QuerySkuDetailsButton.disabled = true
$PurchaseButton.disabled = true
$ConsumeButton.disabled = true
func _on_connected():
func _on_connected() -> void:
print("PurchaseManager connected")
payment.queryPurchases("inapp") # Use "subs" for subscriptions.
# Use "subs" for subscriptions.
payment.queryPurchases("inapp")
func _on_query_purchases_response(query_result):
func _on_query_purchases_response(query_result: Dictionary) -> void:
if query_result.status == OK:
for purchase in query_result.purchases:
for purchase: Dictionary in query_result.purchases:
# We must acknowledge all puchases.
# See https://developer.android.com/google/play/billing/integrate#process for more information
if not purchase.is_acknowledged:
@@ -68,70 +71,71 @@ func _on_query_purchases_response(query_result):
" debug message: ", query_result.debug_message)
func _on_sku_details_query_completed(sku_details):
for available_sku in sku_details:
show_alert(JSON.new().stringify(available_sku))
func _on_sku_details_query_completed(sku_details: Array) -> void:
for available_sku: Dictionary in sku_details:
show_alert(JSON.stringify(available_sku))
func _on_purchases_updated(purchases):
print("Purchases updated: %s" % JSON.new().stringify(purchases))
func _on_purchases_updated(purchases: Array) -> void:
print("Purchases updated: %s" % JSON.stringify(purchases))
# See _on_connected
for purchase in purchases:
# See `_on_connected()`.
for purchase: Dictionary in purchases:
if not purchase.is_acknowledged:
print("Purchase " + str(purchase.sku) + " has not been acknowledged. Acknowledging...")
payment.acknowledgePurchase(purchase.purchase_token)
if purchases.size() > 0:
if not purchases.is_empty():
test_item_purchase_token = purchases[purchases.size() - 1].purchase_token
func _on_purchase_acknowledged(purchase_token):
func _on_purchase_acknowledged(purchase_token: String) -> void:
print("Purchase acknowledged: %s" % purchase_token)
func _on_purchase_consumed(purchase_token):
func _on_purchase_consumed(purchase_token: String) -> void:
show_alert("Purchase consumed successfully: %s" % purchase_token)
func _on_connect_error(code, message):
func _on_connect_error(code: int, message: String) -> void:
show_alert("Connect error %d: %s" % [code, message])
func _on_purchase_error(code, message):
func _on_purchase_error(code: int, message: String) -> void:
show_alert("Purchase error %d: %s" % [code, message])
func _on_purchase_acknowledgement_error(code, message):
func _on_purchase_acknowledgement_error(code: int, message: String) -> void:
show_alert("Purchase acknowledgement error %d: %s" % [code, message])
func _on_purchase_consumption_error(code, message, purchase_token):
func _on_purchase_consumption_error(code: int, message: String, purchase_token: String) -> void:
show_alert("Purchase consumption error %d: %s, purchase token: %s" % [code, message, purchase_token])
func _on_sku_details_query_error(code, message):
func _on_sku_details_query_error(code: int, message: String) -> void:
show_alert("SKU details query error %d: %s" % [code, message])
func _on_disconnected():
func _on_disconnected() -> void:
show_alert("GodotGooglePlayBilling disconnected. Will try to reconnect in 10s...")
await get_tree().create_timer(10).timeout
payment.startConnection()
# GUI
func _on_QuerySkuDetailsButton_pressed():
payment.querySkuDetails([TEST_ITEM_SKU], "inapp") # Use "subs" for subscriptions.
func _on_QuerySkuDetailsButton_pressed() -> void:
# Use "subs" for subscriptions.
payment.querySkuDetails([TEST_ITEM_SKU], "inapp")
func _on_PurchaseButton_pressed():
var response = payment.purchase(TEST_ITEM_SKU)
func _on_PurchaseButton_pressed() -> void:
var response: Dictionary = payment.purchase(TEST_ITEM_SKU)
if response.status != OK:
show_alert("Purchase error %s: %s" % [response.response_code, response.debug_message])
func _on_ConsumeButton_pressed():
func _on_ConsumeButton_pressed() -> void:
if test_item_purchase_token == null:
show_alert("You need to set 'test_item_purchase_token' first! (either by hand or in code)")
return

View File

@@ -23,6 +23,10 @@ run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.2")
config/icon="res://icon.webp"
[debug]
gdscript/warnings/untyped_declaration=1
[display]
window/stretch/mode="canvas_items"

View File

@@ -1,34 +1,28 @@
extends Control
@export var target: NodePath
@export var min_scale = 0.1
@export var max_scale = 3.0
@export var one_finger_rot_x = true
@export var one_finger_rot_y = true
@export var two_fingers_rot_z = true
@export var two_fingers_zoom = true
@export var min_scale := 0.1
@export var max_scale := 3.0
@export var one_finger_rot_x := true
@export var one_finger_rot_y := true
@export var two_fingers_rot_z := true
@export var two_fingers_zoom := true
var base_state
var curr_state
var target_node
var base_state := {}
var curr_state := {}
# We keep here a copy of the state before the number of fingers changed to avoid accumulation errors.
var base_xform
var base_xform: Transform3D
func _ready():
base_state = {}
curr_state = {}
target_node = get_node(target)
@onready var target_node: Node = get_node(target)
func _gui_input(event):
func _gui_input(event: InputEvent) -> void:
# We must start touching inside, but we can drag or unpress outside.
# if not (event is InputEventScreenDrag or
# (event is InputEventScreenTouch and (not event.pressed or get_global_rect().has_point(event.position)))):
# return
var finger_count = base_state.size()
var finger_count := base_state.size()
if finger_count == 0:
# No fingers => Accept press.
@@ -61,7 +55,7 @@ func _gui_input(event):
elif event is InputEventScreenDrag:
if curr_state.has(event.index):
# Touching finger dragged.
var unit_drag = _px2unit(base_state[base_state.keys()[0]] - event.position)
var unit_drag := _px2unit(base_state[base_state.keys()[0]] - event.position)
if one_finger_rot_x:
target_node.global_rotate(Vector3.UP, deg_to_rad(180.0 * unit_drag.x))
if one_finger_rot_y:
@@ -87,29 +81,29 @@ func _gui_input(event):
curr_state[event.index] = event.position
# Compute base and current inter-finger vectors.
var base_segment = base_state[base_state.keys()[0]] - base_state[base_state.keys()[1]]
var new_segment = curr_state[curr_state.keys()[0]] - curr_state[curr_state.keys()[1]]
var base_segment: Vector3 = base_state[base_state.keys()[0]] - base_state[base_state.keys()[1]]
var new_segment: Vector3 = curr_state[curr_state.keys()[0]] - curr_state[curr_state.keys()[1]]
# Get the base scale from the base matrix.
var base_scale = Vector3(base_xform.basis.x.x, base_xform.basis.y.y, base_xform.basis.z.z).length()
var base_scale := Vector3(base_xform.basis.x.x, base_xform.basis.y.y, base_xform.basis.z.z).length()
if two_fingers_zoom:
# Compute the new scale limiting it and taking into account the base scale.
var new_scale = clamp(base_scale * (new_segment.length() / base_segment.length()), min_scale, max_scale) / base_scale
var new_scale := clampf(base_scale * (new_segment.length() / base_segment.length()), min_scale, max_scale) / base_scale
target_node.set_transform(base_xform.scaled(new_scale * Vector3.ONE))
else:
target_node.set_transform(base_xform)
if two_fingers_rot_z:
# Apply rotation between base inter-finger vector and the current one.
var rot = new_segment.angle_to(base_segment)
var rot := new_segment.angle_to(base_segment)
target_node.global_rotate(Vector3.BACK, rot)
# Finger count changed?
if base_state.size() != finger_count:
# Copy new base state to the current state.
curr_state = {}
for idx in base_state.keys():
for idx: int in base_state.keys():
curr_state[idx] = base_state[idx]
# Remember the base transform.
base_xform = target_node.get_transform()
@@ -117,6 +111,6 @@ func _gui_input(event):
# Converts a vector in pixels to a unitary magnitude,
# considering the number of pixels of the shorter axis is the unit.
func _px2unit(v):
var shortest = min(get_size().x, get_size().y)
func _px2unit(v: Vector2) -> Vector2:
var shortest := minf(get_size().x, get_size().y)
return v * (1.0 / shortest)

View File

@@ -17,6 +17,10 @@ run/main_scene="res://Main.tscn"
config/features=PackedStringArray("4.2")
config/icon="res://icon.webp"
[debug]
gdscript/warnings/untyped_declaration=1
[display]
window/stretch/mode="canvas_items"

View File

@@ -1,22 +1,22 @@
extends Node2D
func _process(_delta):
func _process(_delta: float) -> void:
# Keep redrawing on every frame.
queue_redraw()
func _draw():
func _draw() -> void:
# Get the touch helper singleton.
var touch_helper = get_node(^"/root/TouchHelper")
var touch_helper: Node = $"/root/TouchHelper"
# Draw every pointer as a circle.
for ptr_index in touch_helper.state.keys():
var pos = touch_helper.state[ptr_index]
var color = _get_color_for_ptr_index(ptr_index)
for ptr_index: int in touch_helper.state.keys():
var pos: Vector2 = touch_helper.state[ptr_index]
var color := _get_color_for_ptr_index(ptr_index)
color.a = 0.75
draw_circle(pos, 40.0, color)
# Just a way of getting different colors.
func _get_color_for_ptr_index(index):
var x = (index % 7) + 1
## Returns a unique-looking color for the specified index.
func _get_color_for_ptr_index(index: int) -> Color:
var x := (index % 7) + 1
return Color(float(bool(x & 1)), float(bool(x & 2)), float(bool(x & 4)))

View File

@@ -5,16 +5,19 @@ extends Node
# It also remaps the pointer indices coming from the OS to the lowest available to be friendlier.
# It can be conveniently setup as a singleton.
var state = {}
var state := {}
func _unhandled_input(event):
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventScreenTouch:
if event.pressed: # Down.
if event.pressed:
# Down.
state[event.index] = event.position
else: # Up.
else:
# Up.
state.erase(event.index)
get_viewport().set_input_as_handled()
elif event is InputEventScreenDrag: # Movement.
elif event is InputEventScreenDrag:
# Movement.
state[event.index] = event.position
get_viewport().set_input_as_handled()

View File

@@ -21,6 +21,10 @@ config/icon="res://icon.webp"
TouchHelper="*res://TouchHelper.gd"
[debug]
gdscript/warnings/untyped_declaration=1
[display]
window/stretch/mode="canvas_items"

View File

@@ -1,6 +1,5 @@
extends Node
# Below are a number of helper functions that show how you can use the raw sensor data to determine the orientation
# of your phone/device. The cheapest phones only have an accelerometer only the most expensive phones have all three.
# Note that none of this logic filters data. Filters introduce lag but also provide stability. There are plenty
@@ -10,66 +9,66 @@ extends Node
# these cubes to our phones orientation.
# This is a 3D example however reading the phones orientation is also invaluable for 2D
# This function calculates a rotation matrix based on a direction vector. As our arrows are cylindrical we don't
# care about the rotation around this axis.
func get_basis_for_arrow(p_vector):
var rotate = Basis()
## Returns a rotation matrix based on a direction vector. As our arrows are cylindrical, we don't
## care about the rotation around this axis.
func get_basis_for_arrow(p_vector: Vector3) -> Basis:
var rotate := Basis()
# as our arrow points up, Y = our direction vector
# As our arrow points up, Y = our direction vector.
rotate.y = p_vector.normalized()
# get an arbitrary vector we can use to calculate our other two vectors
var v = Vector3(1.0, 0.0, 0.0)
# Get an arbitrary vector we can use to calculate our other two vectors.
var v := Vector3(1.0, 0.0, 0.0)
if abs(v.dot(rotate.y)) > 0.9:
v = Vector3(0.0, 1.0, 0.0)
# use our vector to get a vector perpendicular to our two vectors
# Use our vector to get a vector perpendicular to our two vectors.
rotate.x = rotate.y.cross(v).normalized()
# and the cross product again gives us our final vector perpendicular to our previous two vectors
# And the cross product again gives us our final vector perpendicular to our previous two vectors.
rotate.z = rotate.x.cross(rotate.y).normalized()
return rotate
# This function combines the magnetometer reading with the gravity vector to get a vector that points due north
func calc_north(p_grav, p_mag):
## Combines the magnetometer reading with the gravity vector to get a vector that points due north.
func calc_north(p_grav: Vector3, p_mag: Vector3) -> Vector3:
# Always use normalized vectors!
p_grav = p_grav.normalized()
# Calculate east (or is it west) by getting our cross product.
# The cross product of two normalized vectors returns a vector that
# is perpendicular to our two vectors
var east = p_grav.cross(p_mag.normalized()).normalized()
# is perpendicular to our two vectors.
var east := p_grav.cross(p_mag.normalized()).normalized()
# Cross again to get our horizon aligned north
# Cross again to get our horizon-aligned north.
return east.cross(p_grav).normalized()
# This function creates an orientation matrix using the magnetometer and gravity vector as inputs.
func orientate_by_mag_and_grav(p_mag, p_grav):
var rotate = Basis()
## Returns an orientation matrix using the magnetometer and gravity vector as inputs.
func orientate_by_mag_and_grav(p_mag: Vector3, p_grav: Vector3) -> Basis:
var rotate := Basis()
# as always, normalize!
# As always, normalize!
p_mag = p_mag.normalized()
# gravity points down, so - gravity points up!
# Gravity points down, so - gravity points up!
rotate.y = -p_grav.normalized()
# Cross products with our magnetic north gives an aligned east (or west, I always forget)
# Cross products with our magnetic north gives an aligned east (or west, I always forget).
rotate.x = rotate.y.cross(p_mag)
# And cross product again and we get our aligned north completing our matrix
# And cross product again and we get our aligned north completing our matrix.
rotate.z = rotate.x.cross(rotate.y)
return rotate
# This function takes our gyro input and update an orientation matrix accordingly
# The gyro is special as this vector does not contain a direction but rather a
# rotational velocity. This is why we multiply our values with delta.
func rotate_by_gyro(p_gyro, p_basis, p_delta):
var rotate = Basis()
## Takes our gyro input and updates an orientation matrix accordingly.
## The gyro is special as this vector does not contain a direction but rather a
## rotational velocity. This is why we multiply our values with delta.
func rotate_by_gyro(p_gyro: Vector3, p_basis: Basis, p_delta: float) -> Basis:
var rotate := Basis()
rotate = rotate.rotated(p_basis.x, -p_gyro.x * p_delta)
rotate = rotate.rotated(p_basis.y, -p_gyro.y * p_delta)
@@ -78,33 +77,33 @@ func rotate_by_gyro(p_gyro, p_basis, p_delta):
return rotate * p_basis
# This function corrects the drift in our matrix by our gravity vector
func drift_correction(p_basis, p_grav):
# as always, make sure our vector is normalized but also invert as our gravity points down
var real_up = -p_grav.normalized()
## Returns the basis corrected for drift by our gravity vector.
func drift_correction(p_basis: Basis, p_grav: Vector3) -> Basis:
# As always, make sure our vector is normalized but also invert as our gravity points down.
var real_up := -p_grav.normalized()
# start by calculating the dot product, this gives us the cosine angle between our two vectors
var dot = p_basis.y.dot(real_up)
# Start by calculating the dot product. This gives us the cosine angle between our two vectors.
var dot := p_basis.y.dot(real_up)
# if our dot is 1.0 we're good
# If our dot is 1.0, we're good.
if dot < 1.0:
# the cross between our two vectors gives us a vector perpendicular to our two vectors
var axis = p_basis.y.cross(real_up).normalized()
var correction = Basis(axis, acos(dot))
# The cross between our two vectors gives us a vector perpendicular to our two vectors.
var axis := p_basis.y.cross(real_up).normalized()
var correction := Basis(axis, acos(dot))
p_basis = correction * p_basis
return p_basis
func _process(delta):
# Get our data
var acc = Input.get_accelerometer()
var grav = Input.get_gravity()
var mag = Input.get_magnetometer()
var gyro = Input.get_gyroscope()
func _process(delta: float) -> void:
# Get our data from the engine's sensor readings.
var acc := Input.get_accelerometer()
var grav := Input.get_gravity()
var mag := Input.get_magnetometer()
var gyro := Input.get_gyroscope()
# Show our base values
var format = "%.05f"
# Show our base values.
var format := "%.05f"
%AccX.text = format % acc.x
%AccY.text = format % acc.y
@@ -122,37 +121,38 @@ func _process(delta):
%GyroY.text = format % gyro.y
%GyroZ.text = format % gyro.z
# Check if we have all needed data
# Check if we have all needed data.
if grav.length() < 0.1:
if acc.length() < 0.1:
# we don't have either...
# We don't have either...
grav = Vector3(0.0, -1.0, 0.0)
else:
# The gravity vector is calculated by the OS by combining the other sensor inputs.
# If we don't have a gravity vector, from now on, use accelerometer...
# If we don't have a gravity vector, from now on, use the accelerometer...
grav = acc
if mag.length() < 0.1:
mag = Vector3(1.0, 0.0, 0.0)
# Update our arrow showing gravity
# Update our arrow showing gravity.
$Arrows/AccelerometerArrow.transform.basis = get_basis_for_arrow(grav)
# Update our arrow showing our magnetometer
# Note that in absence of other strong magnetic forces this will point to magnetic north, which is not horizontal thanks to the earth being, uhm, round
# Update our arrow showing our magnetometer.
# Note that in absence of other strong magnetic forces this will point to magnetic north,
# which is not horizontal thanks to the earth being round.
$Arrows/MagnetoArrow.transform.basis = get_basis_for_arrow(mag)
# Calculate our north vector and show that
var north = calc_north(grav, mag)
# Calculate our north vector and show that.
var north := calc_north(grav, mag)
$Arrows/NorthArrow.transform.basis = get_basis_for_arrow(north)
# Combine our magnetometer and gravity vector to position our box. This will be fairly accurate
# but our magnetometer can be easily influenced by magnets. Cheaper phones often don't have gyros
# so it is a good backup.
var mag_and_grav = $Boxes/MagAndGrav
var mag_and_grav: MeshInstance3D = $Boxes/MagAndGrav
mag_and_grav.transform.basis = orientate_by_mag_and_grav(mag, grav).orthonormalized()
# Using our gyro and do a drift correction using our gravity vector gives the best result
var gyro_and_grav = $Boxes/GyroAndGrav
var new_basis = rotate_by_gyro(gyro, gyro_and_grav.transform.basis, delta).orthonormalized()
# Using our gyro and do a drift correction using our gravity vector gives the best result.
var gyro_and_grav: MeshInstance3D = $Boxes/GyroAndGrav
var new_basis := rotate_by_gyro(gyro, gyro_and_grav.transform.basis, delta).orthonormalized()
gyro_and_grav.transform.basis = drift_correction(new_basis, grav)

View File

@@ -50,8 +50,10 @@ environment = SubResource("1")
[node name="Control" type="Control" parent="."]
layout_mode = 3
anchors_preset = 0
offset_right = 1025.0
offset_bottom = 602.0
offset_left = 24.0
offset_top = 24.0
offset_right = 1049.0
offset_bottom = 626.0
size_flags_horizontal = 3
size_flags_vertical = 3

View File

@@ -18,6 +18,10 @@ run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.2")
config/icon="res://icon.webp"
[debug]
gdscript/warnings/untyped_declaration=1
[display]
window/stretch/mode="canvas_items"