Please help me understand scope in Blueprint

Hi guys,

I have run into a problem I seem unable to solve in Blueprint.

I’m creating (or trying to create) a teleporter that will teleport the player to a location of another random teleporter. Additionally, the location teleported to can never be the same as the one teleported from (e.g. let’s say I have 3 teleporters - A, B and C. Teleporter A can only send the player to the location of B and C. Teleporter B can only send the player to A and C, and so on).

Below are blueprints that show how it’s implemented.

Eventgraph

initialize function

Two variables seem to be out of scope. However, I somehow get what I want.

Teleport function

Again, 2 variables out of scope and I suspect this is what causes the error.

I cannot figure out why these variables are not in scope and also how to “put” them in scope. Any ideas or pointers are highly appreciated.

Thank you.

Can no one explain this or give a pointer somewhere?

I am pretty sure it’s because of how BP handles variables. I suspect it’s something similar to this:

To your actual question. What exactly is the error. Just looking over it seems fine but I’m not at all sure what to look for. Doesn’t it move you at all? Does it move you to a wrong location?

And just as a heads up: The way you do it right now you’ll teleport your player whenever anything overlaps with the teleporter… Even bullets or enemies. You might wanna do a check “On Overlap” if it’s equal to your player or go one step further and provide the “Other Actor” to your teleport function so not only your player 0 can teleport but anyone or anything which walks or flys into it. You could also check for character or pawn (character is a pawn as well so with pawn you get pretty much all possible characters or enemys) so it won’t trigger for bullets or environment stuff but for all kinds of players or enemys.

Cheers

Hi Erasio, thank you for taking your time to look through this.

Sometimes the character teleports to a new location and sometimes he’s “teleported” to the current location. I guess it’s about 50/50. The function get’s called every time and the executing is the same every time. Only the outcome is different.
I have checked that no instance of the portal_BP has it’s own location in the portalLocations-array.

I have tried hardcoding a location for testing and that works just fine every time.

Any ideas?

Regarding overlaps and player that can enter the portal. I have that in mind as well. Thank you for suggesting it. Currently I don’t know how to get any character, but you gave a clue to that too, which is awesome.

Ok this seems like an unnecessary thing but try doing the “Get Location” before you compare “Self” with the outcome. It shouldn’t change anything but just to be safe. As a plus it also will disable any actor which is placed exactly on top of another one which shouldn’t be the case every but hey xD

As I said I don’t expect anything to change but just to be sure.

To work with any character you can do something like this:

Oh yea there is a teleport function which also checks for collision and stuff. Set location will work equally well though and since you set the location of actor (the very top parent class) it will work with any character or pawn :slight_smile:

“Unfortunately”, you were right. The suggested changes didn’t change anything.

Just to make sure, I implemented it as you intended I have attached a screenshot.

I tried running through the gates 40 times. The result was 23/17 in favor of teleporting the intended way.

Do you have any other ideas?

Correction### Just to make sure I implemented it as you intended, I have attached a screenshot*. I guess that will get the right meaning across :slight_smile:

Correct me if I’m wrong, but isn’t your PortalBP moving your character to another portal, then this other PortalBP will just teleport you straight back?

Also I think “Random Integer in Range” returns a value >= Min, but < Max. Meaning setting Min and Max to 0 and 1, respectively, will always return 0 (Max should be set to PortalLocations.Length). I could be wrong, but that’s how I remember it works.

Well, I know for sure that I’m wrong, since it’s doesn’t work. However, regarding teleporting back and forth, that’s what the Boolean value “isNotTeleporting” is for. The teleport function is only called if “isNotTeleporting” is true. If the value is true, it’s immediately (and prior to teleporting) set to false, so the character won’t teleport back and forth until crash (infinite loop). Then there’s a 5 sec. timer resetting the Boolean to true, so he can teleport again. So that part should be alright (although it might mess up something else. I don’t know).

However, what you say about random in range might be true. I don’t remember either. Only one way to find out :slight_smile:

Seems random in range is min. - max. (Both included)

Ah no he actually has a point there.

You block a teleporter from teleporting again but not the teleport destination. Then within the same tick you are teleported again with the logic of the destination teleporter.

A text book example for a logic mistake. So super easy to miss if you only concentrate on code :smiley:

Your boolean value “isNotTeleporting” is not shared across all instances of Portal_BP (unless it’s a static member, not sure if that’s possible in blueprints?). Each Portal_BP will have its own “isNotTeleporting” variable.

E.g

  1. Player steps onto PortalA
  2. PortalA.isNotTeleporting set to false
  3. Player teleported to PortalB, overlapping it in the process
  4. PortalB.isNotTeleporting is currently true, so set to false and teleport back to PortalA.
  5. PortalA won’t teleport you back instantly due to the cooldown timer (you will need to walk in and out of the portal since it’s a OnBeginOverlap event)

I tried to add a billboard infront of the portal as a spawn location and add these billboards to an array instead, in order to separate the teleport start and teleport end (in lack of better words) and it works just fine. Now it’s just the rotation that is messed for some reason. I might have something to do with the billboard component. I don’t know.

I’ve implemented it like this.

Regarding the boolean instance not being shared, you are totally right #jaragoondoo. It’s strange though, cause I have been debugging that, and I’m fairly sure that the value was the same across the board. However, now they are not, so I might have seen wrong.

Thank you very much for your time guys. Appreciate it.