Last active
January 5, 2024 16:21
-
-
Save anthonyec/23e737eaf0e76ce5e86786de27fd8867 to your computer and use it in GitHub Desktop.
Little Godot things to remember that I forget after a few months away
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How to do setters and getters in Godot 4: | |
var size: int: set = set_size | |
var forward: Vector3: get = get_forward | |
var files: Array: get = get_files, set = set_files # I think | |
# Getting the current active camera. | |
var main_camera: Camera3D = get_viewport().get_camera_3d() | |
# Direction based of keyboard input. | |
var input_direction: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_backward") | |
# Get direction relative to camera rotation. | |
func transform_direction_to_camera_angle(direction: Vector3) -> Vector3: | |
var camera = get_viewport().get_camera_3d() | |
var camera_angle_y = camera.global_transform.basis.get_euler().y | |
return direction.rotated(Vector3.UP, camera_angle_y) | |
# Iterate backwards over an array | |
for index in range(arr.size() - 1, -1, -1): | |
var item = item[index] | |
# Like `look_at` but only on the Y axis. | |
# TODO: Add `speed` argument here to lerp rotation over time? | |
func face_towards(target: Vector3, speed: float = 0.0, delta: float = 0.0) -> void: | |
if global_transform.origin == target: | |
return | |
if is_zero_approx(speed): | |
look_at(target, Vector3.UP) | |
global_rotation.x = 0 | |
global_rotation.z = 0 | |
else: | |
# From: https://github.com/JohnnyRouddro/3d_rotate_direct_constant_smooth/blob/master/rotate.gd | |
var direction = global_transform.origin.direction_to(target) | |
global_rotation.y = lerp_angle(rotation.y, atan2(-direction.x, -direction.z), speed * delta) | |
# Check if resource exists. Use this instead of `FileAccess.file_exists` because it will work even in | |
# exported projects where the `.import` files may get in the way or something | |
ResourceLoader.exists("res://path/to/resource") | |
# Do string contact without joining strings together with `+`: | |
var resouce_path = "res://dialogue/%s.dialogue" % "hello" | |
# Get display scale, good for scaling stuff on retina displays: | |
DisplayServer.screen_get_scale() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment