Godot Version
v4.6.2.stable.steam [71f334935]
Question
I’m working on a feature in a game, and I did my usual copy-paste into ChatGPT to see if the code was understandable. Here’s the code:
extends Node2D
@onready var positive_line_2d: Line2D = $"Positive Line2D"
@onready var negative_line_2d: Line2D = $"Negative Line2D"
var x_pos: float = 0.0
var x_neg: float = 0.0
var expression_string: String
var var_names: Array = ["x", "e"]
var var_values: Array = [0.0, exp(1)]
var evaluator = Expression.new()
var x_idx: int = var_names.find("x")
func evaluate_expression() -> float:
var result = evaluator.execute(var_values)
if evaluator.has_execute_failed() or not (typeof(result) in [TYPE_FLOAT, TYPE_INT]):
push_error("Expression evaluation failed")
return 0.0
return float(result)
func _ready() -> void:
expression_string = self.get_meta("expression")
expression_string = expression_string.replace("^", "**")
var err = evaluator.parse(expression_string, var_names)
if err != OK:
push_error("Expression parsing failed")
self.queue_free()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
var_values[x_idx] = x_pos
positive_line_2d.add_point(Vector2(x_pos, -evaluate_expression()))
var_values[x_idx] = x_neg
negative_line_2d.add_point(Vector2(x_neg, -evaluate_expression()))
x_pos += delta * 10
x_neg -= delta * 10
ChatGPT said that I need to clamp the domain, which I will implement later in the game. It also said I need to clamp the range to ensure rendering stability, which is what I’m confused about. Do large X and Y coordinates in a Line2D cause rendering problems?
I know they probably cause lag, trying to calculate large values, but I suggested I could clamp the value to [-10000, 10000]. My game’s viewport is 115.28x64.8, so this wouldn’t cause any clipping problems, but ChatGPT said that that wouldn’t reflect the real, unclamped graph. My understanding of maths is that there shouldn’t be a noticeable change.
What happens with large coordinates in a Line2D?