You will create an API that will contain all the functions for the editor. This script will handle dynamically creating the grid and tile controls.
Create a new script called ASCIIEditor and move it to the Editor folder under My Scripts in the Project Content window.
Add the Tiles data table as a custom property called Tiles.
Add the Control Tile template as a custom property called ControlTile.
Add the Map Tile template as a custom property called MapTile
Edit ASCIIEditor Script
Open up the ASCIIEditor script and add the following variables so you have references to the properties. The ASCIIEditor table will contain some properties that will be set throughout the API by various functions.
local TILES = require(script:GetCustomProperty("Tiles"))
local CONTROL_TILE = script:GetCustomProperty("ControlTile")
local MAP_TILE = script:GetCustomProperty("MapTile")
local ASCIIEditor = {
activeControl = nil,
startingColor = nil,
startingId = nil,
opts = {}
}
Code language:Lua(lua)
Create Build Function
Create a function called Build. This function will enable the cursor, the opts table is set to the parameter opts that is sent into the Build function. At the same time, the clickedEvent is connected for all the various UI buttons.
Create a function called OnTileHover. This function will change the button color when the mouse is hovered over a grid tile. At the same time it will check to see if the action Fast Color is being held down, if it is, it will automatically set the background and custom property of the tile that was hovered over. This makes it easier to color tiles.
functionASCIIEditor.OnTileHover(button) button:SetButtonColor(Color.WHITE)
if Input.IsActionHeld(Game.GetLocalPlayer(), "Fast Color") then button:FindChildByName("Background"):SetColor(ASCIIEditor.activeControl:FindChildByName("Background"):GetColor())
button:SetCustomProperty("id", ASCIIEditor.activeControl:GetCustomProperty("id"))
endendCode language:Lua(lua)
Create OnTileUnhovered Function
Create a function called OnTileUnhovered. This function will revert the button color to its original color when the mouse is unhovered.
Create a function called OnTileClicked. This function will change the background color of the button and set the custom property when it has been clicked on.
Create a function called CreateGrid. This function will create the grid with all the tiles. Each tile will be set to the startingColor value, which will normally be the floor tile color. At the same time, the dynamic property id is set to the startingId.
The function also handles setting up the buttons events for when the mouse hovers and clicks on a tile.
To keep the grid centered in the UI, the function sets the width and height based on how many tiles there are. Each tile is 50 x 50, but too many tiles could cause the container to overflow, so a bit of checking is done to make sure the map is no more than 900 in height, if it is, then the scroll panel will be used.
Create a function called OnControlTileClicked. This function will detect when a tile has been clicked on. If the current tile is the same as the last activeControl tile, then no change is needed. Otherwise, the color is changed, and the activeControl is reverted to the default color. The current button is then set as the activeControl.
Create a function called CreateControls. This function will dynamically create the controls in the UI by looping over the Tiles data table to find which tiles are valid. Valid tiles from the data table are tiles that are not below 0. The color of each control is also set by using the color from the row in the data table. Each tile has a dynamic property called id that is set to the id from the row in the data table.
The function will also look for the Floor tile and make this the activeControl by default.
Create a function called BuildASCIIMap that will loop through all of the children of the scroll panel and build up the ASCII string that will get printed to the Event Log. You can then take this string and put it into the GenerateMap script to play the new map.
functionASCIIEditor.BuildASCIIMap()local children = ASCIIEditor.opts.MAP_SCROLL_PANEL:GetChildren()
local mapStr = ""for index, child inipairs(children) do mapStr = mapStr .. child:GetCustomProperty("id")
if(index % ASCIIEditor.opts.WIDTH == 0) then mapStr = mapStr .. "\n"endendprint(mapStr)
endCode language:Lua(lua)
Create ClearGrid Function
Create a new function called ClearGrid. This function will destroy all the grid tiles by looping through all the children in the scroll panel.
functionASCIIEditor.ClearGrid()local children = ASCIIEditor.opts.MAP_SCROLL_PANEL:GetChildren()
for index, child inipairs(children) do child:Destroy()
endendCode language:Lua(lua)
Create UpdateGridWidthHeightText Function
Create a new function called UpdateGridWidthHeightText. This function will update the width and height of the grid so you can see what it is currently set to.
Create a new function called ResizeGrid that will either increase or decrease the grid width. This function is destructive, meaning that any current tiles set in the grid will be cleared.
functionASCIIEditor.ResizeGrid(button, w, h) ASCIIEditor.opts.WIDTH = ASCIIEditor.opts.WIDTH + w
ASCIIEditor.opts.HEIGHT = ASCIIEditor.opts.HEIGHT + h
ASCIIEditor.UpdateGridWidthHeightText()
ASCIIEditor.ClearGrid()
ASCIIEditor.CreateGrid()
endCode language:Lua(lua)
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!