You can help by commenting or suggesting your edit directly into the transcript. We'll review any changes before posting them. All comments are completely anonymous. For any comments that need a reply, consider emailing training@inductiveautomation.com.
Version:
LESSON LIST
LESSON
Component Custom Methods
Description
Learn how to add your own methods to your components and templates.
Video recorded using: Ignition 8.1
Transcript
(open in window)[00:00] In this lesson, we'll learn about custom methods for components. Custom methods are just methods added directly onto components, much like custom properties can also be added to components. To show how to access custom properties, let's simply drag a new label onto our designer window, and then we'll right click on it and go to scripting. And this part is important, we want to double click on the add method and here we wind up in this component scripting window. So we have the beginnings of a new method pre-populated for us. So here, let's turn this into our own custom method. We'll give it our own name, "foo" and one input parameter called "text". And notice how these both got auto-added to the starter script here, so all we're gonna do is something like... print text, we'll just print the input parameter text; just something simple to see that it's working properly. Next, let's link this function to an event.
[01:04] So we will go to the mouse events folder and click on mouse clicked, and that takes us to the script editor. And there we can do event.source, which is the component itself, and then if we add .foo and in parentheses and double quotes some text, like the standard, "Hello World" for example; just any text to be printed. So here, the method "foo" now just appears like any other natural Python method of our label object. To try all this out, let's apply our changes, toggle over to the preview mode at the top, and then when we click on the label a few times, we see the specified text written out to the console just as we specified in our script editor. There are a couple reasons why custom methods are pretty handy; one reason is to have a common location for any repeated code.
[02:05] For example, suppose you're trying to pop up a context menu, a pop-up menu arising from the right click of a component. It turns out that the right click action is supposed to happen on either a mouse up or mouse down differently, depending on what operating system you're running on. And so, as you'll notice if you read the documentation for system.gui.createpopupmenu, you'll see that it recommends putting any script actions in both the mouse pressed and the mouse released events. Just so you can catch this pop-up trigger boolean, this one right here, in both cases. However, that's really annoying, because you don't wanna have to copy and paste code across both of these methods and you don't wanna have to put that sort of code in a global script library, because it's so local to the component you're working on. So that's a great example of how it would be great to just be able to add a method called "showpopup" like so, and then you just call "showpopup" from both the mouse pressed and the mouse released events; so now there's no duplication of any code needed, it's all in this method right here.
[03:17] Templates are another area where custom methods really shine. So imagine you're creating some sort of big, complicated template, like so, and the interior of your template needed to call a function. Well, you could put that function inside of maybe your project scripting library, but the problem with that idea is then you have the logic of your templates spread across multiple areas; you've got the template over here and it's logic someplace else. Then if you try to share it with somebody, they're not going to have the scripting unless you separately export the scripting. So by adding any custom methods to directly under the template, all of the interior components of that template can just call those methods directly from the template itself.
[04:03] And you get to really encapsulate all of the logic, both bindings and scripting, all inside of the template master. So to summarize this lesson, we've seen what custom methods are and how they allow you to localize any scripting code, such as perhaps for common event actions or for templates.