Skip to content

Commit 2736729

Browse files
committed
Add on_pre_pre and on_post_post signals to NativeRopeServer
This allows to hook in before RopeHandles or RopeAnchors execute. Also updated documentation and the rope_pulling example to make use of this feature to prevent a 1-frame lag.
1 parent 6a2886d commit 2736729

File tree

5 files changed

+17
-1
lines changed

5 files changed

+17
-1
lines changed

demo/addons/ropesim/RopeAnchor.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extends BaseRopeTool2D
33
class_name RopeAnchor
44

55
## Can be used to attach nodes at certain positions on a target rope.
6+
## Updates during the on_post_update signal of NativeRopeServer.
67

78
## Gets emitted just after applying the position. This happens always during _physics_process().
89
signal on_after_update()

demo/addons/ropesim/RopeHandle.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ extends BaseRopeTool2D
33
class_name RopeHandle
44

55
## Can be used to control, animate or fixate points on a target rope.
6+
## Updates during the on_pre_update signal of NativeRopeServer.
67

78
## Gets emitted just before applying the position. This happens always during _physics_process().
89
signal on_before_update()

demo/addons/ropesim/RopeToolHelper.gd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ class_name RopeToolHelper
55
## It contains boilerplate for registering/unregistering to/from NativeRopeServer when needed.
66

77
const UPDATE_HOOK_POST = "on_post_update"
8+
const UPDATE_HOOK_POST_POST = "on_post_post_update"
89
const UPDATE_HOOK_PRE = "on_pre_update"
10+
const UPDATE_HOOK_PRE_PRE = "on_pre_pre_update"
911

1012
## Emitted when the assigned rope has been changed, i.e. to a new rope or null.
1113
signal on_rope_assigned(old: Rope)

demo/rope_examples/scripts/character_body_2d.gd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ extends CharacterBody2D
44
@export var speed: float = 300.0
55

66

7-
func _physics_process(_delta: float) -> void:
7+
func _ready() -> void:
8+
# To prevent a 1-frame lag, we connect to the pre_pre event, so the movement code always runs
9+
# before RopeHandles, RopeAnchors and the rope simulation itself.
10+
# It is not necessary to do this instead of just using _physics_process() but it should
11+
# demonstrate how to solve the lag.
12+
NativeRopeServer.on_pre_pre_update.connect(_update)
13+
14+
15+
func _update() -> void:
816
var wishdir := Vector2()
917

1018
if use_arrow_keys:

src/NativeRopeServer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ void NativeRopeServer::_bind_methods()
4646
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "update_in_editor"), "set_update_in_editor", "get_update_in_editor");
4747
ADD_SIGNAL(MethodInfo("on_post_update"));
4848
ADD_SIGNAL(MethodInfo("on_pre_update"));
49+
ADD_SIGNAL(MethodInfo("on_post_post_update"));
50+
ADD_SIGNAL(MethodInfo("on_pre_pre_update"));
4951
}
5052

5153
void NativeRopeServer::register_rope(Node2D* rope)
@@ -119,6 +121,7 @@ void NativeRopeServer::_start_stop_process()
119121

120122
void NativeRopeServer::_on_physics_frame()
121123
{
124+
emit_signal("on_pre_pre_update");
122125
emit_signal("on_pre_update");
123126
const double delta = _tree->get_root()->get_physics_process_delta_time();
124127
NativeRopeContext context;
@@ -140,6 +143,7 @@ void NativeRopeServer::_on_physics_frame()
140143

141144
_last_time = (float)(Time::get_singleton()->get_ticks_usec() - start) / 1000.f;
142145
emit_signal("on_post_update");
146+
emit_signal("on_post_post_update");
143147
}
144148

145149
float NativeRopeServer::get_computation_time() const

0 commit comments

Comments
 (0)