I am not sure if anyone has experience the same problem but I hope someone did because it drives me crazy.
I have ASimpleCharacter which holds:
UPROPERTY()
UBehavior* Behavior;
I initialize it in BeginPlay() by:
Behavior = NewObject<UBehavior>(this, BehaviorClass);
Then while playing, in the SimpleCharacters’s Tick() when I access UBehavior data, it doesn’t give the proper values that Behavior holds, it gives others. When I try changing value of let’s say the Behavior’s A variable (Behavior->A = 0.f, it makes it 0.f but I get this value only from within ASimpleCharacter, within Behavior it stays the same 1000.f as it was). It’s like the references are wrong. But they’re not, I’ve done all sorts of checks. It’s the same pointer, the same address all the time. I checked the references on both objects from both objects, I checked all the objects of their classes globally (in my case they are single instances), I checked CODs. There is no way I duplicate object or something.
But when I do something like that:
this->SimpleCharacter->Behavior
It gives proper values which would only make me think that the SimpleCharacter has a bad reference, and it points to another UBehavior, but there is no indications of it.
Please, help. I am about to lose it
What does “within Behavior” mean ?
Where exactly are you reading 1000.f instead of 0.f ?
Are you trying in multiplayer ?
So far I understand that you’re creating the object from ASimpleCharacter’s BeginPlay, and reading/updating it from ASimpleCharacter’s Tick, and that part works.
What is the part that doesn’t ? Send more details.
@Chatouille
Apologies if it unclear. Let’s say I use ASimpleCharacter::Tick()
and UBehavior::ProcessBehavior()
.
I want to change and log UBehavior::VariableA
from ASimpleCharacter::Tick
;
Inside of ASimpleCharacter
I have a reference to UBehavior
, let’s just call it Behavior
which I get here:
void ASimpleCharacter::BeginPlay()
{
Super::BeginPlay();
{
* code *
}
{
Behavior = NewObject<UBehavior>(this, BehaviorClass_);
}
{
* code *
}
}
The value of Behavior->VariableA
is by default 1000.f
;
UBehavior::ProcessBehavior()
is:
UBehavior::ProcessBehavior()
{
log(VariableA);
VariableA *= 2.f;
log(VariableA);
}
Let’s say, I run ASimpleCharacter::Tick()
:
ASimpleCharacter::Tick()
{
log(Behavior->VariableA); // 1.f
Behavior->VariableA += 10.f;
log(Behavior->VariableA); // 11.f
Behavior->ProcessBehavior(); // 1000.f
// 2000.f
}
The next iteration it’s going to be:
ASimpleCharacter::Tick()
{
log(Behavior->VariableA); // 11.f
Behavior->VariableA += 10.f;
log(Behavior->VariableA); // 21.f
Behavior->ProcessBehavior(); // 2000.f
// 4000.f
}
No multiplayer. The game is singleplayer
I think I kind of fixed it by messing with includes and gen files, at least for now. It has something to do with unreal reflection system, it’s not the first time I get some stupid bugs that just don’t make sense, and in the end it always turns out that the unreal compiler outputs some ridiculous operations from the code it parsed.