Allow explicitly defining interfaces in GDScript
### Describe the project you are working on I'm working on a game where different entities can share similar behaviors, but this proposal is specific to GDScript, so it can apply to a wide range of projects. ### Describe the problem or limitation you are having in your project Let's say we're making a simple shooter game. The player can shoot a `Bullet`. Bullets can hit a `Ship` and `Building`. There are also `Tree` entities that cannot be hit by bullets. Here's some simple code (unrelated code omitted for simplicity). **Ship.gd** ```gdscript extends Node2D class_name Ship func die() -> void: print("Died") ``` **Building.gd** ```gdscript extends Node2D class_name Building func die() -> void: print("Died") ``` **Tree.gd** ```gdscript extends Node2D class_name Tree func die() -> void: print("Died") ``` **Bullet.gd** ```gdscript extends Node2D class_name Bullet func on_body_entered(body: Node2D) -> void: if body.has_method("die"): body.die() ``` There are a few problems with this duck-typing approach: 1. The function "die" is referenced as a string which is error-prone 2. In `Bullet.gd` there is no code auto-completion or checking when calling `body.die()` so you need to implicitly know that that function exists. 3. The mere presence of a function named `die` is not enough to determine what kind of object you're dealing with. In this example, we don't want bullets to hit `Tree`, even though a tree might happen to have a function named `die` (the tree c
Posted by nebs on Jul 14, 2022, 1:44 PM
Tags
Related issues
- struct's lifetime requirements can be bypassed via covariant field assignment
- Proposal to Reorganize the Moderation Team and Review Moderation Policies
- Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
- proposal: container/...: generic collection types
- Better Custom Endpoint BYOK
- A concrete policy for backward compatibility until Godot 5.0
Raise real-world problems, find people facing the same issue, and discuss solutions.2,500 issues0 comments
github (Other)
struct's lifetime requirements can be bypassed via covariant field assignment
Proposal to Reorganize the Moderation Team and Review Moderation Policies
Progressive FPS degradation appears to be related to dota2.com — blocking the domain prevents the performance loss
proposal: container/...: generic collection types
Better Custom Endpoint BYOK
A concrete policy for backward compatibility until Godot 5.0
New MTP models not supported
Tracking: 429 / Capacity Issues
Allow explicitly defining interfaces in GDScript
### Describe the project you are working on I'm working on a game where different entities can share similar behaviors, but this proposal is specific to GDScript, so it can apply to a wide range of projects. ### Describe the problem or limitation you are having in your project Let's say we're making a simple shooter game. The player can shoot a `Bullet`. Bullets can hit a `Ship` and `Building`. There are also `Tree` entities that cannot be hit by bullets. Here's some simple code (unrelated code omitted for simplicity). **Ship.gd** ```gdscript extends Node2D class_name Ship func die() -> void: print("Died") ``` **Building.gd** ```gdscript extends Node2D class_name Building func die() -> void: print("Died") ``` **Tree.gd** ```gdscript extends Node2D class_name Tree func die() -> void: print("Died") ``` **Bullet.gd** ```gdscript extends Node2D class_name Bullet func on_body_entered(body: Node2D) -> void: if body.has_method("die"): body.die() ``` There are a few problems with this duck-typing approach: 1. The function "die" is referenced as a string which is error-prone 2. In `Bullet.gd` there is no code auto-completion or checking when calling `body.die()` so you need to implicitly know that that function exists. 3. The mere presence of a function named `die` is not enough to determine what kind of object you're dealing with. In this example, we don't want bullets to hit `Tree`, even though a tree might happen to have a function named `die` (the tree c
Comments (0)
No comments yet. Start the discussion.