variable problems


I’m pretty new to c++ programming in ue4. I’m having some trouble:

I initialized Shotgun and RPG in the character.h.
In the constructor, I set Shotgun as False and RPG as true.
Screenshot 2021-01-02 152858.png
this is the constructor, right?

I then made it so when I press 2, it sets Shotgun as true and RPG as false.

But when I press 2, everytime I fire afterwards, RPG is still coming as true and Shotgun as false.
Screenshot 2021-01-02 153109.png
When I press 2, I set shotgun to true and I check if shotgun is true. If so, I print out “shotgun is true”, so I prove that shotgun is true. I also set RPG to false.
However, when I press LMB, I check if RPG, and if so, it prints “not”. (to symbolize that shotgun is false, which I also verified.)

Is it a problem with how I’m initializing it?


You need to post some code (constructor + functions handling input). Without it there is no way to point where the error is.

Just a heads up, Constructors in UE4 are slightly different than normal C++ just due to the Lifecycle of an Actor and BP exposed variables. If you set a value to false in the constructor, but the Blueprint asset marks it as true, it’ll start false but end up true after PostInitProperties/PostLoad. It’s generally a good idea, for Actors at least, to move any initialization from the constructor into BeginPlay / PostLoad / PostInitProperties (if you’re new, I’d just toss it in BeginPlay).

Screenshot 2021-01-03 104705.png
This is where it prints that Shotgun is true, proving that it at least changed for the function. If I had to summarize my problem completely, it would be that changes I make to a function only change the function itself and don’t seem to actually carry out.

it still won’t say that it’s true and RPG is false.

and this is how I call it.
@ExtraLifeMatt unfortunately same problem happens.

Toss a break point in where ever you change those values and see what’s causing them to change. They don’t just change on their own. :slight_smile:

I found out the problem, and I hate myself.

Handlefire’s UFUNCTION was Server, Reliable.

SwitchToShotgun’s was just BlueprintCallable.

Thanks for the help @ExtraLifeMatt though.

Heh, figured it may be replication related but I didn’t see that in your code. Glad you figured it out.