Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | michal14 |
I would like to create board like in monopoly game.
I created Path2D with 40 closed points, and I added PathFollow2D as child. Problem is, because eg. If I’m on 35 field and I would like move to 5 field, PathFolllow2D is backwards instead go forward.
Video: https://streamable.com/0axm8d
Here is my code:
extends Node2D
@onready var path_2d := $Path2D as Path2D
@onready var path_follow_2d := $Path2D/PathFollow2D as PathFollow2D
var current_field = 0
func move(amount: int):
if (current_field + amount) < 39:
current_field += amount
else:
current_field = abs(40 - (current_field + amount))
print("Field: %s" % current_field)
var offset = path_2d.curve.get_closest_offset(path_2d.curve.get_point_position(current_field))
var tween = get_tree().create_tween()
tween.tween_property(path_follow_2d, "progress", offset, 1.2).set_trans(Tween.TRANS_SINE)
How can I fix this?
Is the loop
property set to true
on your PathFollow2D
?
jgodfrey | 2023-01-07 15:38
Yes. I have loop property with value true.
michal14 | 2023-01-07 15:39
Oh, I see your code is intentionally preventing a “wrap” around the end point. Assuming you do have loop=true
(as mentioned above), what happens if you change your move()
method to simply add the passed amount
without modifying the result?
I think I’d expect that to behave as you intend, though maybe that causes you other problems?
jgodfrey | 2023-01-07 15:41
Untested, but I’d try something like this:
func move(amount: int):
current_field += amount
print("Field: %s" % current_field)
var offset = path_2d.curve.get_closest_offset(path_2d.curve.get_point_position(current_field))
var tween = get_tree().create_tween()
tween.tween_property(path_follow_2d, "progress", offset, 1.2).set_trans(Tween.TRANS_SINE)
current_field %= 40
That should allow values > 40 for the tween
, which (I think) should wrap as expected. Once that’s done, current_field
is again reduced to a value < 40…
jgodfrey | 2023-01-07 15:51
I have error:
E 0:00:12:0343 _on_roll_dice_pressed: Index p_index = 45 is out of bounds (points.size() = 41).
Because I have only 40 points.
michal14 | 2023-01-07 15:53
Is that error based on my first suggestion (to just keep adding amount
) or on the code I posted above - which should ensure the value is < 40? If you haven’t tried something like the above code, give it a try.
jgodfrey | 2023-01-07 15:56
When I just keep amount i have error like on above, because I have only 39 indexes on path2d.
michal14 | 2023-01-07 15:59
Sorry - It’s not clear whether you’ve tried the modified version of move()
I posted above? And, I’ll say that I HAVE NOT tried it…
jgodfrey | 2023-01-07 16:02
Yes, I tested your version and I have this error:
_on_roll_dice_pressed: Index p_index = 45 is out of bounds (points.size() = 41).
michal14 | 2023-01-07 16:04
It should be pretty easy to get the result you want. Is the project available anywhere for inspection?
jgodfrey | 2023-01-07 16:05
Yes, here:
Dropbox - Business+.zip - Simplify your life
Godot 4 beta 10
michal14 | 2023-01-07 16:06