UE4 Test if pawn is controlled pawn - unreal-engine4

I made up a C++ ThirdPersonExample project. I added several ThirdPersonCharacter BP. (TPC BP) I added a variable to the TPC BP.
I play as one of the TPC BP's. The other ones are controlled by AI.
In Event Graph of TPC BP, on Event BeginPlay, I want to set a variable if this current instantiated TPC BP is the controlled pawn - ie it's me playing!
I see that you can set variables.
However:
I only want to set a variable if I'm not the Controlled Pawn
I want to also set another variable if I am the Controlled Pawn.
I may be thinking of this the wrong way...any help appreciated.

I think that the easiest solution would be:
Add Event Possessed to TCP BP. This should be fired when your pawn (character) is possessed by a controller.
Since previously mentioned event has parameter - reference to a possessing controller, you can then try to cast that reference to PlayerController and AIController ( or their descendants).
Final step is to implement specific code for both types of possessing controllers (when cast was successful).

Related

Can't pick StaticMeshActor from scene when I create variable of that type in blueprint, if the actor is from sub-level in Unreal Engine 5

I have made an blueprint and inside of it, variable of type StaticMeshActor (object reference):
When I create instance of this blueprint in scene, I get the drop-down for my “Test” variable with StaticMeshActor actors in scene (that’s correct).
But once I click on any of them (that are from sub-levels), field resets to “None” again. Same goes for picker icon from scene.
Is it bug, or am I doing something wrong? Is it even possible to select actor from sub-level?

UE UMG, when I addchild use C++, slot lost Slot properties I designed in blueprint, how can I keep it?

When I designed one Slot properties in blueprint, it works just when I put it on another blueprint directly.when I dynamic create one and addchild to another widget use C++, Slot properties designed in blueprint are not works, I need to set it by code, like this:
this->Slot:SetMinimum(FVector2D.ZeroVector);
this->Slot:SetMaximum(FVector2D.UnitVector);
this->Slot:SetPosition(FVector2D.ZeroVector);
this->Slot:SetSize(FVector2D.ZeroVector);

How is the PropertyChanged event used

How is the PropertyChanged event used?
I'm wanting to evaluated the next state of a state machine whenever any property of the class is changed.
Regards Steve
PropertyChanged is part of the standard IPropertyNotifyChanged signaling.
If you want to create side effects if any change is done - consider setting "HasUserCode" on each attribute, Generate code, now you will find a partial method available for you to fill for Set and Get of attributes.
In a setter you can have side effects like running a trigger to step a state machine.

access a variable from level blueprint in unreal engine 4

I Have a variable that updates every time i move my cube in the level blueprint , now i want to access this variable from multiple class blueprints , what do I do , I tried casting to gamestate but didn't succeed , I am really new to ue4 if you could explain in details please
edit: sorry for not adding details ,
The var I want to access is an integer named cube_side that tells me what side the cube is on every time I move , all of this happens in the level bp , I want to access this variable to see what side the cube is on from other class blueprints ->
here are some details in a picture
I know it's not good to code everything in the level blueprint , but it's too late now , I only need to transfer the var cube_side to other class blueprints so the other object can change depending what side the cube is on.
Create an Actor Class for your logic/functionality. Then use Get all actors of class (choose your class) -> Get a copy -> get variable
Communication with the level blueprint is rather tricky in UE4, since they are not as persistent as e.g. the GameMode, and therefore shouldn't be accessed directly (Imagine older games like Final Fantasy where a new level was loaded every time you stepped outside a boundary, so relying on it could potentially break your actors or crash the game due to nullptrs).
It's a little hacky, but works:
Move the variable inside the cube-blueprint. Add an event dispatcher to the cube, if it is moved, call it and pass the variable in.
Select the cube in the editor, open the level blueprint, right-click, "add reference to selected actor" (the cube must be part of a blueprintclass, not only a static mesh dragged in, though), and bind the event dispatcher inside the Level BP.
Create a function inside every blueprint that needs access to the variable, which does whatever it should do, depending on the variable.
The custom event of the Level Bp (that was bound to the Event Dispatcher of the cube), needs references to all actors that have to work, when the variable changes and call each Actors function (you can get the references like you got the one from the cube)
Then, every time the variable changes, the Level BP is notified, the custom Event is executed and this custom event calls all the actor's functions.
Event Dispatchers explained
This is a huge wastage of functions/code, since you only need it for this one level and may never use it again. Avoid this in the future, by not relying on the level BP so much.
You can use GameStateBP to create and store all variables that you need in game, in GameModeBP create functions to get and set this variables via Get Game State function and then function Cast To GameState and then logic. After that from any blueprint access it using Get Game Mode -> Cast to you GameMode -> use your function to set or get data from GameState.

How to keep references to UI elements in a prefab, instantiated at runtime

I have a prefab, which has a script component as MonoBehaviour. This script has 3 public fields as text; which are assigned in the inspector, before saving the gameobject and remove it from the scene.
Now, this works fine if I have a UI element, like a panel. Every text field on the panel, defined in the prefab, is still assigned when the prefab is instantiated at runtime.
This sadly does not work on another prefab that I have made; which is not a UI element. In my case it is a meshgameobject with various components on it (navmesh agent, capsule collider, rigidbody, animator and so on)
I believe this is due the fact that with the UI panel, the elements are already in the gameobject hierarchy, while when the reference is on a different gameobject; Unity does not keep track of them.
This means that I have to always add at runtime via code, the elements that I want to reference on each prefab, if they are not part of the game object itself? In this case I would just avoid to have my references public then, since I won't be using the inspector in this case (every gameobject of this type is instantiated at runtime).
Just checking if there is any other solution to work around this.
Use List to save reference for each generated object.
Lets assume that the object you want to add to store the reference is called meshgameobject.
Example:
List<meshgameobject> meshGOB;
initiaize inside Start function
void Start(){
meshGOB = new List <meshgameobject>();
}
Then create and use list.add() to add reference during runtime like
meshGOB.Add((meshgameobject)Instantiate(prefab, Pos, Quaternion.Identity);
OR
meshgameobject tempGOB= (meshgameobject)Instantiate(prefab, Pos, Quaternion.Identity);
//Do something with meshgameobject
tempGOB.someAction......
then add the reference to the List
meshGOB.Add(tempGOB);
Do not make a perfab which references to other gameobjects in the scene but are not in the hierarchy of the perfabI itself. AFAIK, Unity can not track this kind of references.
Even if Unity supported this kind of prefab, you could not use this kind of prefab in other scenes because the specific references, so it is not make much sense to make a perfab like that. If you have to make it a pefab just because the scene and the game object are maintained by different person, you may use Find() to populate the references at runtime.