Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | primevolcano |
I was following this tutorial https://www.youtube.com/watch?v=RXIRkou021U&t=208s
and have been slowly converting it to 4.0.3 for my project
But im running into the same bug “Invalid get index ‘-1’ (on base: ‘Array’).”
here is the code where it breaks
func draw_water_body():
var curve = water_border.curve
var points = Array(curve.get_baked_points())
var water_polygon_points = points
var first_index = 0
var last_index = water_polygon_points.size()-1
water_polygon_points.append(Vector2(water_polygon_points[last_index].x, bottom))
water_polygon_points.append(Vector2(water_polygon_points[first_index].x, bottom))
This is the code for the water border
P.S. ‘Sorry its broken btw’
class_name SmoothPath
extends Path2D
@export var spline_length:float = 8
@export var _smooth: bool: set = smooth
@export var _straighten: bool: set = straighten
@export var color: Color = Color(1,1,1,1)
@export var width:float = 8
func straighten(value):
if not value: return
for i in curve.get_point_count():
curve.set_point_in(i, Vector2())
curve.set_point_out(i, Vector2())
func smooth(value):
if not value: return
var point_count = curve.get_point_count()
for i in point_count:
var spline = _get_spline(i)
curve.set_point_in(i, -spline)
curve.set_point_out(i, spline)
func _get_spline(i):
var last_point = _get_point(i - 1)
var next_point = _get_point(i + 1)
var spline = last_point.direction_to(next_point) * spline_length
return spline
func _get_point(i):
var point_count = curve.get_point_count()
i = wrapi(i, 0, point_count - 1)
return curve.get_point_position(i)
func _draw():
var points = curve.get_baked_points()
if points:
draw_polyline(points, Color.WHITE, 8, true)
Edited to fix forum code formatting.
jgodfrey | 2023-06-01 16:47
Just guessing, but I assume this:
var points = Array(curve.get_baked_points())
is returning an empty Array
.
If that’s the case, then this:
var last_index = water_polygon_points.size()-1
Will generate a last_index
value of -1
.
And then this will generate the error you reported:
water_polygon_points.append(Vector2(water_polygon_points[last_index].x, bottom))
If that’s correct, then you need to determine why get_baked_points()
isn’t returning anything. Are you sure the underlying curve is populated when that code is called?
jgodfrey | 2023-06-01 16:53