Bullet trail with line2D

Godot Version

<stable4.2>

Question

<Trying to make bullet trail with line2D by following YT tutorial. Im facing an error "Expected statement, found "." instead."

extends Line2D

@export var limited_lifetime := false
@export var wildness := 3.0
@export var min_spawn_distance := 5.0
@export var gravity := Vector2.UP
@export var gradient_col : Gradient = Gradient.new()

var lifetime := [1.2, 1.6]
var tick_speed := 0.05
var tick := 0.0
var wild_speed := 0.1
var point_age := [0.0]
var stopped := false

@onready var tween := $Decay

func _ready():
	gradient = gradient_col
	set_as_top_level(true)
	clear_points()
	if limited_lifetime:
		stop()


func stop():
	stopped = true
	tween.interpolate_property(self, "modulate:a", 1.0, 0.0, randf_range(lifetime[0], lifetime[1]), Tween.TRANS_CIRC, Tween.EASE_OUT)
	tween.start()


func _process(delta):
	if tick > tick_speed:
		tick = 0
		for p in range(get_point_count()):
			point_age[p] += 5*delta
			var rand_vector := Vector2( randf_range(-wild_speed, wild_speed), randf_range(-wild_speed, wild_speed) )
			points[p] += gravity + ( rand_vector * wildness * point_age[p] )
		#if stopped:
			## This part is optional and only servers visual polishing purposes.
			## If a trail is stopped, and a very intense gradient is used, this part can be left in to change the
			## gradient of the line slowly towards a softer end.
			## Performance wise it's slower than the variant without this part, but it looks much better for glowing
			## trails like rockets with a longer life time
			#gradient.offsets[2] = clamp(gradient.offsets[2]+0.04, 0.0, 0.99)
			#gradient.offsets[1] = clamp(gradient.offsets[1]+0.04, 0.0, 0.98)
			#gradient.colors[2] = lerp(gradient.colors[2], gradient.colors[1], 0.1 )
			#gradient.colors[3] = lerp(gradient.colors[3], gradient.colors[0], 0.2 )
			#width += 3
	else:
		tick += delta


func add_point(point_pos:Vector2, at_pos := -1):
	if get_point_count() > 0 and point_pos.distance_to( points[get_point_count()-1] ) < min_spawn_distance:
		return
	
	point_age.append(0.0)
	.add_point(point_pos, at_pos)


func _on_Decay_tween_all_completed():
	queue_free()

This line is what’s causing the error. You only need to include the period if you have a specific node that you’re asking to use the add_point() function. Since this is inside the Line2D class itself, just remove the period.

if i remove the period, "method "add_point()" overrides a method from native class "line2d"

Ah, right.

So that’s a new issue, revealed by fixing the other. What’s going on here is that Line2D has a method called add_point() and the program does not know if you want to use that one, or the one just above that you created yourself that’s also called add_point(). As a result, you get a warning.

The fact that it’s “overriding” it simply means that you’re not going to get the original add_point() from Line2D because you’ve masked it by making your own in this script.

Without watching the tutorial, I can’t say if this is intentional or not. It’s possible that there was supposed to be something before the period that you removed and you missed that step of the tutorial. In fact, perhaps what’s supposed to be there is super.add_point(...). See Inheritance about calling a function in the original class.

tutorial said it is intentional to override

Then you just need to ignore the warning from removing the period. I did a quick test myself and I get this message:

“The method “apply_scale()” overrides a method from native class “Node2D”. This won’t be called by the engine and may not work as expected. (Warning treated as error.)”

So if you can’t ignore it, you need to change your warning setting to not treat it as an error. That’s in Project Settings > GDScript

1 Like

Thank you very much. It worked!

1 Like

sorry, one last question.
I get an error nonexistfunction interpolate_property
tween.interpolate_property(self, "modulate:a", 1.0, 0.0, randf_range(lifetime[0], lifetime[1]), Tween.TRANS_CIRC, Tween.EASE_OUT)

Tween.interpolate_property() is a Godot 3 function and not part of the class in 4.x

You need to use Tween.tween_property() now it seems.

still receive too many argument error

Well it’s a new function, so the number of arguments you need to pass to it is different than the old function. You need to read the documentation and try to figure out what you need to send into it versus the old version.