Topic was automatically imported from the old Question2Answer platform.
Asked By
harrisonschultz
I instanced a scene like
extends Node
var itemScene = preload("res://Item.tscn")
func spawnItems(items, position, rootNode):
for item in items:
var instance = itemScene.instance()
rootNode.add_child(instance)
instance.init(item, position)
However I get the error "Invalid call. Nonexistent function “init” in base “Sprite”.
When I check my itemScene’s attached script there is a custom constructor
extends Sprite
var itemName
var rarity
var value
var type
var damage
var itemRoot
var identity
var toolTip
func init(details, worldPosition: Vector2):
itemName = details['itemName']
identity = Globals.Things.Item
rarity = details['rarity']
value = details['value']
type = details['type']
damage = details['damage']
var image = load(details['sprite'])
self.centered = false
self.texture = image
self.position = worldPosition
Movement.centerMe(self)
I don’t know what’s wrong, but until you figure it out try this:
In the Item.tscn scene’s script:
Define variables for details and worldPosition in the header.
Copy the contents of the init function to a _ready() function.
Remove/comment out the init function.
Change the other script to this:
extends Node
var itemScene = preload("res://Item.tscn")
func spawnItems(items, position, rootNode):
for item in items:
var instance = itemScene.instance()
instance.details = item
instance.worldPosition = position
rootNode.add_child(instance)
Thank you, however I not sure I fully understand this workaround. How would I populate the details variable if I cannot pass anything into the instance? I dont have access to any functions or variables created in the attached script. Its as if the script that is attached is not actually attached but I quadruple checked that it is.