Blueprint not changing variable when casting to other class

Hello,

I am using a trigger box to recognize when MyCharacter walks to a certain item.
Because of this I want MyCharacter to notice this event and when it happens, set a boolean to true.

Here is what I did in blueprints

My Level Blueprints

My Character blueprint

Character blueprint function

What happens is, that the boolean is set to true when the box is triggered, but when I hit the button to recheck, the value is set to false like nothing changed. What am I missing here?

I also put this up in c++ for comparance

MyCharacter.h

UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
bool hasFlashLight;

UFUNCTION(BlueprintCallable, Category = "Equipment")
	void EnableFlashLight();

MyCharacter.cpp

AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
this->hasFlashLight = false;
}

void AMyCharacter::EnableFlashLight()
{
	this->hasFlashLight = true;
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("hasFlashLight:: %d"), this->hasFlashLight));
}

// this is related to action mapping
void AMyCharacter::TurnOnFlashlight()
{
	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("FlashLight Status:: %d"), this->hasFlashLight));
	if (hasFlashLight)
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("Blink blink"));
	} 
	else
	{
		GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT("I should find a flashlight"));
	}
}

Some help would be awesome.

Best

Make the boolean value a UProperty(). If you are doing this on a server + client environment, It might be possible you change the value of a variable on the server while the client still has the old value…

Hi
sorry, that does not fix the issue.
I update my code snippet here with UPROPERTY(BlueprintReadWrite, VisibleAnywhere)

A blueprint callable function should have effect on the state. There are a few possibilities that could be wrong i can think of right now:

  1. you change the value on the server and you read it on the client
  2. you are calling the function in character A and reading it on character B.
  3. you are not calling the function although you think you do :stuck_out_tongue:

I added some more screenshots since this happens when I setup the same code in blueprints :frowning:
It just happens in the enable flash light function which is called from level blueprint.

I found out what is wrong finally.
When I’m using the node Get Player Pawn in the level blueprint instead of creating a reference to my blueprint character everything works fine.

Can someone please just explain me the reason for this, since I’m not able to find out what is wrong here

  • What character you actually possess? Show us a “possess code”.
  • Mb you define your character class as default pawn in GameMode?
  • Exactly how many characters are on stage when editor played?

I dunno what exactly you do, but i will try to suggest.

  1. You create persistent Character before editor run. Name it like “Char1”
  2. You add your AMyCharacter like “Default pawn” in mode
  3. When game start - game mode spawn default character (“Char2”) and possess it with controller
  4. So you have 2 character, but think you have only one
  5. Your function working with persistent char (char1) so they change vars inside Char1
  6. You input go to controller and then to character its possess so it char2 and change var inside char2
  7. When you change persistent link to “Get Player Pawn” - you get link to autospawned Char2 instead persistent Char1, so its all work fine.

If i suggest right - you just dont need use persistent character or pawn

Hi Detech,

thank you for your reply.
I think you are right :slight_smile:

I have following setup

  1. Gamemode which uses MyCharacter Blueprint as a Default Pawn
  2. MyCharacter Blueprint , based on c++ class.
  3. I droppend the blueprint character in the editor

So I have my gamemode which uses MyCharacter as DefaultPawnClass and addiionally having the blueprint in my level, which I referenced first!

Thank you for clearing this up, this was confusing :slight_smile: