Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Dragon20C |
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
Try making your calculation vars
onready vars
instead.
timothybrentwood | 2021-05-09 19:09
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
Dragon20C | 2021-05-09 19:17
It seems now that they are onready they are now showing as nill values
Dragon20C | 2021-05-09 19:23
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)
timothybrentwood | 2021-05-09 19:44
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!
Dragon20C | 2021-05-09 20:19
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
Dragon20C | 2021-05-10 07:52
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.)
timothybrentwood | 2021-05-10 13:56
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
Dragon20C | 2021-05-11 06:12