With a simple change of making %spawn initialize as __Me, I got it to work. Better yet, it eliminated the lag issue upon death as well.
Here is Version 2 of the WIP:
http://www.mediafire.com/download/31a9i32pm8ccd4f/RespawningEnemiesWIPv2.zipAnd the code-
DefaultCharacter changes:
INIT
INT:%canRespawn=1
CHARACTER:%spawn=__Me
FLOAT3:%spawnPos=null
EVENTS
EVENT RespawnCharacter
VARS
CHARACTERTEMPLATE:_root
FLOAT3:_myPos
ON
OnInit()
ACTIONS
IF "c1&c2&c3&c4"
IsEqual(%canRespawn, 1)
CharacterIsDead(__Me)
CharacterGetTemplate(__Me, _root)
GetPosition(__Me, _myPos)
THEN
IF "c1"
IsEqual(%spawnPos, null)
THEN
Set(%spawnPos, _myPos)
ENDIF
SpawnCharacter(%spawn, _root, %spawnPos, 0)
ENDIF
EVENT ProgenyDeath
ON
OnDie(%spawn,_,_,_)
ACTIONS
IF "!c1"
IsEqual(__Me, %spawn)
THEN
GetPosition(%spawn, %spawnPos)
Set(%spawn,null)
Set(%canRespawn, 0)
CharacterEvent(__Me, "CheckForRespawn")
ENDIF
Story Code:
INIT Section-
KB Section-
IF
CharacterCreationFinished(CHARACTER_NULL)
AND
CurrentLevel(_Lvl)
AND
DB_CharacterCreationLevels(_Lvl)
THEN
CharacterAddSkill(CHARACTER_Player1, "Shout_RespawnEnable");
CharacterAddSkill(CHARACTER_Player1, "Shout_RespawnDisable");
CharacterAddSkill(CHARACTER_Player2, "Shout_RespawnEnable");
CharacterAddSkill(CHARACTER_Player2, "Shout_RespawnDisable");
IF
CharacterUsedSkill(_Char, "Shout_RespawnEnable", _)
AND
DB_RespawnEnabled(0)
THEN
NOT DB_RespawnEnabled(0);
DB_RespawnEnabled(1);
IF
CharacterUsedSkill(_Char, "Shout_RespawnDisable", _)
AND
DB_RespawnEnabled(1)
THEN
NOT DB_RespawnEnabled(1);
DB_RespawnEnabled(0);
IF
CharacterDied(_Char)
THEN
CharacterSetEvent(_Char, "CheckForRespawn");
IF
CharacterEvent(_Char, "CheckForRespawn")
AND
DB_RespawnEnabled(1)
THEN
CharacterSetVarInteger(_Char, "canRespawn", 1);
Respawn starts off enabled (and for now works for Players as well). Whenever a character dies, they trigger the CharacterDied story event that calls CheckForRespawn, which allows the character to 'respawn' if Respawn is enabled.
When a 'respawned' character dies, the original character updates their %spawnPos variable with the respawned character's position upon death and checks whether or not Respawn is enabled for further generations. If so, then the next respawn occurs where the last one died.
The skills added after Character Creation can be used to toggle respawning on or off. This toggle matters only when the character is killed; if it is On when a character dies, they will respawn even if it's turned Off later after their death.
----
This is really close to what I want my Respawn mod to be. I'm going to see if I can add one more skill-toggle that will cause everything to 'respawn' when used (ie set all dead characters' canRespawn to 1). I'll also include the check so that Players can't be respawned via this method and maybe add a check for Time since the last spawn died.
As it is right now, the original characters' corpse will remain where they died even when they 'respawn'. I have it this way in case there are some key-items that are stored upon the corpses upon death. Are there any such key-items? If not, I could have them SetOffStage so that they are invisible after respawning.
Edit:
It seems that I left the %canRespawn initialized as 1 instead of 0. This will have the side-effect of making everything respawn at least once even if the Respawn toggle is off. This will be changed in the next WIP version.