Page 1 of 1
new error "attempt to index field 'curState' (a nil value)"
Posted: Thu Feb 14, 2013 12:20 pm
by Komag
Anyone ever seen this error before?
attempt to index field 'curState' (a nil value)
It happens when I try to spawn a certain monster after onDie. It's a weird situation:
- if I type into the console to run the respawn function it works fine, no crash
- if I press K to kill the monster, no problems, he gets respawned immediately just fine
- if I attack and kill it with weapons/spells, I get the crash and error message
some weird junk is going on and I can't quite figure it out!
(EDIT - I cut the irrelevant parts)
Re: new error "attempt to index field 'curState' (a nil valu
Posted: Thu Feb 14, 2013 1:09 pm
by Komag
... it has something to do with the respawn locations, which have plates which trigger functions to change the facing of the monster using setPosition
EDIT - for one thing, ogres (and maybe all monsters) don't like to be turned (and not moved) with setPosition!
Re: new error "attempt to index field 'curState' (a nil valu
Posted: Thu Feb 14, 2013 1:32 pm
by Grimwold
Although I've not looked at setPosition myself, could you use it to move the monster to a holding 'cell' and then straight back?
Re: new error "attempt to index field 'curState' (a nil valu
Posted: Thu Feb 14, 2013 1:48 pm
by Komag
that's exactly what I also thought to try, and it seems to be working well now, and appears instantaneous

Re: new error "attempt to index field 'curState' (a nil valu
Posted: Thu Feb 14, 2013 2:17 pm
by Marble Mouth
Hi Komag. Is curState a variable that you used in your code, or something that the editor came up with? The error message makes it sound like curState == nil, but you're trying to treat it like a table (i.e. using curState.something or curState[ some expression ] .) Any variable which has not been assigned a value is == nil by default in lua. Of course, you can also manually set a variable to nil ( curState = nil .)
"spawn a certain monster after onDie"
You didn't specify, so maybe you already know this. Just in case, remember that at the point in time when onDie gets evaluated, the monster in question isn't dead yet. It's still checking if it's allowed to die. When I wanted to use an onDie hook to respawn a similar monster on top of the dying monster, I had to do silly things with a 0.1s timer to avoid a bizarre side effect. The editor let me spawn a second monster in the same square, but then the first monster immediately died and the second monster didn't obstruct other entities ( the party and other monsters ) from moving into its square.
- if I press K to kill the monster, no problems, he gets respawned immediately just fine
- if I attack and kill it with weapons/spells, I get the crash and error message
Do you also have an onDamage hook for the monster? Or onAttack/onCastSpell hooks for the party? That's the only difference I can think of between these scenarios.
If you post some of the relevant code, I will probably have more specific ideas about what went wrong. I just refreshed and saw your latest post. Grimwold's suggestion fixed everything? Great news, but I'm still curious, especially about whether curState is your variable or the editor's variable.
Re: new error "attempt to index field 'curState' (a nil valu
Posted: Thu Feb 14, 2013 2:40 pm
by Komag
I never used curState, never seen it before in my life, searching the forum comes up empty, so I have no idea what it is or how it works.
About spawning "on top", it's not an issue in my case because the spawn location is always guaranteed to be different from where the monster is that I just killed. There are two main locations, and if both are filled then there is a third "backup" location just in case, which gets utilized if I fight and kill one of the monsters when they are both occupying the two main locations.
I'm not using any onDamage or anything like that, so I too am curious as to what the difference is between K to kill and fight to kill. But there is something, that's for sure. We already know that K to kill doesn't award XP, so there must be other differences as well, under the hood.
Yeah, fight to kill spawns handle setPosition and trigger plates differently for some reason, sorta buggy but I'm managing to work with it.
So the problem seems to have been that the newly spawned monster (into one of the two main spawn locations) was immediately being "turned" with setPosition and it didn't like that. Maybe it can't have any setPosition so soon, but I did try a timer inbetween, up to a full 2seconds, and it would still crash at the time the timer fired. Now I have a very short timer do two setPosition commands in a row, one to the holding cell, and one back, and it's working well and reliably.