Core includes the benefit of having an ever-expanding list of visual effect objects to work with, and thanks to the template system, we can combine these in even more unique ways to achieve whatever your vision may be.
We’ll be making a treasure chest that requires an item to open!
Time to complete: 20 minutes
Knowledge Level: No knowledge required, but this tutorial will use some simple Lua scripting. If you’ve never programmed before, you might want to check out the Intro to Scripting first.
Skills you will gain:
To start, open up a new project or whatever project you’d like to make this treasure chest in.
A trigger can operate in two distinct ways. If Interactable is on, it will turn into a player-confirmed switch. This means the player will have to walk up to it and press F to cause the trigger to activate its functions. This works perfectly for things like switches or dialogue.
If Interactable is off, the functions connected to the trigger will happen instantly whenever a player simply walks into a trigger. This case is more useful for things like traps, or in general things you don’t want the player to know about or have to think about activating.
In the Interaction Label property, type Open.
At this stage, if you press play to try it out, you’ll notice that walking up to the treasure chest prompts you to press F to open the chest, but when you press it nothing happens.
Press V to toggle gizmos on and off to see both the spawn point and the trigger we created.
In order to use that trigger in our script, we need to access it.
We need two other custom properties on our script.
When you add a custom property to a script, Core generates a line of code that can be copied and pasted into the script to reference that object. Below the custom property of the trigger, you should see:
local propTrigger = script:GetCustomProperty("Trigger"):WaitForObject()
With all these variables pasted in, your script should look like this:
local propTrigger = script:GetCustomProperty("Trigger"):WaitForObject()
local propChestSmallOpened = script:GetCustomProperty("ChestSmallOpened"):WaitForObject()
local propChestSmallClosed = script:GetCustomProperty("ChestSmallClosed"):WaitForObject()
local propOpeningVFX = script:GetCustomProperty("OpeningVFX")
local propLockedVFX = script:GetCustomProperty("LockedVFX")
In your script, beneath all those variables we just pasted in, add this code:
local function OnSwitchInteraction(theTrigger, player) UI.PrintToScreen("Hi! You've triggered an event.") end
Right now all this does is print directly on our screen the message we type in. This is a useful technique to make sure functions are being called at the time you expect.
Now we have the function, but in order for it to work when we press play, it needs to be connected to the event called interactedEvent of the trigger.
Copy this code into the end of your script to call the function when the trigger is interacted with:
propTrigger.interactedEvent:Connect(OnSwitchInteraction)
Press Play, walk up to the treasure chest and interact with it by pressing F. The message should appear on the screen,Next we will make the chest open only when you have the item that unlocks it.
Community Content is an insanely valuable resource for making your life easier. Other users (and even you too if you want!) post creations that they have made for anyone to use. This helps people who prefer to program still have quality and varied art models to use in their games, and art people who need coding help the types of resources to do that.
In our case, we want an item the player can pick up. Luckily, there is something like that already on Community Content!
Navigate to the Community Content tab. Type loot into the search bar, and import the Fantasy Loot Bag by standardcombo. We’re going to repurpose this!
There are several things we’re going to change about the Loot Bag to suit our needs here with the Treasure Chest.
Now when a player walks over it, they will instantly grab it rather than having to press F first.
We can use whatever art we want in this object, so instead of a coin pouch let’s try… a crowbar. For prying that chest open with!
Check that all the transforms on the Client Context folder are at 0,0,0 to be sure the crowbar is at the right size and location.
The only other important thing we’ll want to change is on the main root folder of the Loot Bag object.One of the custom properties is called ResourceName. By default it is “Gold” and therefore that’s what resource is given to the player when they pick it up. Change the name to reflect the new resource, Crowbar!
Now when walking up the crowbar, the player will automatically pick it up!
The pickup still plays the coin sound effect and that feels pretty weird with this object. So, let’s create a new effect to play!
To start creating an effects template, let’s create a folder.
Now we’ve got it all set! To use it in other places, we’re going to make it into a template.
Right click the root folder, and select Create New Template from This. Now we have a custom sound effect template to use anywhere!
Since all templates in the Hierarchy are only instances of a template saved into Project Content, we can delete this one.
Now we come back to scripting. We need to make sure the player has the crowbar when trying to open the treasure chest in our interaction code we wrote earlier.
Replace all the code of your OnSwitchInteraction function with this:
local function OnSwitchInteraction(theTrigger, player) if player:GetResource("Crowbar") > 0 then player:RemoveResource("Crowbar", 1) propChestSmallClosed.visibility = Visibility.FORCE_OFF propChestSmallOpened.visibility = Visibility.INHERIT else UI.PrintToScreen("No crowbar") end end
This adds an if statement inside our trigger. If the player has a crowbar, then it will take that crowbar away from the player and swap which chest is visible, effectively switching to the open treasure chest form.
If the player has zero or less crowbars (if it were physically possible to have negative crowbars), it prints that out onto the screen.
Now we just want to make sure that our TreasureChest root group has networking enabled. Right click it to enable this.
By adding this new code to spawn our effect and destroy the trigger, your function should look like this:
local function OnSwitchInteraction(theTrigger, player)
if player:GetResource("Crowbar") > 0 then
player:RemoveResource("Crowbar", 1)
propChestSmallClosed.visibility = Visibility.FORCE_OFF
propChestSmallOpened.visibility = Visibility.INHERIT
World.SpawnAsset(propOpeningVFX, (position = theTrigger:GetWorldPosition()))
theTrigger:Destroy()
else
UI.PrintToScreen("No crowbar")
end
end
Notice that we are destroying the trigger at this step so we don’t need to worry about interacting with it twice or seeing the Open text after it has opened
Test it out! Now the opening effect that we made should play when opening the chest. This helps the action to feel more intentional and rewarding as a player.
Now we’ve got a sparkly little opening effect!
Next let’s create the LockedFX for when a player tries to open the chest without having the crowbar first.
We want this LockedFX template to spawn when the player doesn’t have a crowbar, so let’s replace the UI.PrintToScreen() line with lines to spawn the template.
This will make your whole OnSwitchInteraction() function look like this:
local function OnSwitchInteraction(theTrigger, player) if player:GetResource("Crowbar") > 0 then player:RemoveResource("Crowbar", 1) propChestSmallClosed.visibility = Visibility.FORCE_OFF propChestSmallOpened.visibility = Visibility.INHERIT World.SpawnAsset(propOpeningVFX, {position = theTrigger:GetWorldPosition()}) theTrigger:Destroy() else World.SpawnAsset(propLockedVFX, {position = theTrigger:GetWorldPosition()}) end end
This makes it easier to tell that the player is indeed interacting with the chest–just not in the right way.
Now you’ve got some basic effects set up for your treasure chest! You have learned how to create effect templates by combining various visual effects with sound effects, and how to spawn them at specific times! Go forth, and make more awesome stuff.
In most cases you won’t need to code to use the visual effects in Core. Oftentimes templates will have a custom property for more effect templates, so you can make your own effects as templates and just drag and drop them into other content–just like the Fantasy Loot Bag we used.
There are so many ways that you can make effects unique. By combining several of them, changing their properties dramatically, and using them connected through code you can change the timing and positions of effects in whatever ways you can dream up.