In this topic you will create an API that will handle the loading of an NFT and spawn the correct templates for the NFT loaded based on the attributes you set.
An API (Application Programming Interface) will contain the bulk of the code that would usually be duplicated across files. This allows you to have a set of functions that are shared between your scripts. This makes maintaining the code and applying bug fixes a lot more manageable and quicker.
Create NFTAxeLoader Script
Create a new script called NFTAxeLoader.
In Project Content, click on the NFTAxeLoader script, then find the Blades data table and drop it onto the script as a custom property.
In Project Content, click on the NFTAxeLoader script, then find the Handles data table and drop it onto the script as a custom property.
Edit NFTAxeLoader Script
Open up the NFTAxeLoader script and add the following variables for the custom property references and the API table. The variable NFTAxeLoader is the API that will encapsulate all of the properties and functions that can be used by other scripts when the API is required.
local BLADES = require(script:GetCustomProperty("Blades"))
local HANDLES = require(script:GetCustomProperty("Handles"))
local NFTAxeLoader = {}
Code language:Lua(lua)
Create ColorMeshes Function
Create a function called ColorMeshes. This function will go through all the static meshes of the blade and handle for the Axe, and set the color of each material slot. Since the loader will be using a seed that is pulled from the NFT attributes, then when picking a random color, it will pick the one that was used when generating the Axe. If you need to ignore a static mesh, you can add the Ignore property to it.
functionNFTAxeLoader.ColorMeshes(item, seed)local meshes = item:FindDescendantsByType("StaticMesh")
local ColorRNG = RandomStream.New(seed)
for m, mesh inipairs(meshes) doif mesh:GetCustomProperty("Ignore") == nilornot mesh:GetCustomProperty("Ignore") thenlocal material_slots = mesh:GetMaterialSlots()
for s, slot inipairs(material_slots) do slot:SetColor(Color.New(ColorRNG:GetNumber(), ColorRNG:GetNumber(), ColorRNG:GetNumber()))
endendendendCode language:Lua(lua)
Create Load Function
Create a Load function. This function receives a token ID and contract address that will be used to fetch a specific NFT.
When calling GetToken, the script will yield until the function returns, meaning that the script will pause until the function returns. Notice that the GetToken function returns 3 values. This is so that we can check to make sure the return status of the GetToken function was a success. We can do that by comparing the value stored in status against the enum BlockchainTokenResultCode.SUCCESS.
An important part of this function is reading the attributes of the NFT. The attributes are like the DNA of the NFT, it tells us how it should be made. In this case, it contains 3 attributes that we can check for when we loop over all of them. For each match of the attribute, we spawn either an asset or color the meshes. Notice that the coloring of the meshes is done last in the group to make sure there are meshes to look for.
The script needs to return something. In this case, we return the NFTAxeLoader table so that any script requiring this API will have access to all the functions.
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!