2

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
Dragon20C
  • 307
  • 2
  • 11

1 Answers1

2

This had eluded me in the past. However, by resourcing to OS.get_ticks_usec() I figured out the timing of these variables. And updated that answer where I go over the steps: here. Except that is for nodes, not resources, but the first three steps are the same.

So, when this runs:

var hp  = ((float(Base_hp * level) / 100.0) + 5.0)

Godot has not set the exported value of Base_hp yet. It has its default, which you didn't specify. You could give it a default value like this:

export(int) var Base_hp = default_value

My first intuition was to use _init, however that will also run before Godot sets the exported values. And there is no _ready on Resource, nor any convenient signal, or notification, or method to override.

Using setget to keep track of the exported variables getting initialized would be overkill and error prone.

That let us with simply making getter methods for these variables:

func get_hp(): return ((float(Base_hp * level) / 100.0) + 5.0)

Or with some extra work:

var hp setget , get_hp; func get_hp(): return ((float(Base_hp * level) / 100.0) + 5.0)

Which will at least let you treat it like a variable. Except it recomputes the value when you read it, and setting it is wasted effort.

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • Thanks Theraot I wanted it to be simply be hp = calculation but I tried using a function and that works and that's what I used before but I thought it was messy but it seems there isn't any other way,plus having it as a function might be a plus since level will change and I don't remember if variables update as they go, thanks for the insight, one last question should I go and report this as a bug on GitHub or is this intended? – Dragon20C May 10 '21 at 10:56
  • Should I go a different route and instead of using a resource you a script that extends a custom script with values, at least that way I can edit the script without it being a resource/scene – Dragon20C May 10 '21 at 11:23
  • @Dragon20C I don't think this is not a bug, but arguebly a missing feature. You could try writing a proposal at https://github.com/godotengine/godot-proposals (open an issue) - A Resource for this makes sense. Something you might consider is making the Resource be only exported variables that never change, let us call it "MonsterDescriptor" and then have a Node "Monster" that exports a Resource "MonsterDescriptor" and has all the rest of logic. See also [Resources](https://docs.godotengine.org/en/stable/getting_started/step_by_step/resources.htm). – Theraot May 10 '21 at 12:39