• Godot HelpProgramming
  • Resources created from a script get some of its variables set to 0 once opened in the inspector

I wanted to move from my json data base to resources in order to keep track of metadata like status, durability, exp, etc. So I created to do the job for me. The resources are created and I can open then in the inspector all the variables have their values properly set, but when I click on another resource and go back to the first inspected resource all of a sudden all the variables of the type int except 1 durability get reseted?!

This is the Script for the Item resource:

extends Resource
class_name Item

enum SEASONS {SPRING,SUMMER,AUTUMN,WINTER}

export (String)  var name 
export (String)  var animation 
export (int)     var frame 
export (int)     var max_stack 
export (int)     var sell_price
export (int)     var buy_price
export (String)  var category 
export (Array)   var seasons
export (Array)   var recipes

export (int)     var amount    
export (int)     var experience
export (int)     var durability

And this is the script to create the resources.

func create_resources():
    var items_data = DBase.Items

	for key in items_data:

		var new_resource = Item.new()

		new_resource.name        = key
		new_resource.sell_price  = items_data[key][DBase.SELL_PRICE]
		new_resource.buy_price   = items_data[key][DBase.BUY_PRICE]
		new_resource.max_stack   = items_data[key][DBase.MAX_STACK]
		new_resource.animation   = items_data[key][DBase.ANIMATION]
		new_resource.frame       = items_data[key][DBase.FRAME]
		new_resource.category    = items_data[key][DBase.CATEGORY]

		if items_data[key][DBase.RECIPES] != null:
			new_resource.recipes = items_data[key][DBase.RECIPES]
		else:
			new_resource.recipes = []
		
		if items_data[key][DBase.SEASON] != null:
			new_resource.seasons = items_data[key][DBase.SEASON]
		else:
			new_resource.seasons = []

		new_resource.amount      = 20
		new_resource.experience  = 10
		new_resource.durability  = 100
		
		ResourceSaver.save(str("res://ZZ_Resources/new_resources/"+key+".tres"),new_resource)

If I press on the resource once and with the values properly set I click on save resource on the inspector it gets the same variables set to 0

The variables set to 0 are all integers:

export (int)     var frame 
export (int)     var max_stack 
export (int)     var sell_price
export (int)     var buy_price

But there's some integers that don't get reseted among other variables:

export (String)  var name 
export (String)  var animation
export (String)  var category 
export (Array)   var seasons
export (Array)   var recipes 
export (int)     var amount    
export (int)     var experience
export (int)     var durability

I would also like to point out that there seem to be some kind of problem when setting the variables because I used to have: export (SEASONS) var seasons and this didn't pop an error when trying to set its value to [] when it should right ?

Hmm, strange that it is only happening for the integers. Does replacing int with float solve the issue of the resource not being saved? This would not be a solution, but it would help narrow down if the issue is just integer related or not.

@TwistedTwigleg I didn't try that xd and It did work! Apparently json files only use floats for numbers.

So the problem comes with the resource not enforcing its variable types I would have saved so much time.

When assigning wrong types to a resource in the inspector the variables will be blank or 0 depending on the assigned type but if you open the tres file you'll see the true value assigned independent from the type established in the resource script.

I've already filed up an issue on github with a small project to reproduce the problem. I'll edit it with the new info thanks.

2 years later