Custom drawing


Found I could declare my scripts as a tool. That way I get previews of the custom _draw() code in my editor. As an example:

tool
extends Area2D
 
export (int) var speed = 200
export var velocity = Vector2()
var direction = 1
var MAIN_IMAGE = PoolVector2Array([
	Vector2(0, 50), 
	Vector2(10, 40), 
	Vector2(0, 30), 
	Vector2(10, 20), 
	Vector2(0, 10),
	Vector2(50, 0), 
	Vector2(100, 10), 
	Vector2(90, 20), 
	Vector2(100, 30), 
	Vector2(90, 40),
	Vector2(100, 50), 
	Vector2(50, 40), 
	Vector2(0, 50), 
	])

func _process(delta):
	if position.x > 600 - 100:
		direction = -1
	elif position.x < 0:
		direction = 1
	velocity.x = 1 * direction
	velocity = velocity.normalized() * speed
	position += velocity * delta
		
func _draw():
	# oddly draw_line (and Line2D nodes) don't have the weird skewing issue
	#draw_line(Vector2(0, 50), Vector2(50, 0), Color(255, 255, 0), 4, false)
	draw_polyline(MAIN_IMAGE, Color(0, 255, 0, 255), 4, false)

I’m still getting some weirdness on widths of polylines but it doesn’t look too bad so I’m not going down the rabbit hole yet.

For my Player node I use this construct:

func get_input():
	if not Engine.editor_hint:

so that I get the in editor preview but no errors on the get_input call.

Leave a comment

Log in with itch.io to leave a comment.