Grid-Based Room Generation Tutorial

I'm learning a lot while working on my game in Quest and thought that I share how I implement some of the features within it through video tutorials.

The game is sort of an "open world" (can that even apply to text adventures?) sandbox type so I implemented grid-based room generation, which is what my first finished tutorial series is about.

https://youtube.com/playlist?list=PLL0F4OU1xPG0icNThd34UF239BGoxZu7-

I would appreciate any criticism that would help me improve. I just ask that you excuse the sound quality for now as I don't have a proper microphone just yet.


I appreciate the video lessons. Thanks. They were really neat to follow.
They were exactly what I need to reduce work in what I am currently playing around with.
I don't know very much about code, so you've really helped me with something.
I hope you create even more videos, one day. I've subscribed, just in case you do.


Thank you! I'm glad that you found them helpful. I plan on uploading at least 1 video a week. Thanks for being my 1st subscriber @Jennifer Wren!


I just followed the tutorial on room generation. It was a truly amazing experience! I will follow the other tutorials in a day or two.
Then I will have all the exits.
I am hoping to find a way to incorporate what I learn into the game I was trying to make earlier.

One thing I am hunting for, and was finding hard to find, was a script to substitute (or supplement) the arrows on the compass with the arrows of the keyboard. If you know of a method for that, maybe, you would be willing to make a video on that, one day. I swear I've seen it somewhere, before (unless that was another game engine).


One thing I am hunting for, and was finding hard to find, was a script to substitute (or supplement) the arrows on the compass with the arrows of the keyboard. If you know of a method for that, maybe, you would be willing to make a video on that, one day. I swear I've seen it somewhere, before (unless that was another game engine).

I'm pretty sure that's been posted on the forum before, but I can't find it now… I think it should be a pretty simple modification to the commandKey JS function (which by default makes the up and down arrows cycle through previously typed commands).


Thanks, MrAngel. Code is still pretty new to me, but the message gives me hope. I will see what I can do with that.


I figured out how to use arrows keys to move. I'll try to have a video up either tonight or tomorrow but until I'll post it here until then.

  1. Create a .js file and put in your game's directory. Add the following code. You can add the .js file to your game by going to Advanced -> Javascript.
document.addEventListener('keydown', function(e){
var exit_dir = "None"

if (e.keyCode = 37) {
	exit_dir = "west"
}
else if (e.keyCode = 39) {
	exit_dir = "east"
}
else if (e.keyCode = 38) {
	exit_dir = "north"
}
else if (e.keyCode = 40) {
	exit_dir = "south"
}

if (exit_dir != "None") {
	ASLEvent('MoveCallback', exit_dir);
}

})

  1. Create the MoveCallback function. Give it a single parameters 's' and add the following code.
exit_dir = s
  1. You'll need to add more code to MoveCallback but that's dependent on how you got your game set up so I'll list the basic steps.

3a. Get all the exits that lead from the current room and that go in the same direction as exit_dir.

3b. If there's only 1 exit, you can just move the player. If there's multiple, you can have them choose from a list first and then move them.

I tested this using the same project that I used for the tutorial. There's only one exit going in a given direction at a time and the exits are named so that I can easily access them. The MoveCallback function for me looks like:

exit_dir = s
// Get exit obj
exit_obj = GetObject(exit_dir + "_region_exit")
// Get exit target
target = exit_obj.to
// Move player
player.parent = target


This is fantastic! I look forward to following your tutorial. Will you also create a tutorial for generating a map of the Region rooms? Can a game of this type have more than one set of regions, like up and down levels? I am assuming rooms can be treated one at a time, using the coordinates, somehow. Otherwise, would adding an up exit to Region as an object affect all rooms, or just the mapped rooms?


Yes, I was planning on doing a tutorial drawing a map. I just have to get a working version up first.

You can generate a game world that goes up and down. Its the same process, you just have to add a z coordinate.


That sounds like it should work, as long as you don't have locked doors, effects that block movement, or doors with scripts on.
It would probably make more sense to have something like:

document.addEventListener('keydown', function(e){
  if (e.keyCode = 37) {
    compassClick("west")
  }
  else if (e.keyCode = 38) {
    compassClick("north")
  }
  else if (e.keyCode = 39) {
    compassClick("east")
  }
  else if (e.keyCode = 40) {
    compassClick("south")
  }
});

Then it acts as if the player clicked on the compass, including giving the relevant message if they can't go that way for any reason.
(I assume you were right about the numbers for the keys)


I went to double-check the key codes and paste in mrangel's code and I realized that I've made a mistake somewhere. Both our of codes work exactly the same way but the player moves to the wrong room. No matter which arrow key is pressed the player always moves west. Using the compass stills allows the player to move to the correct room though.

Nevermind, I figured out what the issue was. Forgot the other = sign in the if statements. So the code should be...

