while loop issues:

I am having issues with a while loop. It crashes the game.

.
.
.
Void addCard()
{
While(thisCard > 2  thisCard < 25)
{
thisCards string to guitex;
endGame();
}
}
.
.
.

There’s nothing in there to change the branch condition, so as soon as it becomes true you’re going to get stuck in that loop.

I thought the way i am giving it a range that would work. What could i do to stop it?

while ( something ) { somethingElse }
means
repeat somethingElse for as long as something is true
if somethingElse never modifies something, it runs forever.

While loops are basically just another way of doing for loops, with a different syntax. There’s nothing magical or special about them. If you did this:

for (var thisCard = 5; thisCard > 2  thisCard < 25;) {
    Debug.Log(thisCard);
}

What will happen? Or, for a different example, this:

for (var i = 0; i < 10; i++) {}

is the same as this:

var i = 0;
while (i < 10) {
    i++;
}

(Well, almost the same, maybe…depending on the language, there’s the matter of the iterator being local to the loop or not.)

–Eric

The value for thisCard with in the loop isn’t true after the second or third time looping though. Thats why im not understanding why its crashing.

Unless you’re changing it in endGame() the value of thisCard is not being modified.

The only thing you are testing against is “thisCard”, and “thisCard” does not get modified in the loop. Thus, the loop condition will never get broken once it is entered.

When the while loop starts, the value of thisCard is say 10, which should fire the endgame to raise the value. So after 2-3 loops it should be over 25 which should stop the loop right??

You haven’t posted endgame, so we can’t comment on it, is it modifying thisCard?

That sounds good in theory, but that can’t be what’s actually happening.

I suggest taking a close look at “thisCard” and “thisCards” as being a possible source of confusion. Failing that, we can’t help a lot without seeing the code in endGame(), because we can’t help with what we can’t see.

I’m at work now…the endgame is just the function to instantiate more cards into the game to raise the value of thisCard.

Here http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops it says to construct the while loop just how I have it above. And it still isn’t working.

if I use a if statement it only checks it once and fires the endGame function. Should I even use a while loop or just use the if statement then use a coroutine to run the addcard function again?

You still haven’t posted the part which appears to be the problem
if you just do this

While(thisCard > 2  thisCard < 25)
{
thisCard = thisCard + 1;
}

then everything will be fine
we cannot troubleshoot unposted code

Hpjohn, I sent you a PM.

Righto, and as theorised, the endgame function doesn’t modify the score values
For that matter, none of the functions you showed appear to modify the logic.playerTotal’s

I think you might need to reassess the logic of this step in the game, from what i can guess it should be: (psudocode)

this guys turn {
    while(score is less than 25) {
        deal me another card, and increase score
    }//the loop ends once score exceeds 25

    //after the loop is done
    go to end game and evaluate scores
}

Isn’t that what I am doing??? With the “dealersHcards();” in the while loop??

yes and no
in the script you sent, the loop is removed, and if all you did was change the while(…) into the if, it looks like you weren’t letting the loop complete before calling endgame (the OP suggests this too); it must come after the end of the loop, after the closing parenth

(plus the bit about nowhere actually incrementing the score once a card is drawn)

That’s how I have it set up-

/
/
/
void dealerHIT()
    {
        while(DealerTotalLogic.playerTotal < 17)
        {
            dealersHcards();
        }
            endGame();
    }
/
/
/

The playerTotal is incremented in Update.

This is unnacceptable

This while loop evaluates instantly, it doesn’t wait for the next time the frame updates
The loop may be dealing new cards to the hand, but the score variable doesnt change = the loop never exits.
If you were to Debug.Log the value inside the loop, it would just print the same value each iteration (except that becuase it loops forever, the console won’t refresh to show it)