Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Loxias |
Hey folks,
Im trying to make a random item generator and want to use different art assets depending on the type of item is made. Each art asset is a grid with different sprites, so I want to first select the type of item, and then choose a random frame from that asset and use that for the sprite itself. Here is the code:
extends Area2D
var art_potions = preload("res://Assets/Art_Potion.tscn")
var art_medwep = preload("res://Assets/Art_MedWep.tscn")
onready var image = $Sprite
var possible_items = [
art_potions,
art_medwep
]
func _ready():
randomize()
roll_item_type()
func roll_item_type():
possible_items.shuffle()
print(possible_items[0])
image = add_child(possible_items[0].instance())
set_sprite()
func set_sprite():
var image_h_number = image.get_hframes()
var image_v_number = image.get_vframes()
var hframe = randi() % image_h_number
var vframe = randi() % image_v_number
var randframe = Vector2(hframe, vframe)
image.set_frame_coords(randframe)
the $Sprite is a sprite node bound to the item itself. it has the same art asset as art_potions. Changing the line from
onready var image = $Sprite
to
var image = null
gets me the same error.
The error is Invalid call. Nonexistent function ‘get_hframes’ in base ‘Nil’. The code works when the roll_item_type function is removed, so I guess the error comes from add_child, though I dont know how or why.
Any help would be appreciated.
Thanks
EDIT: The ‘art_potions’ and ‘art_medwep’ are bots scenes with only a Sprite node.