0 Favourites

How do I make the player randomly teleport between doors?

  • I searched a lot but couldn't find anything too specific. You'd better check the image, should be easy enough to understand what I want to do:

    Objects: Player(0), Doors(1, 2, 3)

    What I need is entering to the door 1, then getting out on, ideally, door 2 or 3, but any of the 3 will do for now.

    My logic says that:

    When the Player is overlapping Doors, press a button then set position Player to Doors UID's X and Y.

    But I have no idea how to randomly take one of the 3 UID. I tried with a variable but it says "variable is not an object name".

    Note: it's very important that the UID is randomly selected everytime I press the button, not on start of layout.

  • You could store the UID of each door in an array then pick it randomly from there..

  • notnsane there are many ways one can do this but, here is simple demo to start you

  • I kinda have a "NO ARRAYS PLEASE" philosophy so I try to avoid them like the plague, lol.

    Thanks to both of you, testing the capx now =)

    @Lordshiva1948 It's really similar to what I want, but as much as I try, I can't make it work with only one object. I replace tr with spawn and it always comes out in the same place. Then do it otherwise, I add the instanced variables to tr, and the same thing happens. It always comes out on the same "door".

    I want to avoid using two objects for this, else I would have to overlap them and then make one of them invisible. Could also create other issues, I think.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Easy enough, use the system choose parameter.

    If player overlap door 1 then set variable randomDoor Choose (1 , 2, 3)

    If randomDoor = 1 then spawn player at Door 1

    if randomDoor = 2 then spawn player at Door 2

    etc...

    Sorry, I'm at work. If you need a Capx example then let me know. I'll do it late tonight or tomorrow morning (eastern time).

  • To add on that, you could break it down by case of over lapping doors,

    Eg.

    If player overlap door 1 then set variable randomDoor Choose (2, 3)

    If player overlap door 2 then set variable randomDoor Choose (1, 3)

    If player overlap door 3 then set variable randomDoor Choose (1, 2)

  • Thanks for the help guys, but this still doesn't work as intented.

    Maybe I wasn't clear enough but I need to use only one object besides the player. There's no point on having duplicates of duplicates.

    I tried using variables now but I don't know if to use instance variables or global variables. Or how to select a specific door (because the action should be triggered with Any door or I'll turn more crazy) Any help appreciated.

    @mepis, can you elaborate your example? Also I don't mind having to choose like that, but my game will have like 10-15 doors, so it might be annoying to type, say "choose(1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)" for each door (deleting and adding one number, obviously)... but if there's no other way, then no probs =P

    Made a new mockup to make this idea more "readable". The only objects here are "player" and "doors". Of course each door has a specific UID (created all of them with Ctrl+Click except the first one)

  • Well, notnsane, I think I've got what you want. I even kept your colors. The player has platform, press down to go through a door.

    What it actually does is teleport the player to a random door including the one he started at, but moves him again if he would appear at the original door. The UID of the origin door is stored as a separate instance variable in the player, and compared against the destination door.

    What's handy about this over the Choose(1,2,3....) version is that you can dynamically add or remove doors to your heart's content*, and it won't change the algorithm at all. You can even just drop it down to two doors to verify that it'll never drop you back at the door you started at.

    You may note that the doors and floors all have physics. Frankly, that was easier than aligning them all with the floor manually. You can remove them and it'll still work just fine.

    Edit: Sod, left a bug in there. If you walked to a door after teleporting from it, you'd teleport again.

    *Provided your heart's content doesn't include a single door. That will cause an infinite loop.

  • This will avoid the entered door, slight mod to yours hope you don't mind

  • That's fantastic, unnatural20, exactly what I was looking for. It seems that the task was quite complex, considering the events' logic. It took me a while to understand it completely and I'm still confused about the sub events... but it makes sense. I also tried to nest or "de-nest" the sub events but it only worked correctly the way you nested them.

    Thanks again =)

    Oh, codah's edition is a bit cleaner but it's harder to understand for me. Seems like I need to learn a lot about picking

  • This will avoid the entered door, slight mod to yours hope you don't mind

    Absolutely not, this teaches me more about Picking!

    @notnsane Picking, inside of a single event, happens from the top down.

    So in codah's capx,

    First, all doors are picked;

    Then, doors where the UID < > (does not equal) the UID stored in Mans are picked (out of all);

    Last, one random door of the remaining set is picked, and the Mans is moved to that door.

    However, unlike in my .capx, this only occurs when the down key is pressed and Mans is overlapping a door. v. efficient.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
167 14,485
DMT2005's avatar
DMT2005
Unread hot topic
0 Favourites
[plugin] Youtube player
175 20,536
rexrainbow's avatar
rexrainbow
Unread sticky
1 Favourites
C2 Games made with CocoonIO
136 89,811
minhcuong's avatar
minhcuong