I’m trying to get an AI to move to the location of an object in my scene. I’m trying to use an interface to pass the world location of the object into a behavior tree task.
The first image is inside the object I want my ai to move to. I get the world location in a vector and then send it out using the interface called ‘In Bed Location’. This is working as I tried printing the result and it shows the correct xyz of the object in my scene.
Second image is inside the task of the behavior tree. I’m trying to call the event from the interface ‘In Bed Location’ and apply that to an AI MoveTo.
It looks like you are resetting the bed location as you call it from the 2 events so.
Leave the set bed location ONLY attached to the event recieve bed location
On the event recieve ececute drag in a get bed location node and attach to destination
Otherwise if you use it as is every event receive execut ai will set the bed location and theres no input from anywhere so its value will be defaults 0,0,0
I’m new to UE5 and just starting to wrap my head around interfaces. My understanding is in this image I’m sending out the location of the bed using the custom function I made in the interface. And the node with the envelope on it sends out the location.
Then in the AI task I’m calling that location using the Event In Bed Location
How are you running your behaviour tree? Somehow you jneed to tell it to move only when given a location i would guess. Can you show me your behaviour tree so i can see please?
I have a feeling youre using local variables to the functions so once it leaves the function the variable is unset but i might be wrong.
And usually in this sort of system you use a blackboard for your variables.
I think your right about the variables. I have one in the bed BP is set by getting the world location of the box collision . I thought using an interface I could pass this through to another local variable in the Sleep Task but that doesn’t work.
I have a Blackboard in my behavior which I was using for flow control… I removed it while I was troubleshooting the location issue.
I haven’t seen any info about using blackboards to store variables. If you know of a good tutorial I would love to lean more.
Looks like you are mixing up interfaces for global variables or something.
Your bed is calling interface method InBedLocation on itself (you pinned Self as target), which probably doesn’t make much sense.
Your BTTask implements the interface, which doesn’t make much sense either as I highly doubt that other classes are supposed to/capable of getting a reference to a BTTask to call an interface method within.
It’s hard to tell what you exactly want to do, and I can’t really help until you answer the question about how things are supposed to be linked together :
If there are two beds in the level, which one should the AI move to ?
Is there a specific event that causes a specific AI to want to move towards a specific bed ?
What if there are multiple AIs, do they all move towards the bed ?
This Receive Execute AI and “Event In Bed Location” combination, is really, really incorrectly wired.
You’re calling In Bed Location event in BP_Bed.
You’re receiving it it BTTask_Sleep.
This doesn’t make any sense.
Even if you’re implementing the Interface Bed Location in the BTTask, you’re still not ever calling it in the BTTask. I don’t know how you would even achieve such a thing.
I think you’re thinking that an interface is some kind of global event receiver system. It is not.
What you need to do is find the Bed Location, and get it to the AI Controller’s Blackboard.
I don’t see any way to get there from here, though, you’ve just got the Tick function of the BP_Bed telling itself where it is every frame.
Answer to your questions.
If there are 2 beds in the level which one should it go to. I hadn’t really thought of that but is suppose it would decide by which one is closer.
Is there an even to get the ai to move to the bed. Yes then plan for this is to implement an energy system, so when the ai is tired is seeks out the bed. Right I’m using if ai sees node while I’m trying to get it working.
If I ever have more than one AI I will implement a check to see if it’s empty then the ai may move into it.
I hope that makes sense. I’m new to unreal so I really appreciate any help I can get.
So by that logic, it should be the AI that is looking for a bed, rather than the bed telling the AI where it is. You CAN use an interface, it is not required, but it is decent practice to avoid accumulating dependencies among actors. In this case we can use an interface we’ll call BedInterface, which doesn’t provide any functionality but can be used for filtering actors when AI needs to look for beds. Implement BedInterface in your BP_Bed blueprint :
That’s it. Nothing required in the BP_Bed event graph.
Now you want to tell your AI to look for a bed. I’m not gonna do the full conditional behavior tree for you, just show you the simplest possible approach : a BTTask that seeks a random bed in the level and makes AI move to it.
This should be a starting point.
From that you can expand complexity step by step to reach your desired goals :
Add an energy variable in the AI controller, reflected in the BT Tree
Add a behavior branch that conditionally takes priority to look for a bed/resting place when energy is low
Find nearest bed instead of random
Add a boolean variable to the bed blueprint, to keep track of whether bed is free or not, then add interface methods to the BedInterface (and implement them in BP_Bed), to help communication from the AI/BTT blueprint.