How can import textures to a script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Savio

HI, Im a new godot user, still learning some things.

I had this project I wanted to do for training, it’s basicaly a little hell bullet, ya know, a plane and projectiles coming directly at it. But now, Im having trouble doing the plane’s bullet

So heres the codes I tried:

Okay, so I basically needed to create an Area2D, with a collider and a sprite as it’s sons, at first I thought i had, so I did this:

if Input.is_action_just_released("Shot"):
	var bullet = Area2D.new()
	bullet.transform.origin = Vector2(position.x + 1000, position.y)
	self.add_child(bullet)
	 
	
	var spr_bullet = Sprite.new()
	var tex_bullet = Texture.new()
    tex_bullet.load("res://bullet.png")
	spr_bullet.set_texture(tex_bullet)
	bullet.add_child(spr_bullet)
	var coll_bullet = CollisionShape2D.new()
	coll_bullet.shape = RectangleShape2D.new()
	bullet.add_child(coll_bullet)
	bullet.position += Vector2(2, 0)

When I tried that though, the console gave me an error saying that i could not use the load() method on a texture. I tried using the export in the texture var, but i couldn’t get it to work

So, how do I get an png file in var? Any answer is welcome.

:bust_in_silhouette: Reply From: Inces

You want to set texture of a sprite with a png file.
What You did is force the texture tu load an image.
All You need to do is :

var sprbullet = Sprite.new()
sprbullet.texture = load("res://bullet.png")

Hey, thanks for the info! Really helped me a lot.

Savio | 2021-10-12 20:33