In our previous example, we broke down all the different parts of a PolyPop object to understand the various parts involved to define our functionality. In this example, we’ll gloss over some of the specifics of the previous example, and focus on the new stuff.

In this example, we’ll see how to create an alert that fires periodically with the current system time. We’ll be able to connect this to a Text object in our scene to see the current system time update.

File Locations

  1. Inside our “SDK Examples” folder, create a folder called “Example 2”. You should now have “C:/Program Files/PolyPop/UIX/SDK Examples/Example 2”

  2. Finally, create 2 blank text files within this folder:

    1. Example2.uix

    2. Example2.lua

UIX file

Open the “Example2.uix” file you created in your code editor and paste the following code into the file.

Example2.uix

<uix type="Source">
  <category>Alert Objects</category>
  <source_type>Alert Generator</source_type>
  <name>[SDK Example] Example 2</name>
</uix>
CODE

Lua file

Paste the following code into Example2.lua:

Example2.lua


------------------------------------------------------------------------------------
--  User changable properties
------------------------------------------------------------------------------------
Instance.properties = properties({
	-- Property Group
	{ name="Alerts", type="PropertyGroup", items={
		-- Alert
		{ name="onExampleAlert", type="Alert", args={value="Text"}}
	}}
})

------------------------------------------------------------------------------------
-- onInit(): Called once when object is first loaded
------------------------------------------------------------------------------------
function Instance:onInit()

end
LUA

Restart Polypop to ensure it discovers the new component.

Adding The Functionality

Now that we have our structure ready, we can add our system clock logic. To do this, we will be creating a timer that fires at an interval that we’d like to keep our time updated. For this example, we’ll update every second.

Let’s add the code to do this. On the onInit() function, write the following:

function Instance:onInit()

	-- Create a timer that fires repeatedly every second
	getAnimator():createTimer(self, self.onTimerUpdate, seconds(1), true)

end
CODE

Next underneath the onInit() function, let’s add a new function called onTimerUpdate() that gets called every second:

function Instance:onTimerUpdate()
	local time = os.date()
	self.properties.Alerts.onExampleAlert:raise({system_time=time})	
end
CODE

This function gets the system date and time as a string using the os.date() function and passes it through to the Example Alert. You can now connect the alert to an object with a Text property to see the value it produces. Since out timer fires every second, it updates the value every second.