The Goal: Remembering Where We've Been
Solving the Problem Using Nested Arrays
How do we move from map to map, loading them randomly, but return to specific maps if the player turns back.
Let's go through the process in player and program terms using human language, step by step:
Player is exploring an unexplored area and adding each new location to a map.
The program tracks various pieces of data that are all necessary ingredients. From a programming perspective, it's worth creating these variables in a separate one time event when the game begins:
- Player's Current Local X and Y coordinates. (that's two variables one for X and one for Y) (In this thread we've already come to the solution about how load maps randomly, and how to automatically place the player in the new map at the correct position, read above for the answers.)
- Player's Previous Local X and Y coordinates. (Again that's two variables) (In this thread we've already come to the solution about how load maps randomly, and how to automatically place the player in the new map at the correct position, read above for the answers.)
- Player's Future Global X and Y Coordinates (two variables) (I'll explain later) (imagine the game maps are put together like a quilt - each map is sewn on based on what X and Y coordinate it has)
- Player's Current Global X and Y coordinates (two variables) (somewhat redundant)
- Player's Future Map ID (to temporarily store a random number)
- Player's Current Map ID (somewhat redundant)
- A Random Number Generated to represent a Map ID
- The Width and Height of the Future Map (two variables)
- Array 1: An Array of Player's Future Global X and Y Coordinates (for example, [1, 0])
- Array 2: An Array of Player's Current Global X and Y coordinates (somewhat redundant but initially set at [0, 0])
- Array 3: An Array containing a list of all Array 1's that have been created. In other words: An array of all X and Y coordinates that have been visited. (The first one will be [[0, 0]])
- Array 4: An Array containing Array 1 and the current Map ID. An Array of an array of (yes I said that twice) Player's Current Global X and Y coordinates and Current Map ID. (For example, [[0, 0], Map ID]
- Array 5: An Array containing a list of all Array 4's that have been created. In other words: An array of the array all X and Y coordinates matched with the Map IDs that were randomly chosen when the player arrived. (For example, the initial value will look like [[[0, 0], Map ID]] that's an array nested within an array, within another array)
Step 1: The player reaches the edge of the map either on the local X or Y coordinate.
Step 2: The program adds or subtracts 1 from one of the Future Global X and Y coordinates. For example, if Player is at Global (0, 0) and exits on the right then the new coordinates would be (1, 0)
Step 2: The program overwrites Array 1 with Player's Future Global X and Y Coordinates. (for example, [1, 0])
Step 3: The program checks to see if Array 1 appears in Array 3, to see if Player has been to those coordinates before.
- The program checks Array 5 for Array 1 and extracts the Map ID. For example, Player has been to [1, 0] and it loaded map 16, so the program will find [[1, 0], 16] in the array and then get the number 16.
- Player's Future Map ID is updated with the ID extracted.
- Array 2 is set to Array 1
- The player is transferred to the map. (That process has already been discussed in my previous post)
Step 4b: If the Player has not been to those Global Coordinates before:
- A random number is generated and Player's Future Map ID is updated with that number.
- The program adds Array 1 to Array 3
- The program overwrites Array 4 with Array 1 and Player's Future Map ID.
- The program adds Array 4 to Array 5
- The program loads a Map based on the random number.
- Array 2 is set to Array 1.
- The player is transferred to the map.
Okay so that was pretty intense stuff.
The Ruby documentation on Arrays is excellent. I also had help from people in forums, so thank you to them.
The commands I used:
push: To add information to the Arrays I used push,
- for example array3.push(array1) will add array 1 to the end of array 3.
- for example if array3.include?(array1) then ... else ... end (fill in the ... with the commands)
- for example array5.assoc(array1) will extract an entire array, meaning it will pull out something that looks like array4 if it exists within array5
- for example array4.at(1) will extract the Map ID.
Things to keep in mind:
Ruby is case sensitive so if you capitalize any of your words while writing in the script column ruby will think you're referring to a global variable instead of something local.
I've written a version of this post in this discussion thread.