Functions calling each other in weird order

the functions currently call each other in this order CharacterFunc1, PCFunc(with value = 0), GMFunc, PCFunc(with value = 2 but skipping the character cast), PCFunc, PCFunc, CharacterFunc1, PCFunc(with value = 0)

i want it to CharacterFunc1, PCFunc(with value = 0), GMFunc, PCFunc(with value = 2), CharacterFunc2

void AShooterCharacter::CharacterFunc1()
{
	AShooterPlayerController* PC = Cast<AShooterPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
	if (PC)
	{
		PC->PCFunc(0);
	}
}

void AShooterCharacter::CharacterFunc2(int32 Value)
{
	Variable = Value;
}

void AShooterPlayerController::PCFunc(int32 Value)
{
	if (Value == 0)
	{
		AShooterGameMode* GMM = Cast<AShooterGameMode>(UGameplayStatics::GetGameMode(GetWorld()));
		if (GMM)
		{
			GMM->GMFunc();
		}
	}
	else
	{
		AShooterCharacter* Character = Cast<AShooterCharacter>(GetCharacter());
		if (Character)
		{
			Character->CharacterFunc2(Value);
		}
	}
}

void AShooterGameMode::GMFunc()
{
	AShooterPlayerController* ShooterPC = Cast<AShooterPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
	if (ShooterPC)
	{
		ShooterPC->PCFunc(2);
	}
}

}

please dont use int as switches for a function’s logic .that reduces code simplicity .break functions down to separate functions .

Is that why it is not workin

I don’t know how you checking the execution order, but don’t trust the order of the logging messages.

So GEngine->AddOnScreenDebugMessage order isn’t completly correct ?

if so that makes sense

Just not trustworthy, specially when you log a large number of messages in a short amount of time.

Is the way I am calling the functions correct

Like is passing variables through 2 or more functions bad

Without context we can’t say.

In my example with the code

What I was trying to accomplish was getting a variable from game mode to character

Was my method the correct way

What I was trying to accomplish was getting a variable from game mode to character

If that’s it, then in the character, get the game mode and then the value of that var. I don’t see any use for the playercontroller here.

yes but i have problems though

one is that when i cast to gamemode and call a function in gamemode that then calls a function in character and passes a variable i cant do anything with the variable

the problem is that the TestNewVariable function returns the ChangingVariable but it is 0 when it should be 2

BTW TestNewVariable func is binded to keyboard key

Character cpp

void AShooterCharacter::getGM()
{
	AGameModeBase* GM = Cast<AGameModeBase>(UGameplayStatics::GetGameMODE(GetWorld()));
	if (GM)
	{
		GM->GetVariable();
	}
}

Gamemode cpp

void AGameModeBase::GetVariable()
{
	AShooterPlayerController* PC = Cast<AShooterPlayerController>(UGameplayStatics::GetPlayerController(GetWorld(), 0));
	if (PC)
	{
		PC->RetrieveVariable(2);
	}
}

Character cpp

void AShooterCharacter::RetrieveVariable(int32 Value)
{
	ChangingVariable = Value;

	FString IntAsString = FString::FromInt(ChangingVariable);

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, IntAsString);
}

Character cpp

void AShooterCharacter::TestNewVariable()
{
	FString IntAsString = FString::FromInt(ChangingVariable);

	GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Orange, IntAsString);
}

I created this without much of a link to the top code

This example is what I am now focused on

I am just trying to set a variable in the player controller to be the same as a variable in gamemode

RetrieveVariable() is in the playercontroller or in the character? From the code above, RetrieveVariable in the character but you’re calling it in the character.
Do you have a RetrieveVariable() function in both the character and in the controller?

Thanks but i wanted to have it open for

  • pc asking gm if variable in array is avaliable
  • if so then pc confirming it and setting gm variable to unavailable
  • then have a copy of which variable was selected in the pc for later use

Sorry, I’m just not getting it. It makes no sense to me.
An interaction diagram would be very helpful to fully understand the flow of data and communications.

Can’t an event dispatcher help with this issue?

So, we have:

  • PC and GM;
  • var in PC = var in GM

So, who and where is setting that variable?
If, for example, the value of the variable is set from a key event in the PC, then when it’s set in the PC, you also set it directly in the GM. No need for GM to go to PC to get that value.

Sorry Maybe i should have said this earlier on

this is what i am trying to achieve