I've been pretty excited about getting my room copy and spawn system working in Game Maker Studio 2 and I figured it would be a great opportunity to explain some of the logistics to those who may be interested.
First, there was a list...
The above code creates a ds_list, then adds 36 variables to it, including 1,2,3,4, and a bunch of zeroes. After the list is complete, the list is shuffled.
This list will be used to create a 6x6 grid which will be the structure of the final room.
The numbers 1, 2, 3, & 4 are going to be used to identify the starting point, the item room, the shop, and the boss room. The 0s are all generic rooms.
The ds_list_add function in Game Maker only allows for a certain amount of items to be added at a time. I think it's 18? That's why the command is run multiple times instead of once with a for loop. I could have probably done a for loop for each run, but it's honestly easier to just type a bunch of zeroes.
After the list is finished and shuffled, their positions might look something like this when placed on a grid with each list position continuing left to right and top to bottom.
Now that the list is created, we're going to put some modifiers in to make it feel more organic.
The method that I decided to use involved creating a "safe grid" by choosing specific points in the list and giving those points an option to change to a "5" if they are currently a "0". 5s will be sub rooms that can be fully obscured, or contain basically anything without obscuring the main objectives due to their positioning. The points are chosen based on their locations to guarantee that no matter how many of those 0s are changed to 5s, every other space will be accessible.
Below is the script run to check the darkened squares in the example above.
The loop basically checks each pre-determined position of the list to see if it's a 0 or not, and if it is there's a 70% chance to change it to a 5. This will end up creating something like this:
There it is; The final room layout. The only addition that I made to what's above was by creating more safe grids and plugging their list positions into the insert_filler_rooms() script then assigning them to the pat == 2 and pat == 3 options.
Now that I have the ds_list that I'm going to use to spawn the room, I can use my 2 spawner objects to actually create the main room.
This is a bit more involved but I'll explain the basic function of it using the above grid.
- o_room_spawner persistent object is created in the current room
- o_room_spawner moves to room 1 on the ds_list that was created upon spawn
Room 1 chooses from X potential rooms in that group
This room will be in group 5
- Once in the chosen group 5 room, create o_room_copier persistent object
- Copy all instance_id, x_scale, y_scale, rotation, x_position, y_position etc. in the room minus the spawners and save them to a ds_grid using the o_room_copier object
- After everything is copied, move to the main room
- Once in the main room, paste the objects in their relative positions at a specific point
I'll just call it grid_space[0,0]
- Destroy the o_room_copier object as well as the ds_grid within it and change the amount of rooms copied to +1 in o_room_spawner
- o_room_spawner then moves to room 2 on the list which is a 0 room
This loop continues, copying to each grid space next in line until the main room is fully populated.
The player and camera are then created, lots of instances outside of the view are shut off, and the player can start exploring.
I'm looking forward to getting some artwork put in the game to make these maps feel more alive. Lots can be done with camera movement and hiding certain things until a specific time.
Next up is to make a better mini-map!
Lots of work to be done on code name "Delivery Man."
Until next time.