In this section, you will be moving the code to generate the map into an API. Because a lot more code will be added later, it makes sense to think about organizing this now while the code is small.
Create MapBuilder Script
Create a new script called MapBuilder and move it to the My APIs folder in Project Content.
Add Custom Properties
The MapBuilder script will need custom properties for the ASCIIParser and the Tiles data table.
Select the MapBuilder script in Project Content so it is the active item selected.
Add the ASCIIParser script as a custom property called ASCIIParser.
Add the Tiles data table as a custom property called Tiles.
Add Script Variables
Open up the MapBuilder script and add variables for the custom properties so you have access to the ASCIIParser and the map Tiles data table.
Add a variable called MapBuilder and store an empty table. This variable will hold all the properties and functions that will be returned at the end so that scripts which require this API can use it.
local ASCIIParser = require(script:GetCustomProperty("ASCIIParser"))
local TILES = require(script:GetCustomProperty("Tiles"))
local MapBuilder = {}
Code language:Lua(lua)
Create Build Function
The build function will be responsible for forwarding on options to other functions in the API, and also responsible for spawning the map based on the table generated from the ASCIIParser script.
Notice that the function for Build is prefixed with MapBuilder. This is so that the Build function is added to the MapBuilder table so it will be exposed to scripts that require the API.
The spawn function will be responsible for spawning the tiles for the map built by the ASCIIParser in the Build function.
Notice the first if statement inside the spawn function is checking the environment the script is being run in. Because this is a script in a static context, you need to make sure the spawn functions are done on the server, or you will get an error.
The MapBuilder API needs the MapBuilder table returned at the end of the script so that scripts which require this API will be given the MapBuilder table.
The GenerateMap script in the Hierarchy will need to be modified to use the new API. It will also need the custom properties changed so it no longer uses the ASCIIParser API, but the MapBuilder API.
Edit Custom Properties
In the Hierarchy select the GenerateMap Script in the Generated Map folder.
Remove the custom property called ASCIIParser.
Add the MapBuilder API as a custom property on the GenerateMap script called MapBuilder.
Edit GenerateMap Script
Open up the GenerateMap script and remove all the code in the script. You will be using the MapBuilder API and passing options to the Build function that will handle spawning in the tiles. From now on, any algorithm changes that need to be done to determine tile spawns and other items will be done from within the API.
Require MapBuilder API
You can require the MapBuilder API by using the require keyword in the script.
local MapBuilder = require(script:GetCustomProperty("MapBuilder"))
Code language:Lua(lua)
Build the Map
Call the Build function from the MapBuilder API to build the map. The Build function requires a table as the input that passes in the information about the map that needs to be built.
Property
Description
width
The width of the map being created. This should match the width of the ASCII string.
height
The height of the map being created. This should match the height of the ASCII string.
size
The size of the tiles.
map
The ASCII string the map will be built from.
container
The static context the assets will be spawned into using SpawnSharedAsset.
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!