Begginer question: C++ code work ... only once.

Hello.
I’m completely new to UE4. And there is even worst - I’m physicist - not a programmer. So here is a problem. I try to make easy game. I write piece of code that create map from TXT file. Player can move only on plane so I thought this will be quickest way to do it. Then I create new C++ class that inheritance after APawn class. And inside my header file i add:


UFUNCTION(BlueprintCallable) void SetPlayerInStartPosition();

and in my source I add:



UFUNCTION(BlueprintCallable) void AMyPawn::SetPlayerInStartPosition()
{
 FTransform transform;

 //... here I take x and y player position....
 //x and y is a proper number - I found them by searching of my
 //std::array for value that represent "here start player"
 transform.SetLocation(FVector(x * 100.0f, y * 100.0f, 0.0f));
 //...other not UE4 important code
 this->AddActorWorldTransform(transform);
 return UFUNCTION(BlueprintCallable) void();
}


Then in MyPawn_BP Blueprint I made link from “Event Begin Play” to this function.

So here is a problem: I compile this in Visual Studio (F7) I hear “beep” from UEditor - so I return there, press “Play” (Alt-P) application and everything work as expected (player is visible in right position - in my case (900,600,0) ). Now in UEditor i press “Stop” (Esc) and again press “Play” (Alt-P) - WITHOUT CHANGING ANYTHING AT ALL - now player is NOT in right position (sits on (0,0,0)). Only “fix” to this is to click “Compile” in UEditor and then “Play” but then again - if I click “Stop” and “Play” again then … Player start in (0,0,0).

As you can imagine clicking “Compile” every time before I click “Play” is annoying. Any ides where I can do something wrong to got such problems?

I’m using:
UE: 4.24.3-11590370+++UE4+Release-4.24
Visual Studio Enterprise 2019: Version 16.5.5

Best regards,
Yansen
PS. Sorry for my poor English - it’s not my native language.

That code shouldn’t compile at all. You can’t have UFUNCTIONs in your source file… (Or maybe it just gets compiled out)…

You can also skip using AddActorWorldTransform, since what you really want is to just set the Actor Location.

Your source file should look like this:



void AMyPawn::SetPlayerInStartPosition()
{
//... here I take x and y player position....
//x and y is a proper number - I found them by searching of my
//std::array for value that represent "here start player"
//...other not UE4 important code
FVector startLocation(x * 100.0f, y * 100.0f, 0.0f);

SetActorLocation(startLocation);
}


You could also use “TeleportTo” which will do a sweep and more intelligently place the player.

Also, mixing std and UE4 is a bad idea, so I would use TArray over std::array if you can.

Dear ExtraLifeMatt,

Thank you for you advice about UFUNCTION(BlueprintCallable). A far as I get it I leave it only inside header file (or method is not visible in Blueprint). Right? SetActorLoaction is also much more clear - no idea why I didn’t found it :slight_smile:

However making this changes don’t resolve my problem. Still need to “Compile” every time before I click “Play” or Player is in wrong place. I also discovered UE_LOG macro. So I put:


UE_LOG(LogTemp, Warning, TEXT("MYLOG:SetPlayerInStartPosition - BEGIN"));

at the beginning of my method and:



UE_LOG(LogTemp, Warning, TEXT("MYLOG:SetPlayerInStartPosition - END"));
FVector loc = GetActorLocation();
UE_LOG(LogTemp, Warning, TEXT("MYLOG: Player pos: %f %f %f"), loc.X, loc.Y, loc.Z);


at the end. And when I look at log file located in “\Saved\Logs” i see that this function is always called. Just make nothing (except writing text to log…). So after a first run i got:

[2020.05.15-14.16.32:877][601]LogTemp: Warning: MYLOG:SetPlayerInStartPosition - BEGIN
[2020.05.15-14.16.32:877][601]LogTemp: Warning: MYLOG:SetPlayerInStartPosition - END
[2020.05.15-14.16.32:877][601]LogTemp: Warning: MYLOG: Player pos: 900.000000 600.000000 0.000000

and after second run (WITHOUT clicking Compile):

[2020.05.15-14.16.46:562][736]LogTemp: Warning: MYLOG:SetPlayerInStartPosition - BEGIN
[2020.05.15-14.16.46:562][736]LogTemp: Warning: MYLOG:SetPlayerInStartPosition - END
[2020.05.15-14.16.46:562][736]LogTemp: Warning: MYLOG: Player pos: 0.000000 0.000000 0.000000

Best regards,
Yansen.

Hello.

Ok problem (partially) solved. It was in “other not UE4 important code” in code above. To this who may encounter such problem in the future. I create 2D map like this:.



xxxxxxxxxxxxxx
x         x  x
x   b     x  x
x            x
x      b  x sx
x  p      x sx
xxxxxxxxxxxxxx


where “x” is wall, space is floor, “b” is box and “s” is place where player need to push boxes to win the level, “p” is player initial position. I store this map in something like:



std::array<std::array<int, size_x>, size_y> Level_Map;


Based on this array I:

  1. Create level by spawning actors like “floor”, “wall”, “box” and “box_place”.
  2. In my C++ code i create a function that:
    a) search for “p” in this 2D table and place player there
    b) after player is placed it change this “p” to “floor” (so game logic will see that this is free space and player can walk over it or push box there if he want)

And here all problems started. First time after compile everything work just fine and as expected. But when I try to run this game second time I discover that my Level_Map contain “p” no more! This is behavior I don’t understand - because so far I thought that “Stop” button in Unreal Editor is full analogy to “close” button when I write classic application. I suspect some data caching for better UE performance here - but I wish to be sure to avoid such problems in the future. In next steps I will try to use TArray as ExtraLifeMatt [suggest. And also will try to make minimal app that show this behavior and create other thread about it since I can’t found any info about this behavior and wish to understand this in deep.

Best regards,
Yansen.](https://forums.unrealengine.com/member/172958-extralifematt)