Page 1 of 2

For loop not working

Posted: January 27th, 2022, 4:36 am
by Paul
Last time albinopapa suggested that I make a github for my project. I would love to do that but I don't know how :(.

Anyways I'm having trouble with a for loop for some reason. The variable is initializing to the wrong number. It's 3 no matter what I do and doesn't change.

Has anyone encountered an error like this?

I'll attach my code again incase it's of use.

The problematic for loop is the y for loop in the update_discovered function in the Player class. The only time it's called is when a new ship is built.

Btw I'm still open to adding it to github but I need help.

Re: For loop not working

Posted: January 28th, 2022, 11:51 pm
by FirepathAgain
Do you mean that the "vision" variable is always 3?

Re: For loop not working

Posted: January 29th, 2022, 7:36 am
by Paul
No, the vision variable is 2 as it should be. The y variable in the for loop is 3.I tried setting y = -2 directly to see what happens and it was still 3. I can tell because I set up break points.

Re: For loop not working

Posted: January 29th, 2022, 11:48 am
by albinopapa
I give up man, I can't seem to figure out how to get the game to call that function, to be honest I don't know how to play the game lol. The for loop looks like it should work just fine in terms of setting 'y' to -vision to +vision.

Re: For loop not working

Posted: January 29th, 2022, 10:56 pm
by FirepathAgain
Are you debugging in release mode? That does strange things.

Re: For loop not working

Posted: January 30th, 2022, 6:23 pm
by albinopapa
Haha, yeah, the only variables that seem to be anywhere near accurate in the Locals window are the function parameters. Outside of that, some things seem to be optimized away so much that the compiler doesn't know which values are associated with the source code.

Re: For loop not working

Posted: January 30th, 2022, 10:58 pm
by Paul
@abinopapa I appreciate all the help and understand if you're calling it a day on this one. If not I'm more than willing to try explaining how to play my game. In the set up stage you can choose to build a ship as your starting project. Then in the first turn you can click on colonies, then the coordinate of the colony (there will only be 1 colony at the start of the game) and select place ship. You then must place the ship by clicking a square directly beside your colony on the galaxy map (cyan square for player 1, magenta square for player 2) and not on a star (white square). Then a text should pop up asking if you would like to place a ship on the selected square, and if you click on that an 'S' should appear on the selected square. At that point the 'update_discovered' function is called to update the discovered squares around the ship (turn them from black to grey). This is where we get our for loop problems...

@FirepathAgain I'm not too sure. How would I know if I was in release mode? And how would I change that? I'm using visusal studio 2022 btw.

Re: For loop not working

Posted: February 1st, 2022, 4:16 am
by FirepathAgain
Usually at the middle top of Visual Studio (VS) there is a dropdown with debug or release in it. That is where you choose the build configuration. When in release you can still run with debugging (F5) and it will hit breakpoints but it jumps around like crazy and values are all wacky. Basically it is not to be trusted lol. I don't think this is the problem but is something to try. If you're in debug it will step through the lines of code sensibly and values will be trustable.

I might give the game a crack later. I just looked at the Player.cpp code. Seems like it shouldn't always have that variable as the same value.

Re: For loop not working

Posted: February 1st, 2022, 4:43 am
by FirepathAgain
paul1.png
paul1.png (21.79 KiB) Viewed 3095 times
I put an attachment on showing my debugging session. Looks like it works fine.

I imagine you may be somehow examining y after it has finished the loop, in which case yes it will always be 3 when vision is always 2. It loops if it is less than or equal to vision (2), then increments it to 3, then the next loop checks if y(3) is less than or equal to vision(2) and it is not. y is left as 3.

Maybe in the build you're examining it in you declared y outside the loop so you could look at it and then every time the loop is done it is 3. Something like that.

Re: For loop not working

Posted: February 1st, 2022, 9:50 pm
by Paul
Oh yes I think I had some of the comparison operators backwards lol. I thought I had my breakpoints at the right position but I guess not :S. Thanks for your help!