Access BP component from character BP

Hello!

My question may be trivial as I’m getting back to unreal, and I just can’t figure this out while it feels quite basic, that’s annoying.

I need the character to do a line trace, then checks if it hits a specific collider component from another BP (that’d probably have multiple instances in the level).
So I was thinking the correct way would be to store a reference to this BP class in the char BP, then access the said component and compare with the line trace hit, and then I would use the component to find the parent actor instance in the level for other things.

Something like this :

But I can’t manage to get a reference to the BP in the first place. I tried creating a variable referencing the actor but no surprise it’s “accessing none”.

How to proceed further / is this the right way ?

Thanks

hi @Waz0

if your reference to the target BP is sound then thats not your problem.

Your problem (which i see alot) is you are not checking if there is a hit first. because of that you are trying to access a hit actor (or component) that does not exist which will throw Accessed None errors.

Straight after your “Line Trace By Channel” node execute a branch with the “Return Value” (Red pin of the line trace node output) as the value. only do what you need if TRUE, otherwise if its false then there is nothing to access!

Well, that couldn’t be simpler :smiley:

Not a reference issue indeed. I’m still getting the errors rn but they’re not blocking anymore. That’s a progress, thanks!

1 Like

Just check you havent got more code above what i could see that access other objects that aither havent been set or not detected

Hello again,

So the initial problem persists. I just shifted the test from component to actor to make it simpler, and the ref actor variable still doesn’t match the “hit actor” returned by the line trace, and/or passes the valid test. Though when printing the hit actor it returns the right bp name :disguised_face:.

As additional info, testing if the ref actor is valid at begin play returns false.
So I tried an extra step at setting the ref, seen in another thread, but the execution seems to stop at the cast node, so nothing will execute past it.

So the answer to that one is its NOT a BP Point Light actor.
so on the Cast Failed pin put a print node and drag off the return value of that Get Actor node and it should get the object name. Lets see what it is!

You mean like that ? It returns PlayerController

So you cant cast a controller to an actor. What is this actually doing? Im guessing you need to set the references (unless they are components of your actor) before you can do anything with them. and this code shouldnt be in the player controller!

edit: Oh i do see a problem, you are validating 2 objects with an OR but then checking BOTH objects when only 1 is valid. So you need to rethink that part!

That will sequentially check each Object Reference to make sure they are valid before comparing them

That’s what I stated in the original post, that I tried storing a ref to this LightBP in the chara BP so i can identify it later on. The later post with the cast was just an additional try. So having the PointLight ref in the chara bp is not the right way ?

For the check after the line trace, I’ve tested IsValid and IsEqual separately, also reducing the tests to one BP ref (point light). It’s returning false in every configuration…

Yes you can reference that actor in your BP thats correct. But make sure its valid and check its valid seperatley before trying to run code on it. Your original checks check BOTH objects uising an OR so if only 1 is valid it will pass then you try using an equals but the other object may not be valid thats why you check them seperatley.

How are you storing you 2 light references? where is the information coming from in editor setting? or code?

I’ve created two variable in the chara bp, each being an object ref of two other bp (PointLight & SpotLight)

image

With this simple test, IsValid returns false

image

I take note of your remarque concerning the logic after the line trace, but it seems the issue is somewhere else ?

because they are kinda like variables.

Ok best way to find out what you need is this:

Do you want to know the actors TYPE or Class that you hit? or Are you storing a spawned or placed actor in the world?

It looks like you want the class type which is done a little differently
but i may be wrong

Yes!

And in a second phase, get which actor from this class it’s hitting in the scene, maybe get its component. But I think I can manage that part.

Edit: Ok so from what I read, referencing the class would be the right way. What’s confusing is that when I’m setting the object reference in chara BP, it looks like I’m referencing the BP class, as there’s only one actor in the list, although there’s multiple instance of this BP in the scene :woozy_face:

ok 1st

You have 2 object references, change them to BP_Spotlight Class reference (the purple one instead of the object reference) see example below, do same for the BP_Pointlight variable too!

So they should have the CLASS reference for their appropriate actors!

it does not show correct node colours on that site for CLASS but see image below:

1 Like

Thats fine it detects its a light, but then the Hit Actor still holds the information of the EXACT light you hit so you can control it directly or whatever u need to do! so just drag off the Hit Actor again in your code

Thanks for your help again! It’s lunch time, I’ll keep you updated with the result later today

1 Like

Ok it’s working with the class reference :slight_smile:

I have a last question, that’s a follow up to the above and addressing class reference so I believe it’s relevant to this thread :

I’m trying to find if a world location is inside a collider, component of this same class BP. All I could find is UPrimitiveComponent::GetDistanceToCollision, which seems c++ only ? and seems to be searching for a specific actor instance component, while at first I’d like it to be “agnostic” like with class reference.

For the first half of the process, I think I could spawn a collider at the location and test if it’s overlapping the other class component, but it sounds like a less optimal solution ?

So far I didn’t find how to drag a component out of this class reference, maybe it’s not possible ?

yea that’s three questions in one i guess

no u cant drag out of it, you need an actual actor to get what you want OR if you havent got a specific actor yet then an object reference you can work on, just make sure you set the reference to an actual actor before you run the code or you will get access none errors.

You can get distances to all kinds of things. does it have to be “inside” a collider? could you not check if youre near to an actor of class (Get All Actors of Class) then check if your within the collider range with a little math?

so say you want all the BP_Spotlights and you want to check if youre “within range”

you use Get All Actors of Class set BP_Spotlight as the class. This will then give an array of all those spotlights. For Each every one and use “Get Distance To” then work out if that distance - the radius of collision < 0 if it is youre within range.

Also another thing you could try (not sure if it will work though) in the spotlights or whatever actor it is, have a timer that checks every say .5 of a second ( change to suit)
enable the collider then disable then enable etc.

Im wondering if it will re register the overlap when its enabled?
kinda like a radar pulse

Ok that’s clear. So I need to check from an array of actors, it sounds heavy but I can try this

In the case of the spotlight yes, because its collider is a cone, so the area isn’t uniform like a point light.

I took a look at the “get distance to”, but it’s only taking an actor as input, and I guess a specific location instead of a “mesh defined” area ?

Yeah its not exact, so a bit of math on the cone angle and length maybe , but the spotlight BP is an actor and you can then get the components of that actor.

There is another way if its detection, surely the BP_Spotlight can do it? Perform ray traces based on a sphere targeted at the end of the cone roughly matching its radius.

If you can explain exactly what you want to achieved and the actors involved then maybe I can give a bit more than just maybe this what about that. its a bit vague to you that way, because its vague to me understand? might not be til tomorrow now as its late here sorry!