Can't use the enum integers to define a couple variables

Godot 4.3 stable.

Hello everyone,

So I’m trying to code for a spell, where two of the variables are mana and sp_trsmog, and both will have their values be dependent on the enums spelllevel and trsmog_affinity.
the variable mana is set to be equal to whichever spell level (from the enum) multiplied by 10 and then plus 10. so if minor it will be 10 * 1 + 10 = 20
the variable sp_trsmog is set to be equal to whichever integers are set from trsmog_affinity and spelllevel multiplied by each other. so if minor and strong will be 1 * 3 = 3
However, this way of going about it doesn’t seem to work and I don’t understand why exactly. It just has mana = to 10 and sp_trsmog = 0, which makes me think it is a bug since I had mana = 10 as the default while I was figuring out the basic spell mechanics. I have copy-pasted the code bellow.

Going to create an Enum for spell level inshallah. Spell level will be a factor in trsmog and manacost.

enum spelllevel { cantrip=0,minor=1,moderate=2,major=3}
@export var splevel : spelllevel

not sure if this should go here or in what it’s affecting. either way I’ll have to make a signal I guess.

enum trsmog_affinity { none =0,weak=1,moderate=2,strong=3}
@export var trsmogaf : trsmog_affinity
var sp_trsmog = trsmogaf * splevel

var mana = ((splevel * 10) + 10) :
set(value):
mana = clamp(value,0,INF)
@onready var timer: Timer = $Timer
@export var spell = true

func _ready() → void:
timer.start()
print(“spell is cast”)
print(sp_trsmog)
print(splevel)
print(trsmogaf)
print(mana)

The output is :

spell is cast
0
3
3
10

Any help will be appreciated!

For the code execution:

  • Init variables (from top to bottom)
    • splenel = 0
    • trsmogaf = 0
    • sp_trsmog = 0*0 = 0
    • mana = ((0 * 10) + 10 = 10
    • timer = null
    • spell = true
  • Load variable from scene
    • splevel = 3
    • trsmogaf = 3
    • spell = <I don't know what you assigned to it>
  • @onready
    • timer = $Timer<Timer>
  • _ready()
    • Call timer.start()
    • Output spell is cast
    • Output sp_trsmog = 0
    • Output splevel = 3
    • Output trsmogaf = 3
    • Output mana = 10

The line I bold is the final result.
Now you know why the output is that.

Solution:

  • Recalculate the sp_trsmog and the mana when(choose one):
    • _ready() is called.
    • splevel or trsmogaf is assigned a new value.
  • Or, use a function instead of variable to calculate that.
2 Likes

Thanks man! I was really smacking my head against the wall for a while there.