UI_Objective_List --Created by Ashton Sanders --Date 11.22.24 --GMS Version: 3123.6 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------LIBRARY FOR USAGE--------------------------------------------------------------------------- --objectives.TurnOnNumber(identifyingIDNumber, currentNumberOfObjectForObjective, maxNumberOfObjectsForObjective) --use to set amount collected for an objective (e.g. On collecting +1 out of 3 rocks, I can use "TurnOnNumber" to add 1/3 to the objective text) --objectives.TurnOnCheck(identifyingIDNumber, boolForTriggedByGameplay) --Use to activate checkmark on objective -- ID number = objective (1-6) to which checkmark should apply -- if calling this function through lua, bool = true --objectives.CompletedObjectiveRemoveItem(identifyingIDNumber) --Use to remove an objective from the list completely -- ID number = which objective (1-6) item to remove from screen --Use public or private variables to fill out text and image paths for list. Otherwise, this script should not be modified. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- local public text1 = "Go Caroling" local public path1 = "" local public text2 = "Turn on Tree Lights" local public path2 = "" local public text3 = "Deliver Hot Chocolate" local public path3 = "" local public text4 = "Build A Snowman" local public path4 = "" local public text5 = "Donate" local public path5 = "" local public text6 = "Explore the Ice Cave" local public path6 = "" local textTable = {text1, text2, text3, text4, text5, text6} local pathTable = {path1, path2, path3, path4, path5, path6} local bodyTextTable = {} local theCheckObjList = {} local questIDNumber = {} local questObjectiveImgPath local questObjectiveText local curPanel local panelChild local uiChildNumber = 0 local textObj local theText local theTextObjList = {} local theObjectiveList = {} local itemIDFromSaveScript --Creates and disables all objective list UI features at start of scene function Start() loadAsync("Missions/Objectives", OnLoaded) luaEvent:AddEventListener("AllObjectsAcquired", this, QuestTurnIn) luaEvent:AddEventListener("ObjectiveAlreadyCompleted", this, ConvertDataForCheck) end --Loads UI location on screen and calls SetUI method function OnLoaded(prefab) if IsExist(prefab) then local go = newObject(prefab) go.name = prefab.name local tf = go.transform curPanel = tf.gameObject local rectTf = go:GetComponent(typeof(UnityEngine.RectTransform)) local preRectTf = prefab:GetComponent(typeof(UnityEngine.RectTransform)) tf:SetParent(transform:UIroot()) tf.localScale = Vector3.one tf.localEulerAngles = Vector3.one tf.rotation = Quaternion(0,0,0,0) rectTf.sizeDelta = preRectTf.sizeDelta rectTf.anchorMin = preRectTf.anchorMin rectTf.anchorMax = preRectTf.anchorMax rectTf.anchoredPosition = preRectTf.anchoredPosition rectTf.anchoredPosition3D = preRectTf.anchoredPosition3D SetUI(go.transform) end end --Sets all objects in Objective UI to false on scene start function SetUI(uiTF) panelChild = uiTF:GetChild(uiChildNumber).gameObject for i = 0, 4, 1 do textObj = panelChild.transform:GetChild(i) -- child key id textObj.gameObject:SetActive(false) end for i = 1, 3, 1 do textObj = GameObject.Instantiate(textObj.gameObject) textObj.transform:SetParent(panelChild.transform) textObj.transform.localScale = Vector3.one textObj.gameObject:SetActive(false) end MakeTextValues() luaEvent:DispatchEvent("AllQuestObjectivesCreated") end --Creates each objective based on text values, image paths, and location on the list function MakeTextValues() for i = 1, 6, 1 do CreateObjectiveListItem(textTable[i], pathTable[i], i) luaEvent:DispatchEvent("QuestCreated", i) SetVariables(i) end end --Stores the current text in a variable so that the item can be incremented without affecting to the initial text function SetVariables(infoIndex) local newIndex = infoIndex if theTextObjList[newIndex] ~= nil then table.insert(bodyTextTable, theTextObjList[newIndex].text) end end --Creates objective icon variable and objective text variable function CreateObjectiveListItem(itemText, itemPath, questIDOneToFive) questObjectiveText = itemText questObjectiveImgPath = itemPath table.insert(questIDNumber, questIDOneToFive) SetObjectiveText() --Sends data from this func to update objective text and icon appearance end --Stores text, image, and checkmark image into tables so that the quests can be picked up in any order and completed in any order function SetObjectiveText() StoreTextObjectAndTextDetails() SetTheObjectiveImages() uiChildNumber = uiChildNumber + 1 --increment the uiChildNumber for the textObj so that the next quest appears in the next objective ui object (not overwrite existing objectives) textObj.gameObject:SetActive(true) --Enable the newly created quest objective (text, img, checkbox) end --Stores text details and object details in tables so that each can be updated and (de)activated function StoreTextObjectAndTextDetails() textObj = panelChild.transform:GetChild(uiChildNumber) -- child key id table.insert(theObjectiveList, textObj) --Stores full objective ui object in table to be toggled off on quest completion local textEle = textObj:GetChild(1) theText = textEle.gameObject:GetComponent(typeof(CS.TMPro.TextMeshProUGUI)) table.insert(theTextObjList, theText) --Stores objective Text object in a table so that the text can be updated when quest objects are picked up CheckTextItemActive() end --Sets the icon images for each objective based on input values - if no image, use default function SetTheObjectiveImages() local check = textObj:GetChild(0) --Find checkbox object in objective UI and store as variable local checkBoxBackgroundImg = check.gameObject:GetComponent('Image') local checkBoxImg = check.gameObject:GetComponent('Sprite') -- Set the checkbox object to the "checkmark" image if questObjectiveImgPath ~= "" and questObjectiveImgPath ~= "nil" then loadAsyncSprite(questObjectiveImgPath, function(image) checkBoxBackgroundImg.sprite = image end) else checkBoxImg = image end local theCheckObjCounter = check:GetChild(0) table.insert(theCheckObjList, theCheckObjCounter) --Store the checkbox object in a table to be toggled on/off as quests are completed DeactivateInactiveChecks() end --For each object in the text object table, if object is inactive, set the text to the input text from CreateQuest() method --If text is already active on an object, do nothing function CheckTextItemActive() for i, theTextObjItem in ipairs(theTextObjList) do if theTextObjItem.gameObject.activeInHierarchy ~= true then theTextObjItem.text = questObjectiveText break end end end --Remove checkbox image if object should not be checked function DeactivateInactiveChecks() for i, theCheckObjectItem in ipairs(theCheckObjList) do --Turn all inactive checkbox objects to false if theCheckObjectItem.gameObject.activeInHierarchy ~= true then theCheckObjectItem.gameObject:SetActive(false) end end end --Updates the item text when collecting quest items to reflect current item count function TurnOnNumber(questNumID, numOfCurrObj, numOfMaxObj) for i = 1, 6, 1 do if questNumID == questIDNumber[i] then if numOfCurrObj == nil and numOfMaxObj == nil then questObjectiveTextCounter = bodyTextTable[i] else questObjectiveTextCounter = bodyTextTable[i].." ("..numOfCurrObj.."/"..math.floor(numOfMaxObj)..")" end theTextObjList[i].text = questObjectiveTextCounter if numOfCurrObj == math.floor(numOfMaxObj) then TurnOnCheck(i, true) end break end end end --When objective is complete, turn on the checkmark image next to the corresponding objective function TurnOnCheck(intCheckNum, boolCheckDuringGameplay) itemID = intCheckNum for i = 1, 6, 1 do if itemID == i and theCheckObjList[i].gameObject.activeInHierarchy ~= true then theCheckObjList[i].gameObject:SetActive(true) if boolCheckDuringGameplay then luaEvent:DispatchEvent("ObjectiveComplete", itemID) end break end end end --Recieves dispatch from completion status script, converts data, and sends to checkmark method function ConvertDataForCheck(infoItemID) --print("convert to check called with info ", infoItemID.Data) if infoItemID:CheckData() then itemIDFromSaveScript = infoItemID.Data end TurnOnCheck(itemIDFromSaveScript, false) end --Allows user to use text on UI function GetTextMeshProUGUI(tf,path) local ctf = tf:Find(path) if ctf ~= nil then return ctf:GetComponent(typeof(CS.TMPro.TextMeshProUGUI)) end return nil end --When quest is turned in, disable the corresponding objective ui object function CompletedObjectiveRemoveItem(infoWhichObjective) local objectiveID = infoWhichObjective if theCheckObjList[objectiveID].gameObject.activeInHierarchy then theObjectiveList[objectiveID].gameObject:SetActive(false) else print("the objective has not been completed") end end --Remove script functionality on disable function OnDisable() GameObject.Destroy(curPanel) luaEvent:RemoveAllEventListener(this) unBindScriptByname(gameObject, "UI_Objective_List") end function OnDestroy() print("objectives object was destroyed") end