Set object reference during or after spawning class

My blueprint class is an Actor with two custom components : A and B. It is for testing purposes.

A contains a reference called “BOwner” to the Owner to whoever contains component B (In this case it is the same Actor, but could be some other actor in the future).

The reference is this:

UPROPERTY(EditAnywhere, BlueprintReadWrite,, meta = (DisplayName = "B Owner", ExposeOnSpawn = true))
	AActor*	BOwner = NULL;

Then I added a keyboard trigger. After pressing SPACE , this blueprint class gets spawned on the scene.

My problem:

After the actor is spawned, I need to set the BOwner reference.

I tried:

  • Defining a function that creates this
    structure by hand instead of using a
    Blueprint class . It spawns actor →
    adds B → adds A and I connect the
    BOwner pin to the actor already
    created before
    . But this never
    works, and it is always NULL even
    when the actor and both components
    are created.
  • On the previous setting, I tried creating a variable SET right after
    creating that actor to store the ref.
    Then the execution goes to the
    creation of B, and then the creation
    of A. In the BOwner pin I put that
    variable with a GET. It doesn’t work
    either, it’s still NULL. (???)
  • Then I created the structure on a BP class. But I still have the problem that I need to set this BOwner afterwards.

I have no idea how to modify BOwner. To get the reference to the actor I used “getOwner” on component B, and now I have a reference to the actor… but now I have no idea how to set this variable on BOwner on component A since it is created within the blueprint class.

[EDIT]

I managed to make this work, but I still don’t know how to fix those.

  • Add a setOwner function (a setter).
  • Add UFUNCTION(blueprintCallable) to it
  • On the creating script of the blueprint attach to a call to that function using self (the blueprint) as the item passed to the funcion, and the component as the target.

It depends on how you spawn those actors and if you add those components dynamically. Best is to do so during creation on both, you just take references from SpawnActor nodes. The bigger problem is if they are not created at once, then you way for them to interact with them, in worst case scenerio you will need to use TActorIterator or “Get All Actors of class” to search for actor with B component, or make B component search for actor with A. But if you got multiple actors then you will need to find way for them to interact and connect. If you just place actors in the level editor with those components… then it is super easy :stuck_out_tongue: just place them and set reference inside details tab, you can refrence any object on the level, same as in Level Blueprint.

You don’t really explain what those actor and components does, so i really can’t give you proper solution aside of those general ideas. Again best way to do so with dynamic create actors and components is to find way for them to interact or best moment for that, where they can exchange references with each other during some event where you have A or B gets reference of another, other wise you need to search the level .

I made it work, but the way I did it was using a setter function. I added it at the bottom.

My main two problems are not clear, let me summarize:

  • Create Actor → Add component B to It → Add component A to It with the previous actor output pin connected to BOwner pin. -----> stays NULL even when the actor was created already at this step.
    Is having NULL in the default declaration of that property affecting?

  • How to modify a property inside an object reference?. I’ve seen this image on a book. I think what I need is that little node that looks like getting Name from the target given… that’s a getter. How do you create that node? and how to make a setter?

221697-captura.png

“previous actor output pin connected to BOwner pin” what you mean by that? how you get previous actor? you do this in previous actor?

You can get Get/Set nodes for properties by grabing object pin (in case of image pin form MyWarrior) and drop it in empty space, you will get suggestions of all fitting nodes you can connect, and there will be set and get. Your variables need to be public for them to be set and get externally, you do so by opening eye icon near varbales on varbales list

About set and get, I’m confused. I couldn’t make that work, maybe some other problem. I’ll try, thanks!

About the first issue. I mentioned :

Create Actor → Add component B to It → Add component A

In the node “add component A” , on the BOwner pin, I connect the output of Create Actor node from before. That should return the instance of that actor.

But after I run this graph, BOwner is NULL anyway. I tried setting a variable with the actor reference inbetween:

Create Actor → SET VAR → Add Component B → Add comp A. And then use that variable on the Add Component A’s BOwner pin… but still NULL… ??¿¿

Ok. I was confused. That’s a get node, the one in the middle, I guess…
About the actor, I spawn the actor. Then connect the output pin (actor instance link) to another node that is executed afterwards and uses an instance of that actor. That’s the problem. That is always NULL, but it has been spawned before… I tried assigning a variable inbetween… but it’s the same.