So, at the end I wrote two separate code, the first one is for a quest script, the second for a script attached to a ability spell with 1 of duration. The problem is that look like the remove spell doesn't work at all, so the script work only the first time.
Global variable set to 0 at start:
spelladded and enemyref
Quest name knockingout
Quest Script:
scn knokcingdownscriptshort dooncefloat fQuestDelayTime begin gamemode set fQuestDelayTime to 0.2 if player.isincombat != 0 player.addspell getenemyref set spelladded to 1 message "Combat started, spelladded set to %g", spelladded, 5 endif if spelladded == 2; if enemyref !=0 message "Combat stopped, spelladded set to %g", spelladded, 5 player.removespell getenemyref; endif endifend
Script attached to a ability spell with duration 1 (getenemyref)
scn getenemyrefscriptref enemyref creatureshort numenemyshort countershort removeshort distshort dist2ref attackerbegin scripteffectstart set numenemy to getnumrefs 69 1 set creature to getnumrefs 36 1 message "Enemy Number: %g, Creature: %g", numenemy, creature, 14 set enemy to getfirstref 69 1 set attacker to enemy set dist to 12 while (enemy) if enemy.getdistance player <= dist if enemy.getisplayablerace != 0 set enemyref to enemy else set dist2 to enemy.getdistance player message "enemy distance: %g", dist2, 14 endif endif set enemy to getnextref message "distance: %g, enemy: %g", dist, enemyref, 14 loopendbegin scripteffectupdateendbegin scripteffectfinish if player.isincombat == 0 set spelladded to 2 stopquest Knockingout message "stopped quest", 14 endifend
The begin scripteffectfinish never fire when combat finish. Surely there must be a really dumb error, but I'm unable to find it...