Godot Version
for effect in effects:
if len(effect) == 2:
if typeof(effect[1]) == typeof(0.0) or typeof(effect[1]) == typeof(0):
print(effect)
effect[1] -= delta
Output:
["burning", 4]
Question
I’m trying to make a game, and this is the code that is supposed to handle status effects. Each element in the “effects” array is an array itself with the format (“effect”, 1), with the first element being the effect name and the second being the effect duration.
The code above is meant to decrease the countdown time by delta for each effect applied to the enemy. However, when I run it, it throws an error on the fifth (last) line: “Invalid set index ‘1’ (on base: ‘Array’) with value of type ‘float’”.
Things I have tried:
converting the “4” into “4.000001”
writing “effect[1] = effect[1] - delta” instead of “effect[1] -= delta”
I don’t think it’s a good idea to mix different types (strings, numbers) in the same array. Such a structure is prone to errors and difficult to extend later. Try to use a dictionary:
var effects = [
{"name": "freezing", "duration": 5.0},
{"name": "burning", "duration": 4.0},
]
And then in the _process function:
for effect in effects:
print(effect)
effect.duration -= delta
It should work.
1 Like
I changed the data structure from arrays to dictionaries. However, the exact same error still appears:
for effect in effects:
if "duration" in effect.keys():
print(effect)
effect["duration"] -= delta
if effect["duration"] <= 0.0:
effects_to_be_removed += [effect]
else:
effects_to_be_removed += [effect]
Output:
{ "name": "explode", "duration": 100 }
It’s the exact same error, despite me having thrown out most of the old code and rewritten it from scratch.
Your dictionary suggestion has been very helpful in making my code more readable. The restructuring was probably long overdue.
Can we see more about the ‘effects_to_be_removed’ please, specifically how it relates to altering the effects collection? Also can you confirm the specific line you get the error on? I’m assuming it’s “effect[duration] -= delta” but I don’t see why it would err.
1 Like
Trying hard coding an integer in place of delta to see if it changes.
Have you tried using single step in the debugger? That will show you exactly what is happening.
there’s code directly after this:
for effect in effects_to_be_removed:
effects.remove_at(effects.find(effect))
Would the repeated ‘effect’ variable affect things? I haven’t tested it (and I can’t right now for reasons.)
“Would the repeated effect variable affect things?”
It could, depending on how you implemented it all. I’m still not seeing any immediate culprits. How long is the script you are pulling portions out of? If it’s a reasonable length I’d say just go ahead and post the entire thing verbatim, maybe it’ll be easier to spot the problem.
It cannot be the exact same error.
The original error is pointing at the index ‘1’.
effect[1] -= delta
You are no longer using that index. There is no way the error can be the exact same.
effect["duration"] -= delta
If it is the same error message then the error is somewhere else in your code.
PS: Another person a couple of threads up from this had a similar error and it turned out he had declared his array as a constant const
.
Doing that I am able to reproduce the error.
So if that is the issue simply change that to a variable var
declaration
Nope, I have not declared any constants besides preloads such as
const preload("res://projectile.tscn")
What I would like to know is what sort of “errors somewhere else in my code” would cause such an error?
When I say the error is somewhere else in your code I mean double check the line number and the script file in the error message.
Again, since you are no longer indexing an array with ‘1’ the error message cannot say
invalid set index ‘1’
What does the new error message say precisely?
If it still says index '1'
then you have a repeat of your original code somewhere else and that is error-ing.
Please show us the definition of effects
.
I would also like you to check if you make it past the code block even once.
Put a print statement (or a break point) after the subtraction line:
for effect in effects:
if "duration" in effect.keys():
print(effect)
effect["duration"] -= delta
print("we made it past")
if effect["duration"] <= 0.0: