Statement inside loop runs even though condition not met

Post » Tue Aug 19, 2014 8:57 am

A really old project I started back in 2012 that I shelved so many times..

The statement inside the while loop is supposed to run if iIndex = 8. However it just runs anyway, while the iIndex is counting up to 8.

Function Soulburn(Actor akDeadActor)If bWaitAbit == true    SetCurrentStageID(5)    utility.wait(1.0)    Victim.ForceRefTo(akDeadActor)        If Victim.GetReference()            SetCurrentStageID(10)            Int iIndex = 0                                               While iIndex < 8                iIndex += 1                debug.notification("Current iIndex" +iIndex)                                        If iIndex == 8                        ;All of this runs regardless if iIndex == 8..                         EnchPurpleAura.Stop(Victim.GetReference())                        ;playing next visuals.                        SoulFXTarget.Play(Victim.GetReference(), 4.7, PlayerRef)                        SoulFXCaster.Play(PlayerRef, 5.9, Victim.GetReference())                                            Endif            EndWhile        Else            debug.notification("Alias not filled.")        EndifEndifEndFunction
User avatar
Lucie H
 
Posts: 3276
Joined: Tue Mar 13, 2007 11:46 pm

Post » Tue Aug 19, 2014 6:35 am

Might you want your If statement to be outside the While? Otherwise it will only run While the condition is < 8, including your If statement. Maybe.

User avatar
Alisia Lisha
 
Posts: 3480
Joined: Tue Dec 05, 2006 8:52 pm

Post » Tue Aug 19, 2014 3:58 am

Two things:

1. Does the debug output say that index is lower than 8?

2. Like Matthias said, you have a while loop that demands that the index be lower than 8, then inside that you have an if that checks for the loop to = 8, depending on how it handles the += 1 I would think this should never run.

User avatar
Andrew Perry
 
Posts: 3505
Joined: Sat Jul 07, 2007 5:40 am

Post » Tue Aug 19, 2014 6:18 am

Yes the output is lower. It counts up to 8 starting at 1.

I need to loop to exit somehow, but I don't need the If statement to run right away either.

From the log:

[08/18/2014 - 12:35:06PM] Current iIndex1[08/18/2014 - 12:35:06PM] Current iIndex2[08/18/2014 - 12:35:06PM] Current iIndex3[08/18/2014 - 12:35:06PM] Current iIndex4[08/18/2014 - 12:35:06PM] Current iIndex5[08/18/2014 - 12:35:06PM] Current iIndex6[08/18/2014 - 12:35:06PM] Current iIndex7[08/18/2014 - 12:35:06PM] Current iIndex8

Edit: I think this is running out of order because this function is called from a perk fragment...

User avatar
Benji
 
Posts: 3447
Joined: Tue May 15, 2007 11:58 pm

Post » Tue Aug 19, 2014 4:15 am

What is the purpose of the loop? What you're doing is incrementing iIndex until it hits 8, but you're never using iIndex for anything. The while loop is going to finish almost instantaneously, after all, it's just incrementing an integer. Removing the loop and the if statement would have almost the exact same effect, though it might complete a frame or so quicker.

And as for your question, the if statement isn't running 8 times, only the loop is. Your notification is outside of the if statement. Put it inside of the if statement and you will see that it only triggers when iIndex hits 8.

I actually disagree on the += check. It increments before the if statement, so when the iIndex = 7 at the top of the loop, it will increase by 1 before hitting the if statement. Still a seemingly pointless loop, however.

User avatar
{Richies Mommy}
 
Posts: 3398
Joined: Wed Jun 21, 2006 2:40 pm

Post » Tue Aug 19, 2014 12:06 am

Well putting iIndex in the statement, indicates that's being set straight to 8.. no 1,2,3,4 unlike what it's doing in the loop. Which makes no sense because I clearly have it written IF iIndex == 8 and not iIndex = 8.

The purpose of the loop was to prevent the if statement from running til needed. I guess that's not the proper way. Though I've seen syntax like that used in arrays, if i'm not mistaken.

So the question is how I keep that from running too soon and when I want it to run?

Edit: errr oh I understand now about it finishing almost instantly. I was put off by how slow the debug was incrementing thinking that was the actual speed of the loop -.- So it was running correctly, but just FASTER than I intended.

User avatar
^~LIL B0NE5~^
 
Posts: 3449
Joined: Wed Oct 31, 2007 12:38 pm

Post » Tue Aug 19, 2014 6:22 am

Another way of explaining it (sorry if this is too basic or too complex):

A loop is going to run every single frame (or however Skyrim handles script ticks) that the condition at the top of the loop isn't met. Your loop's condition says that the loop should run every single frame until your integer, iIndex, equals 8. However, you're incrementing iIndex at the top of the loop at every frame, so it will reach 8 very quickly. Your if statement inside of the loop will then run once on that last repetition through the loop, as iIndex is equal to 8.

I suggested that you put the debug.notification() inside of the if statement because the notification will then properly display that the if statement is only running once. It's not necessary, it was just to illustrate my point. :smile:

On what condition do you want the if statement to run? Not in terms of iInteger, but in real terms - i.e. "I want the statement to run after ____ finishes" or something.

User avatar
brandon frier
 
Posts: 3422
Joined: Wed Oct 17, 2007 8:47 pm

Post » Tue Aug 19, 2014 8:37 am

And your point was made well. :smile:

I want the statement to run after a certain amount of seconds have passed. I am thinking at least 15-20. Anything less would be too fast.

This would have been much easier if conditions on scene phases didn't cause the phase to be skipped by the quest engine if they are false.

User avatar
marina
 
Posts: 3401
Joined: Tue Mar 13, 2007 10:02 pm

Post » Tue Aug 19, 2014 4:20 am

Ah, luckily there's a much simpler way of doing so - http://www.creationkit.com/Wait_-_Utility. Perhaps remove the loop and if statement, and instead put Utility.Wait(15) in place of the if. ;)

User avatar
GEo LIme
 
Posts: 3304
Joined: Wed Oct 03, 2007 7:18 pm


Return to V - Skyrim