...
It’s nice to control where you start from and keep incrementing the value as we go forward. The COUNT library solves this problem.
Expand |
---|
title | Create a custom component using +COMPONENT |
---|
|
To test out the COUNT library, make a custom component. See Create a Component. |
Expand |
---|
title | Import the COUNT library into your project. |
---|
|
See Import Library if you need a refresher on how to do it. |
Expand |
---|
title | Create a custom number field called 'NextId' and set it to the value 156 in your card |
---|
|
First, in config.json add your Custom Fields. Then go to your component card and set the value to 156. See Custom Fields Note: You’ll need to change your component to use the DEVELOPMENT commit to see the newly added custom field. See Choosing the code to run for your component. |
Expand |
---|
title | local NextId = COUNTget("NextId") is how we fetch the data |
---|
|
This is how we We can use COUNTget to get the data from a the custom field called NextId. We increment NextId by 1 each time the component runs. Code Block |
---|
| local NextId = COUNTget("NextId")
NextId = NextId+1 |
|
Expand |
---|
title | COUNTset("NextId", NextId) is how we set it. |
---|
| So this |
COUNTset saves the new value of the custom field in the custom field value file. Code Block |
---|
| COUNTset("NextId", NextId) |
|
Expand |
---|
title | It This means we can see the NextId in the card view of the component - and we can set it too! |
---|
|
|
Expand |
---|
title | To test this, we can set the component to run every 2 seconds using component.setTimer{delay=} |
---|
|
This is a complete little program to increment a custom number field: Code Block |
---|
require "COUNT.COUNT"
function main(Data)
--Poll the component every 2 seconds
component.setTimer{delay=2000}
local NextId = COUNTget("NextId")
NextId = NextId+1
COUNTset('NextId', NextId)
end |
|
...