The NFT Axe template needs a script that will load a specific NFT. This could load the player’s last selected Axe from storage. In this case, we will set up some custom properties on the script that we can read and send to the API.
Create NFTAxeLoaderServer Script
Create a new script called NFTAxeLoaderServer and place it inside the NFT Axe template that is in the Hierarchy.
Add the NFTAxeLoader API as a custom property called NFTAxeLoader.
Add the AxeGeo group as a custom property called LoadingGeo.
Create a custom property called ContractAddress which is a string type.
Create a custom property called TokenID which is a string type.
Set ContractAddress Property
All lazy minted tokens on Core will have the same contract address. This can be an issue because it means loading a collection is not possible. You would need to store all the token IDs locally in a data table. In this case, we are after loading a specific token for an Axe the player may have equipped.
Set the ContractAddress property to 0x495f947276749ce646f68ac8c248420045cb7b5e.
Set the Token ID Property
The TokenID property is the ID of the NFT you will be loading. You could imagine an inventory of axes that the player has purchased, and they could select one to load instead. For this case, we will keep it simple and load a specific NFT for this Axe.
There are a couple of ways you can get the Token ID for an NFT. The easiest way on OpenSea is either from the URL when you access the NFT or from the details section on the page when looking at the NFT. Once you have the Token ID, update the TokenID property with the ID.
Create Template
The NFT Axe in the Hierarchy is finished. No more changes need to be done, so this can be created as a template. Right click on the NFT Axe, and select New Template From Object.
Edit NFTAxeLoaderServer Script
Open up the NFTAxeLoaderServer script and add the variables for the custom properties that were added so you have a reference to them.
local NFTAxeLoader= require(script:GetCustomProperty("NFTAxeLoader"))
local LOADING_GEO = script:GetCustomProperty("LoadingGeo"):WaitForObject()
local TOKEN_ID = script:GetCustomProperty("TokenID")
local CONTRACT_ADDRESS = script:GetCustomProperty("ContractAddress")
Code language:Lua(lua)
Connect EquippedEvent
When the NFT Axe is equipped by the player, the equippedEvent is fired. We can use this to call a function from the NFTAxeLoader API that will then load the NFT based on the arguments passed in.
local NFTAxeLoader = require(script:GetCustomProperty("NFTAxeLoader"))
local LOADING_GEO = script:GetCustomProperty("LoadingGeo"):WaitForObject()
local TOKEN_ID = script:GetCustomProperty("TokenID")
local CONTRACT_ADDRESS = script:GetCustomProperty("ContractAddress")
script:FindAncestorByType("Equipment").equippedEvent:Connect(function(equipment, player) NFTAxeLoader.load(player, TOKEN_ID, CONTRACT_ADDRESS, LOADING_GEO)
end)
Code language:Lua(lua)
Test the Game
Test the game by playing it and picking up the NFT Axe. After a little while, the parts for the blade and handle will spawn in. While the Axe is loading, the player can see that, but can also still use the Axe. So it won’t slow down the player’s experience.
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!