Calculate trigger distance problem

Hi There,

I’m having a bit of an issue with a level blue print I’ve developed.

It’s supposed to calculate distance from the final position of the player to their ultimate goal which is a trigger.

now how it works is the player has to:
a) navigate to the same door 3 times in room 1
b) They then have to repeat this in a different room (navigate to a different door in another room 3 times.
c) they are then put back in room 1 and must find the first door after which they are transported to room 2 and must remember where the door is and this completes the level.

d) if they cant find the door they press the space bar which aborts the game and calculates the distance from where the player was to the door.

Now after testing, I’m having an issue with the last bit (part C) as the value in the stored variable I’m setting seems to be wrong and the only way I can get it to work is if I set it manually (as in the picture), what am I doing wrong?

I’d appreciate some help!

@Delta1 What I’m noticing is that you’re moving the collision component. Once you’ve done that, the component will not be where the BP ( box ) is, it will be where you moved it to.

So getting the world location of the collision will not be the same as the world position of the box.

Does that make sense, or am I barking totally up the wrong tree?..

No that makes sense, but what I’m trying to do and possibly failing is getting a variable to update with a location of the trigger this variable is then used to undertake a distance calculation IF the player cant find the exit and gives up

Sorry, I’m missing it, why are you moving collision components? Surely the doors don’t move, and the player does. When they give up their distance is just the distance between the player and the door?..

It’s my fault totally, I’m probably explaining it in a bad way, you’re right the door doesn’t move in each room so for the first bit it can just be set once and left.

However, for the second room its a different door (in a different location) so needs to be moved.

Additionally, for the final bit the player has to return to the first room (collision component has to be reset to the location of the first door) then subsequently they go the second room (collision component has to be reset to the different location value of where the door now is in this room).

I’m absolute pants at coding and because it’s semi working, I left it structured like this, is there an easier way?

Yes, I think much easier way, which is partly why I don’t get what’s going on here :slight_smile:

Set it up so you just have doors with collision components, that’s it.

In fact, for greater ease you can always make that a BP, the door mesh and the collision box. That way, you can just drop a bunch of them in the level and you’re done.

As far as knowing if I player has open or touched a certain door etc, you can just write the door BP so that it updates an array of used / touched doors in a central place.

That central place would be either another BP actor that you drop in the scene ( here it has no visible components, just contains the array ). Or you could use the game instance.

So you don’t need to do anything once it’s set up, no code :slight_smile: And at any point ( when the player presses a button ) you will know which doors have been visited, and how far the player is from any given door just because the door BP can make the calculation :

World location myself ( the door ) / world location ( the player ) / use a vector Distance node.

Does that make sense?.. :slight_smile:

I’m a bit lost tbh, sorry for the hassle but could you please show me what this would look like and it would be awesome if this could be done from the level bp, which is where the guts of my code resides.

Note: this looks like a lot, but it’s really simple, just follow through ]

Hey, afraid I’m not at my machine at the mo, but will try my best to describe it. It’s pretty straightforward, honestly :wink:

First thing is to let go of the idea of controlling everything with the level BP. There are times when it’s appropriate, and others, not. You’ll get a feeling for it.

Try this ( you can leave your level stuff there ).

  1. For now, keep the door simple, just a white block is fine, with a simple frame. So put that together in the editor, select all the parts ( door, frame ) and right click and say ‘convert to blueprint’

  2. Now you have a BP with the door parts in.

  3. On the components list ( top left ) there are green pull down menu, Add Component… From there choose collision… cube.

  4. It will be added to the BP at it’s center and may be small. You can use the same way you normally do in the editor to make it the right shape for the door.

  5. Add an Int to the variable list and make it visible in the editor ( click the eye next to it ). Call it DoorNumber.

— There’s one more thing to do here, but we’ll come back.

  1. In the content manager, right click on the back ground and choose ‘new blueprint’ ( type Actor ). You can call it something like DoorManager

  2. Open the DoorManager BP. Make an array variable called Doors, which is just an array of Int

  3. In the code window, right click on the background, choose CustomEvent and call it something like ‘DoorUsed’

  4. With this node selected, in the details panel ( on the right ), you can see a plus sign where you can add a parameter, call it Door and make it an Int

  5. Now, you connect a bit of code to the custom event. All you have to do is drag your door array into the code area ( select ‘get’ ) drag a pin from it and choose SetArrayElement.

  6. The parameter that comes in is the door number, so you can just set that array element to 1 ( or add one for each time the door gets used ). Make sure to tick the Resize box on the SetArrayElement.

— So, you have a way, in the door manager of ‘servicing’ the calls from the doors. You can set an array element to one, or increment it. That’s fine for now.

  1. Back in the door BP, make a new variable of type DoorManager ( or whatever you called it ). Again, make it editor visible ( click the eye ).

  2. In the code area, right click on the background and find the BeginOverlap event. It’s called something like OnActorBeginOverlap. If you’re not sure which event, click the collision box in the components list, and in the details, there’s a big list of wide green plus buttons, I think the top one is beginoverlap.

  3. This gives you to overlap node. You can also drag a copy of the DoorManager variable in from the variable list ( choose ‘get’ ) when you drag the pin from the door manager BP and type in the search box, you’ll find the event you made over there called DoorUsed :slight_smile: Also grab a copy of the door number variable, and connect it to the custom event call.

  4. So, what we’re saying in the door BP is, when something overlaps with my door, call the door manager BP and say ‘hey, I just got an overlap, here’s my door number’

– Now we go back to the main editor window ( the one with your level in ).

  1. Place ONE copy of the DoorManager in the level. It just looks like a white football.

  2. Place a few copies of the door BP you make. Notice what happens when you click on one. In the details panel on the right, you can see slots for these variables you made. Namely the DoorManager and the DoorNumber.

  3. Tell each of the doors which door manager you’re using. Just click the slot in the details panel for the door manager, and you’ll see you get a drop down with only one thing in it, that’s because you have only one door manager. Cool. Fix the other doors too. What you’re saying here is ‘This is the door manager I want you to use’ You might be thinking ‘surely it’s obvious’. Nope, the BP doesn’t know, for all it knows you could 500 doors managers, it wants to know which one.

  4. Also, when you click on a door, in the details, you can see a space to set the door number. Just set them to 0 - 3 ( or how ever many you have ).

— Run it! :slight_smile:

What happens? Apparently nothing. But you know better. Every time you walk up to a door, it ‘calls’ the door manager, and it keeps a count ( if you coded it that way ), of how many times each door has been approached. The index of the array is the door number. Put a print string in your door manager so you can see when the array gets updated.

So what I’m showing you here is, many things actually, but principally:

  • How BPs talk to each other. There are other ways, but this is one of the main ones.

  • A way to construct a complex level with many doors, but you don’t have to think hard AT ALL. You only coded one door, and the thing that ‘listens’ to them.

  • How to centralize the management of the doors, you don’t need lots of squiggly level BP code, just a few lines in the right place ( no moving collision boxes :wink: )

  • It’s called ‘instancing’ when you drop those doors into the level. You wrote one door, buy you get to re-use it many times at no extra effort.

I hope you found something useful here, and I hope I got SOME functionality you’re looking for included. If not, you can easily adjust I hope.

If you get stuck quote me ( use the quote button when posting ) otherwise I don’t know you’ve posted. I wont actually be at a machine with UE until middle of next week, but we’ll get it sorted.

Greetings…

Thanks for that clockwork I’ll give it a shot, I just need the calculation to work on keypress initially.

Really appreciate your help