Variable at construction not always updating

The Partridge Family were neither partridges nor a family. Discuss.
Neithari
Posts: 7
Joined: January 3rd, 2018, 7:44 pm

Variable at construction not always updating

Post by Neithari » January 3rd, 2018, 9:22 pm

Hello guys!

this is my first post here after half a year of watching chillies videos.

I made a Space Invaders clone and it's working pretty well i think. But when I added a background sprite and realigned my invader starting position I found a wired behaviour.

I do set the invader starting position inside game.h and forward it to my alien.h with a rect.
The rect however that arrives in alien is not updating every time I make adjustments to the rect in game.h or in game.cpp. I varied the generation and the point of generation of the rect various times but still the same problem. I do get it updated with a full recompile of the solution. Maybe thats how it is but I think it's more likely that I fucked up some compiler variable generating things or whatever.

Here is my Github: https://github.com/Neithari/Space-Invaders
I mostly use the dev branch but both are the same and up to date after I finish my work.

I hope you guys can help me :-)

Thanks in advance
so far
Neithari

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Variable at construction not always updating

Post by Yumtard » January 3rd, 2018, 9:41 pm

in Alien.h you have this

const Rect<float> playSpace;

This should be a reference or a pointer

Neithari
Posts: 7
Joined: January 3rd, 2018, 7:44 pm

Re: Variable at construction not always updating

Post by Neithari » January 3rd, 2018, 10:43 pm

thats a good idea. I changed it but still the same behaviour. The variable does change only with rebuild solution and not with a normal compile.
Could it be me sending the variable before it's initialization?

Repo is updated to pointer now.

Edit: I was in release, behavior is normal in debug. So that is at least fixed, but why is it occuring?

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Variable at construction not always updating

Post by MrGodin » January 3rd, 2018, 10:56 pm

Do you have DirectX SDK installed ?. I can't complie because of directx errors ->

Severity Code Description Project File Line Suppression State
Error C1083 Cannot open include file: 'd3d10sdklayers.h': No such file or directory (compiling source file Alien.cpp) Engine c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\d3d10.h 6773

must be because of i am using Windows 10
Curiosity killed the cat, satisfaction brought him back

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Variable at construction not always updating

Post by Yumtard » January 3rd, 2018, 11:03 pm

in game.cpp constructor

pAlien( new Alien( gfx, alienShotMax, alienShotChance, &alienSpace ) )

move this to the body of the constructor.

or init alienSpace before in the constructor instead of the h file.

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Variable at construction not always updating

Post by Yumtard » January 3rd, 2018, 11:09 pm

Sorry I haven't downloaded you game. Maybe I' not understanding your problem or something.
I don't see you ever changing alienSpace in gampe.cpp? it's also a const so you can't change it. So what do you mean it's not getting updated?

Neithari
Posts: 7
Joined: January 3rd, 2018, 7:44 pm

Re: Variable at construction not always updating

Post by Neithari » January 4th, 2018, 12:57 am

Yumtard wrote:I don't see you ever changing alienSpace in gampe.cpp? it's also a const so you can't change it. So what do you mean it's not getting updated?
When I am in release mode and I want to make chages to the alienSpace (now called playSpace) to realigne it for example I change a value and compile it but it is not changed in the game. Only if I rebuild everything or use Debug mode. And I don't know why this is happening. I get it aligned now so I don't have to tough that variable again but I am curious why it does behave like this.
MrGodin wrote:Do you have DirectX SDK installed ?. I can't complie because of directx errors ->

Severity Code Description Project File Line Suppression State
Error C1083 Cannot open include file: 'd3d10sdklayers.h': No such file or directory (compiling source file Alien.cpp) Engine c:\program files (x86)\windows kits\10\include\10.0.15063.0\um\d3d10.h 6773

must be because of i am using Windows 10
Not that I remember installing it or can find it right now. I am on win 10 too. Maybe because my solution is for VS 2017?
Edit: I checkt my VS install and the only thing I have directX related is
Graphics debugger and GPU profiler for DirectX under Games and Graphics
Found a solution in google. Do you have Windows 10 SDK installed? I have Windows 10 SDK (10.0.15063.0) for Desktop
Should I use 16299 or is that fine?
Last edited by Neithari on January 4th, 2018, 1:34 am, edited 2 times in total.

Neithari
Posts: 7
Joined: January 3rd, 2018, 7:44 pm

Re: Variable at construction not always updating

Post by Neithari » January 4th, 2018, 1:19 am

Yumtard wrote:in game.cpp constructor

pAlien( new Alien( gfx, alienShotMax, alienShotChance, &alienSpace ) )

move this to the body of the constructor.

or init alienSpace before in the constructor instead of the h file.
I tried moving it to the body before it was a pointer.
However now everything is working as expected. I init alienSpace/playSpace before in the constructor and now everything is working fine. Maybe I had it double init once in .h and once in .cpp I am not sure.

But anyway thank you for your help :-)

Florian3321
Posts: 27
Joined: February 12th, 2017, 1:50 pm

Re: Variable at construction not always updating

Post by Florian3321 » January 4th, 2018, 2:13 pm

Hi there!
If I understand your problem, I think it happend due to that you only changed the values in the h file. If you then just build the project, because noting changed in the cpp file, the compiler does not make a new obj and does not link in a new exe, so the changes do not get into the game. If you rebuild, then the "new" h file gets it into the game, and the values are updated. On the one hand you can rebuild the solution or init the variable in the cpp file, not the h file. You can verify what I said if you compile your project, run it in the debugger and step into the constructor of game. If you get to the lines where the construction happens in the h file, Visual Studio tells you that the h file is different form the last build... I had the same problem and found that out, maybe it helps you:) What I do not unterstand is that it works if you build in release mode, maybe because VS rebuilds the project with optimizations?!

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

Re: Variable at construction not always updating

Post by albinopapa » January 4th, 2018, 7:03 pm

Personally I think it's a bug that has been with VS for a while now. Sometimes I can make changes in headers and every .cpp file that includes that header is recompiled as it should. After a while of using VS or for some random reason, this behavior stops and I have to choose Rebuild instead of Build.
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

Post Reply