0 votes

I have this beautiful script that is a resource that allows me to make mulitple units, in the script/resource I have it calculate the stats of the units damage, health etc, but I have the base stats set up as export values so i add them in the editor, but for what ever reason the script its self ignores the export values and thinks them as zeros but I know that some export values work because when i print say the base_hp it prints it correctly but when i try to print just hp its just 5, (it seems the added 5 at the end works, makes sense to me at least) but the ints dont remember unless I make it a function that calls them to return it correctly.

Here is the script, am I making a mistake somewhere this is the second time I actually tried using a resource but this can be annoying.

extends Resource
class_name Monster

export(Texture) var front_sprite
export(Texture) var back_sprite
export(String) var name
export(int) var level
# Gets the types and gives the monster a type
export(TypeData.types) var type_1 = TypeData.types.none
export(TypeData.types) var type_2 = TypeData.types.none
# Base stats the limit the amount of points a monster can get
export(int) var Base_hp
export(int) var Base_attack
export(int) var Base_special_attack
export(int) var Base_defence
export(int) var Base_special_defence
export(int) var Base_speed
# The avaliable moves the monster can use to attack or other actions
export(Array,Resource) var move_slot = [null,null,null,null]
# The monsters stats based on the level
var hp  = ((float(Base_hp * level) / 100.0) + 5.0)
var attack = ((float(Base_attack * level) / 100.0) + 5.0)
var special_attack = ((float(Base_special_attack * level) / 100.0) + 5.0)
var defence = ((float(Base_defence * level) / 100.0) + 5.0)
var special_defence = ((float(Base_special_defence * level) / 100.0) + 5.0)
var speed = ((float(Base_speed * level) / 100.0) + 5.0)
# So when the monster takes damage we can takeaway the hp
var current_hp = hp
Godot version 3.3
in Engine by (156 points)

Try making your calculation vars onready vars instead.

That seemed to fix it but now I cant use the values because of a script that sets the values to the ui, seems the ui is ready before well, this resource

It seems now that they are onready they are now showing as nill values

Never mind I was being silly. I think you want to do all the calculations you're doing inside of the _init() function. So:

var hp  
var attack 
...
func _init() -> void:
    hp  = ((float(Base_hp * level) / 100.0) + 5.0)
    attack = ((float(Base_attack * level) / 100.0) + 5.0)

A init func never used it before I will use it thanks, also don't worry about we all make mistakes we are human after all!

I have tried to use it, like you did and it still producing the same result where 5 is the only value showing, meaning the base_hp * level is presented as 0, why wont it use the export values

Can you post some code around where you are using these resources? And how they are attached to your monsters?

You may need to make a generic setup() function that does the work that you have inside of _init() and call that instead of _init(). (You could keep it in _init() and call that but that's bad practice.)

I am gonna take back my comment, apparently it isnt a godot limitation but something wrong with the code, it seems adding onready should fix the issue but somehow the way I am accessing the resource is making it produce null, here is more infomation about the scripts I used and how I access them

https://godotforums.org/discussion/26246/a-resource-when-preloaded-into-another-script-only-produces-null-variables-when-calling-them/p1?new=1

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.