document.addEventListener('keydown', function(e){

if (e.keyCode == 37) {
	compassClick("west")
}

else if (e.keyCode == 39) {
	compassClick("east")
}

else if (e.keyCode == 38) {
	compassClick("north")
	
}

else if (e.keyCode == 40) {
	compassClick("south")
	
}

});


I figured there would probably be a z-coordinate, but then every level has to be exactly the same? I suppose detailed work with the individual room coordinates will make invisible some exits one at a time. Is that right?
̶A̶l̶s̶o̶,̶ ̶I̶ ̶a̶m̶ ̶e̶x̶c̶i̶t̶e̶d̶ ̶a̶b̶o̶u̶t̶ ̶t̶h̶e̶ ̶d̶o̶c̶u̶m̶e̶n̶t̶ ̶,̶ ̶b̶u̶t̶ ̶I̶ ̶d̶o̶n̶'̶t̶ ̶a̶l̶r̶e̶a̶d̶y̶ ̶k̶n̶o̶w̶ ̶w̶h̶e̶r̶e̶ ̶t̶o̶ ̶p̶a̶s̶t̶e̶ ̶i̶t̶.̶ ̶I̶ ̶w̶a̶s̶ ̶a̶b̶o̶u̶t̶ ̶t̶o̶ ̶t̶r̶y̶ ̶a̶ ̶g̶u̶e̶s̶s̶ ̶a̶n̶d̶ ̶t̶h̶e̶n̶ ̶d̶e̶c̶i̶d̶e̶d̶ ̶t̶o̶ ̶w̶a̶i̶t̶ ̶u̶n̶t̶i̶l̶ ̶I̶ ̶k̶n̶e̶w̶.̶ ̶
I have the keys working! I just had to read the instructions and create my first ever .js.
Meanwhile, I have cats in some of my rooms and mice in some of my rooms.


Yes, every level would be the same. If you don't mind sharing, what type of game are you trying to make? Because the answer to your second question is technically yes but it depends on what you're trying to do and how you do it. If I had more details I would be able to better answer your question.


I had wanted shaped levels.
I guess it makes sense to just use the room names and write individual scripts to make exits invisible. I am starting to understand how it works at that level, at least. I think I can sit down with the code and figure out how to create a script with < and > or <> and =, maybe. (Maybe not, but I can definitely use the room names one at a time.)


If that's the case, its probably better for you to make a list of coordinates that you don't want generated. During room generation, when the function gets to those coordinates, skip that room. If the room isn't going to be used, then there's no point in it taking up memory. Granted, it probably won't make that much of a difference depending on the size of your game. You'll have to tweak some of the other functions too as now there's a possibility that they won't return a room because it wasn't generated.


I could just make two levels and use some rooms as cellars to the upper level, and other rooms as attics to the lower level, and use the invisible exits to make two shaped levels with use for all the rooms.
I have to confess, I had no idea what to do with the msg(coordinates). I pasted it into the entering rooms script. The game just gave me an error that said it didn't know what coordinates are, but I might not still run into the original error since I have unticked the map.


I went to double-check the key codes and paste in mrangel's code and I realized that I've made a mistake somewhere. Both our of codes work exactly the same way but the player moves to the wrong room. No matter which arrow key is pressed the player always moves west. Using the compass stills allows the player to move to the correct room though.

I should have spotted that; I just copied the framework of your function and changed which function it calls inside.


I have figured out how to change the alias of specific regions, but I have not been able to, so far, find a way to add any other script to these rooms, like a room description to go with each alias. I can make random changes to random rooms, but don't know how to add a specific "after entering script" to specific regions. I tried attaching the room description as an msg to the alias script, and got everything printed at once in one room.


I don't know how to add scripts retroactively like that. If its really important that you have specific things in specific rooms, your time is probably better spent just manually creating the map and adding objects/scripts.

If the description is simple text, you can use the Get_Region_By_Coors function to get the room and do room.description = "You room description here.".


It actually didn't work at all. I have it under all the other script of after entering room, in Region. Maybe, the if statement should be different? Thank you for all the help by the way.

if (this.y = 0) {
room. Description = ""
}
else if (this.y = 9) {
room. Description = ""
}
else if (this.x = 0) {
room. Description = ""
}
else if (this.x = 9) {
room. Description = ""
}


When inside of a script attribute you can use the this keyword to refer to the object that the script is attached to. If you're working inside of the enter script, this.description = "Your room description here." will work.

Instead of using an if statement, its better to get the exact room obj you need if you know the coordinates.

room = Get_Region_By_Coors(x, y)

room.description = "Your room description here."

But if you want to use if statements, you have to do it like this. Replace the x and y after each = with x,y coordinates of the room you want to add a description to.

if (this.x = x and this.y = y) {

this.description = "Description"

}

else if (this.x = x and this.y = y) {

this.description = "Description1"

}

You're welcome. I struggled a lot when I first started using Quest and I was able to get help from others by posting here so I'm just paying it forward. Learning the basics of programming will definitely help though. Once you know that you'll be able to program any feature you want in your game.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums