Hello, I am new to UE4 C++. I am making a puzzle that when user step on a correct tile, the times the player has stepped on that tile (SteppedOnTimes) will increase, and when step on the wrong tile, ALL the tiles’ steppedOnCount will be reset to 0.
The correct and wrong tile is from the same ATile class, just determined by a boolean variable isCorrectTile.
My code below only reset the SteppedOnTimes to 0 of the overlapping instnace, while I want to reset all the instance.
void ATile::OnOverlapBegin(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
AMainChar* Main = Cast<AMainChar>(OtherActor);
if (isCorrectTile == true && SteppedOnTimes == 0)
else if (isCorrectTile == false)
SteppedOnTimes = 0;
Ok. First a small piece of advice to make your code more readable: better/more meaningful variable names. Calling your character variable ‘Main’ is not considered good practice and it confused me a tiny bit
Now, you can do this in many ways. As a design choice, perhaps you want to think about another type (class) whose job is to be the ‘Manager’ of the Tiles. Holds an array of them and checks stuff. It may get notified when one of the tiles complains about a mistake, so it goes in a loop and resets everything. This actually can go into a GameMode as well, rather than a separate class.
The simplest thing to do, to achieve exactly what you are saying, is to make that variable static, meaning it is the same variable for all instances of that type. Job done.
I only suggested the first option though, because it seems like you are doing the same count in your character class? That’s why I’m saying, maybe it needs to be a kind of global variable somewhere for everyone involved (player, tiles, whomever etc.).
Thank you for your suggestion, but I have some problem when trying to make a class to manage the whole puzzle, which type of class should my manager class be. Basically, my puzzle has a starting tile, an ending tile and tiles (correct or wrong is check by a boolean variable), each one is a class.
I have added an UPROPERTY id to those 3 classes to identify each puzzle, it worked but have to input the id of each tile when place it in the level.
Ok, I’m not exactly following how your puzzle works (maybe send pics) but in my mind you probably don’t need 3 classes for what is essentially the same thing (you can come up with other identifiers for starting/ending tiles, an enum maybe). But anyway, a ‘manager’ class can most likely be an actor as well. It should be super simple. Just an actor whose job is to monitor these ATile actors. Or it can be a UActorComponent that is part of some other actor that makes more sense. You decide.
Have you tried the static variable I suggested?
Yes, thank you, after some adjustments, I have a manager class to manage the static variable, cannot do it in only the tile clase since it will update and reset all while i just need to reset all and update individually