I made a scene to hold an item in a players inventory (it includes an textureRect and a label) and when I instance it the first time it works just fine but when I create a second instance the texture is the correct texture (meaning there are 2 different textures showing, one in each instance) but the label does not appear at all on the second instance, and the label on the first instance displays the number passed in to the second instance. My first guess was that this is a consequence of scenes sharing resources but I have tried using duplicate to no avail and I've tried checking the "Local to scene" checkbox on the scene script itself and it doesn't seem to have an effect either. Is this a consequence of the fact that scenes share resources? (Although if this is the case why is the texture allowed to be different but the label doesn't even show up, much less show a unique value) Can anyone tell me why the label doesn't show up on the second instance?
Label missing on instances of scene
- Edited
xyz Ok, sorry for the delay. I'm new to this so it may be really bad but here is the scene that is being problematic:
and here is the code for it:
extends CenterContainer
var Item : BaseItem #this is a resource containing an image that I use, and a few values I dont use yet.
var Quantity : int = 1 #when created it must have at least 1 item.
func AddToQuantity(TempQuantity : int): #this function is for altering the quantity without having to create a new slot.
Quantity += TempQuantity #add to the quantity
$QuantityLabelBox/QuantityLabel.text = str(Quantity) #display the new quantity
func InitializeSlot(TempItem : BaseItem, TempQuantity : int): #called when slot is created
Item = TempItem #hold the resource
$InventoryImage.texture = Item.Icon #a texture to display
Quantity = TempQuantity #the quantity
$QuantityLabelBox/QuantityLabel.text = str(Quantity) #display quantity, this is the part that isn't working the way I intend
$QuantityLabelBox.layer = 2 #to make sure the label is displayed on top of the image
That is called by this scene:
and its code is here:
extends ScrollContainer
var InventorySlotsArray : Array #holds a copy of the inventory to display, not yet used
func AddSlot(InventoryItem : BaseItem, Quanity : int): #create a new slot
var SlotScene = preload("res://NewInventorySystem/InventorySlot.tscn").instance() #instance the inventory slot
var Slot = SlotScene.duplicate() #i am under the impression this should make each slot unique
Slot.InitializeSlot(InventoryItem, Quanity) #call the initialization for the slot
$InventorySlotsContainer.add_child(Slot) #add the slot to its container
SlotScene.queue_free() #this is to free the original slot from memory so it is not a leak
and finally those scenes are put together in the main scene of the group:
and the code for this scene:
extends ColorRect
#FOR TESTING
var TestItemZero = preload("res://Items & Resources/Potato.tres") #load in the resource for testing the scene
var TestITemsOne = preload("res://Items & Resources/Strawberry.tres")
#end testing
func CollectResource(TempItem : BaseItem, TempQuanity : int): #this should collect the resource and add to the appropriate slot or add a new slot
if $InventoryContainer/InventorySlots/InventorySlotsContainer.get_child_count() == 0: #for the first item just add it
$InventoryContainer/InventorySlots.AddSlot(TempItem, TempQuanity) #add a new slot for the item
else: #look for duplicates
var Duplicate : bool = false #if we find a duplicate, make this true
var NumberOfChildren = $InventoryContainer/InventorySlots/InventorySlotsContainer.get_child_count() #how many entries are there to look through
for N in NumberOfChildren:
if TempItem.Name == $InventoryContainer/InventorySlots/InventorySlotsContainer.get_child(N).Item.Name && TempItem.InherentValue == $InventoryContainer/InventorySlots/InventorySlotsContainer.get_child(N).Item.InherentValue:
Duplicate = true #if the item is already in the inventory, dont add another slot for it
$InventoryContainer/InventorySlots/InventorySlotsContainer.get_child(N).AddToQuantity(TempQuanity) #update the quantity for the item
break #dont sort the whole list once you find what you are looking for
if Duplicate == false:
$InventoryContainer/InventorySlots.AddSlot(TempItem, TempQuanity) #if no duplicate is found, add a new slot
func _ready(): #all this code is just for testing
var Temp = TestItemZero.duplicate()
var Temps = TestITemsOne.duplicate()
CollectResource(Temp, 3)
CollectResource(Temps, 8)
Also here is the resource code for the 2 resources I am testing this with:
Base Item code:
extends Resource
class_name BaseItem
export(String) var Name : String = ""
export(float) var InherentValue : float = 0 #value in copper pieces
export(float) var Weight : float = 0 #weight in pounds
export(Texture) var Icon : Texture #for the icon that will be used both in investory and on map
export(String, MULTILINE) var Description : String = ""
func GetName() -> String:
return Name
func GetInherentValue() -> float:
return InherentValue
func GetWeight() -> float:
return Weight
func GetIcon() -> Texture:
return Icon
func GetDescription() -> String:
return Description
and when I run it all I get this:
as you can see the image is right but the label is wrong for the first quantity (it should be 3) and the label is missing on the second instance all together. Both of these things confound me to no end.
Thanks again for looking at my post, I hope I provided all the info you requested in a logical and organized manner. If you can offer any help it would be appreciated! Thanks!
edit: the code snippits are all messed up and I have no idea why, I used the insert code button and then pasted my code it and it looks all unformatted except for a few spots where it correctly formatted the code, I apologize for this.
Gman99 edit: the code snippits are all messed up and I have no idea why, I used the insert code button and then pasted my code it and it looks all unformatted except for a few spots where it correctly formatted the code, I apologize for this.
I've fixed it. For future reference, you want to paste the code on a empty new line, ideally a empty line surrounded by empty lines itself, then highlight that code and only then press the code button to format it. Cause there's 2 ways it can format code, default is this kind of inline code formatting
that can be used in the middle of a sentence. The second is for code blocks. It defaults to the first, so if you don't have multi-line code block selected you get the formatting for the first.
Megalomaniak Thank you, Ill keep that in mind!
xyz Thanks for the reply, I changed the AddSlot function as you suggested but it did not fix the problem. Here is the AddSlot function now:
func AddSlot(InventoryItem : BaseItem, Quanity : int):
var SlotScene = preload("res://NewInventorySystem/InventorySlot.tscn").instance() #instance the inventory slot
$InventorySlotsContainer.add_child(SlotScene) #add the slot to its container
SlotScene.InitializeSlot(InventoryItem, Quanity) #call the initialization for the slot
Gman99 What happens if you only add one item? What happens if you add 3 items? What happens if you comment out code that checks for duplicates?
If the problem persists it'd be best to isolate it in a minimal separate project and upload it. More likely you'll get help with debugging if someone can actually run it and plant breakpoints to follow the execution flow.
Btw you have too many long repeating names that obfuscate the scene structure an make your code hard to read and follow. For example: InventoryScene/InventoryContainer/InventorySlots/InventorySlotsContainer
should be rearranged to something like: Inventory/Slots
If you must have deep nested structure, encapsulate it and provide access via neatly named methods.