Easy Logic#2
Sometimes, adding decision-making moments to your game can make it more engaging for players. It gives them time to think and feel more involved in the gameplay, which can enhance their overall experience. In Yahaha, this can be done easily. This is the second tutorial in the series, where I show how to create custom logic with minimal coding by using Yahaha’s built-in components to support your game logic. (˶˃ ᵕ ˂˶)
The Video
I’ve made a video tutorial that show examples of how this system work and help you walk through it step by step. Here, I’ll also provide some extra explanations, along with the script below. ദ്ദി(˵ •̀ ᴗ - ˵ ) ✧
*How this work
This system lets you create a multiple-choice UI where each option can trigger a different event. It uses two lists: one for the option texts (which you can set in the Inspector) and one for the EndTrigger GameObjects. The number of buttons shown matches the number of option texts you provide—so if you set two options, it shows two buttons; if you set three, it shows three. Each button is linked to an EndTrigger based on its index in the list. When a button is clicked, the matching EndTrigger is activated, and any events set up in its Event Trigger component will happen.
To use this system, just activate the MultipleOptionHandler GameObject when you want the choices to appear—for example, after interacting with an item or when an NPC finishes talking.
Code
local fieldDefs = {
{
name = "UIField",
label = "UI Field",
hint = "UI Field",
description = "UI Field",
type = "UIPackageFile"
},
{
name = "OptionList",
type = {
type = "list",
items = {
name = "Option",
type = "string",
}
}
},
{
name = "EndTriggerList",
type = {
type = "list",
items = {
name = "EndTrigger",
type = "GameObject",
}
}
}
}
script.DefineFields(fieldDefs)
local UIField = script.fields.UIField
local OptionList = script.fields.OptionList
local EndTriggerList = script.fields.EndTriggerList
local Self = script.gameObject
--Buttons
local option1
local option2
local option3
local YaResourceManager = YahahaMiddleLayerSlim.Resource.YaResourceManager
local mainPanel
local packageName
script.OnStart(function ()
LoadResource()
end)
function LoadResource()
YaResourceManager.LoadResourceByUIPackageField(UIField, function(state, name)
if state == AssetStatus.AllAssetCompleted then
packageName = name
-- Create the main panel
CreateMainPanel()
end
end)
end
function CreateMainPanel()
mainPanel = UIPackage.CreateObject(packageName, "OptionBox")
GRoot.inst:SetContentScaleFactor(2436, 1125)
GRoot.inst:AddChild(mainPanel)
mainPanel.size = GRoot.inst.size
mainPanel:AddRelation(GRoot.inst, RelationType.Size)
option1 = mainPanel:GetChild("Choice1")
option2 = mainPanel:GetChild("Choice2")
option3 = mainPanel:GetChild("Choice3")
SetUpButton()
end
script.OnUpdate(function (dt)
UnityEngine.Cursor.visible = true
UnityEngine.Cursor.lockState = UnityEngine.CursorLockMode.None
end)
function SetUpButton()
option1.text = OptionList[1]
option2.text = OptionList[2]
option1.onClick:AddListener0(Button1OnClickEvent)
option2.onClick:AddListener0(Button2OnClickEvent)
if #OptionList > 2 then
option3.text = OptionList[3]
option3.onClick:AddListener0(Button3OnClickEvent)
else
option3.visible = false
end
end
function Button1OnClickEvent()
EndTriggerList[1]:SetActive(true)
OnDispose()
end
function Button2OnClickEvent()
EndTriggerList[2]:SetActive(true)
OnDispose()
end
function Button3OnClickEvent()
EndTriggerList[3]:SetActive(true)
OnDispose()
end
function OnDispose()
mainPanel:Dispose()
Self:SetActive(false)
YaResourceManager.RemoveResourceByUIPackageField(UIField)
end