CPP bool varaible always returning false

When I try to access variable in blueprints, it always evaluates as false. I added it through cpp and set it up in instance of actors (like editable blueprint variables)

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "AI")
	bool bCrouchIdle;

I understand that you drag some actors into the world and then change this property to be true.

If you manipulate bCrouchIdle in c++ or somewhere else, make sure you’re not setting it to false by mistake.

Also double check which instance you are debugging. You can set it up in the toolbar of the blueprint.

I’m not setting it anywhere as far asI can see. I added it to the BT service now and in debug window I notcied that it irregulary and very often changes from true to false and then to true again

You have to set a variable in the constructor if you do not want to experience undefined behaviour. The variable is empty and has no value set so it can be anything. The code you showed is fine, but there has to be more. Where is it exposed? Where is it used? Every time an object containing this variable is created the value of that variable is random. It can be true OR false. That means that every time it you spawn one, every time you restart the simulation or play in editor it is randomized. In the constructor set the variable to true or false and the problem should be fixed.

I’m setting it up in actor instaces defaults (like for blueprints editable varaible). alos it’s set to false in constructor

I added it to print string in tick to check, it alternates between false and true every tick

Then you have to have a node in your blueprints taht is changing it or in your code.

I did the search for references and I’m not setting it anywhere. Also it’s paired with rotator variable that get sets to (0,0,0) for some reason.

Then my only advice is to recreate a small test project with your variable in it and see if it changes. It is highly weird that you have this behaviour. Are you sure you are not overriding the constructor without calling the super constructor?

UPROPERTIES are initialized. Still, it is good practice to init variables

Hey -

Can you explain or provide a screen shot of where you’re using the boolean value? If the boolean is always returning false, there may be something wrong with the setup that switches the value of the variable as necessary.

I tried creating a bool in code and printed it from a blueprint as shown.

The default for the variable in code is false and after the first print string it gets set to true.

Hey, I’m declaring it in my custom enemy Character class, initialize it with false in constructor, then set it up for instances of those enemies in editor. I need it in Behaviour Tree to establish if I should go down one branch of it. It didn’t work so I put it to service to check if maybe it will set up correctly at later time. It didn’t so I added it to print string of EventTick of my enemy Character and it alternates between true and false. The only times I set it up is in the constructor and the checkbox on Character instances

At Begin Play of Character I call method in the AI

bool cr = Cast(GetPawn())->bCrouchIdle;
	BlackboardComp->SetValue<UBlackboardKeyType_Rotator>(BlackboardComp->GetKeyID("baseRotation"), Cast<ABaseEnemy>(GetPawn())->baseRotation);
	BlackboardComp->SetValue<UBlackboardKeyType_Bool>(BlackboardComp->GetKeyID("crouchIdle"), cr);

Make sure your variable is not global over all AI characters

This bool has to be false in the BB Data

That was it, thanks! I can’t remember setting it up earlier, was it added at later time? Also I think it would be quite more intuitive for this to be false at default (or is there something I don’t see here?)

Man try to give more info for the start. Mentioning Blackboards so late delayed a good answer

Glad it helped :slight_smile:

I remember getting also strange behavior because of this, and I also feel like it used to be different, not even used this “feature” yet ^^

It is true though, if you added a bit more info like Plosnita suggested we could of helped you a lot faster!