For loop not working

The Partridge Family were neither partridges nor a family. Discuss.
Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

For loop not working

Post by Paul » January 27th, 2022, 4:36 am

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.
Attachments
Quest for Zest.zip
(391.49 KiB) Downloaded 64 times

FirepathAgain
Posts: 30
Joined: March 20th, 2021, 3:01 am

Re: For loop not working

Post by FirepathAgain » January 28th, 2022, 11:51 pm

Do you mean that the "vision" variable is always 3?

Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

Re: For loop not working

Post by Paul » January 29th, 2022, 7:36 am

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.

albinopapa
Posts: 4370
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: For loop not working

Post by albinopapa » January 29th, 2022, 11:48 am

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

FirepathAgain
Posts: 30
Joined: March 20th, 2021, 3:01 am

Re: For loop not working

Post by FirepathAgain » January 29th, 2022, 10:56 pm

Are you debugging in release mode? That does strange things.

albinopapa
Posts: 4370
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: For loop not working

Post by albinopapa » January 30th, 2022, 6:23 pm

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

Re: For loop not working

Post by Paul » January 30th, 2022, 10:58 pm

@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.

FirepathAgain
Posts: 30
Joined: March 20th, 2021, 3:01 am

Re: For loop not working

Post by FirepathAgain » February 1st, 2022, 4:16 am

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.

FirepathAgain
Posts: 30
Joined: March 20th, 2021, 3:01 am

Re: For loop not working

Post by FirepathAgain » February 1st, 2022, 4:43 am

paul1.png
paul1.png (21.79 KiB) Viewed 1159 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.

Paul
Posts: 18
Joined: December 31st, 2021, 1:45 am

Re: For loop not working

Post by Paul » February 1st, 2022, 9:50 pm

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!

Post Reply