So Im trying to change an integer value from the blueprint that gets created for the player character anytime you start a third person template of another blueprint that I have all values are public. My problem is the instance of the player gets created on runtime so its impossible for me to make a reference to the other object using the object variable the variables value cant be set before runtime as the instance does not exist so if I try to cast it will fail. Is there a way to make the instance of character exist before runtime to set the value and if not what else can I do.
Hey @Lucifer1234556781! Welcome to the forum!
So I think what you’re trying to achieve is 100% doable, based on what I’m able to gather. However, your post is quite jumbled; if you could iron things out, maybe make a bullet list of individual parts, I could help you more accurately.
But some things I think may help: You can use an IsValid node to check if a variable is set. If it is valid, you can then reference using that variable, if it is NOT valid, you can simply bypass the code using that variable. That should help with your Null Reference Exception.
As far as setting a reference in runtime: upon spawn of the object, you can have the object get: PlayerPawn and cast to ThirdPersonCharacter, then as thirdpersoncharacter SET the variable you’re looking for.
Blueprints are “classes.”
You’re saying that you want to change a variable of some other “class” but that doesn’t
make sense – you change values of “objects” which are instances of classes.
So, something creates the instance (object) of whatever that other object is. If you want to change this from the player’s instance, then you need to get a reference to that other object somehow. The way that typically happens is based on how objects “hear” about each other. For example, a trigger box on some object might notice that the player entered, and call some method on the player, which lets the player know. For another example, you can do “find all objects of class” to find all objects of some class. This is often somewhat slow, but sometimes the right choice.
Once you have a reference to the object in question, you simply call the “Set Xxxx” on the object in question.
Note that the Unreal gameplay system already has a bunch of default objects you might want to use. There’s the Game Mode (server only,) the Game State, the Player State, and the Player Controller, for example. Read up on the gameplay framework for more information on when to use which kind of object:
First thank you for your response. I believe I wasn’t clear in my question previously so I hope this better show what I’m trying to do. So when you start a new third person project in unreal it creates a player with the mesh movement some other things and a blueprint class called “BP_ThirdPersonCharacter” which I want to use to set up the some code such that when the left mouse button is clicked a variable called “health” inside another blueprint called “Enemy1” to change, get lowered. What I’ve been trying to do is to use the event of left mouse button connect it to a “cast to enemy1” node, this node requires a variable, “object reference” which I have set to the parent of enemy1 blueprint. However the value of this object reference never gets set since under the outliner there is no instance of the “BP_ThirdPersonCharacter” meaning there is no instance of it inside game world it only exists after runtime in which case I cannot set the reference and change the value so the cast will fail. However my main goal is to be able to pass values between two blueprints so if there is other ways that I can do that please let me know.
Hey again! So there are a few ways you can do this: It’s all about communication between actors.
The best way to get the ThirdPersonCharacter you are playing as would be:
On the Enemy’s BeginPlay event:(right click open space) use “GetController <0>” → “GetPlayerPawn” (this means you now have the playable character’s Pawn) → “CastToThirdPersonCharacter” → (Drag off of As TPC) “Set Enemy1” with an input of “Self”.
So what this will do: When the enemy is spawned, it looks for the player blueprint, assures that it is ThirdPersonCharacter_BP, and then sets itself to that variable. Now your reference is set, and your left click should be able to reference the enemy!
There are other ways to go about it, such as aiming the mouse, doing sphere trace to get closest enemy, etc. But that is more advanced. One step at a time!
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.