In this section, you will modify the generation algorithm to use the Enemies table and spawn a random enemy from that table. By using the data from the ASCII map to determine where enemies spawn, you can randomly pick an enemy from the Enemies table.
Add Enemies Custom Property
The MapBuilder API script will need a reference to the Enemies data table.
In My APIs in Project Content, click on the MapBuilder script.
In My Tables in Project Content, add the Enemies data table as a custom property to the MapBuilder script called Enemies.
Update MapBuilder API Script
The MapBuilder script will need to be updated to handle enemy spawning.
Add Enemies Data Table Reference
Add a reference to the Enemies data table custom property you added so the script can pick an enemy to spawn.
local ENEMIES = require(script:GetCustomProperty("Enemies"))
Code language:Lua(lua)
Create SpawnEnemies Function
Create a new function called SpawnEnemies. This function will receive a table as a parameter that will contain all the spawn points for enemies. It will loop over those spawn points and randomly select an enemy from the Enemies data table. The position of the enemy will be the position of the tile, and a small UP value is added to make sure the enemy doesn’t spawn in the tile below them.
When using the SpawnAsset function, an optional parameter can be added containing a table of options. One of those options allows you to specify what context the asset is spawned in. Because the MapBuilder script is in a static context, you will need to specify the context for the enemy. The enemy templates are networked, so setting the option networkContext to NETWORKEDwill prevent any errors and spawn the enemy in the correct context.
functionMapBuilder.SpawnEnemies(spawnPoints)for index, point inipairs(spawnPoints) dolocal enemyAsset = ENEMIES[math.random(#ENEMIES)].asset
local enemy = World.SpawnAsset(enemyAsset, {
position = point + (Vector3.UP * 50),
networkContext = NetworkContextType.NETWORKED,
rotation = Rotation.New(0, 0, math.random(0, 360))
})
endendCode language:Lua(lua)
Update Spawn Function
The Spawn function will need updating so that the enemy spawn points are collected, and also a few extra checks need to be done to make sure a floor tile is spawned under the enemy.
A table called enemySpawnPoints is added above the loops. Each time the loop finds a map value that is E, it will insert the position of that tile into the table. A check is done to see if the extraTile variable is nil, if so it will be set to a floor tile so that the enemy has something to stand on. After the loop is completed, a call to SpawnEnemies is done that will handle the spawning.
A check is also added when the tile is used for spawning the player. If the extraTile is nil then a floor tile is spawned.
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!