Description

Learn how to add your own methods to your components and templates.

Video recorded using: Ignition 7.7

Transcript

(open in window)

[00:00] In this lesson, we'll learn about Custom methods for Components. Custom methods lets you add methods to Components, much like Custom properties lets you add properties to components. The way you access them is through the scripting pop-up here, and you can just go ahead and double click on Add Function to add a new function. So, we're going to add a function to this label called foo, and it'll take a parameter called arg. And it's just going to print the argument. Just so we can see this thing working. And then let's go ahead and link it up to the mousePressed so we can just say event. source .foo Hello, world. So you can see that the method foo now just appears like any other natural python method of our label object. Okay, we click on this thing, and it prints out "Hello, world," to the console. There's a number of reasons why this is pretty handy. One reason is that, let's say, for example, you're trying to pop-up a context menu, a pop-up menu, on the right-click of a component. Well, it turns out that the right-click action is supposed to happen on either mouse-up or mouse-down differently, depending on what operating system you're running on. And so, if you've read the documentation for system.GUI.createpopupmenu, you'll see that it recommends putting your script in both mousePressed, as well as mouseReleased, just so that you can catch this popupTrigger bullion in both cases. But that's really annoying because you don't want to have to copy and paste code across these two methods, and you don't want to 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 where it would be great to just be able to add a method called showPopup, and then you just call showPopup from both mousePressed and mouseReleased, and you don't have any duplication of your code. Templates are another area where custom methods really shine. So, imagine you're creating some sort of big, complicated template, and the interior of your template needed to call a function. Well, you could put that function inside of your, maybe your project script library, but the problem with that idea is that then you have the logic of your template spread across multiple areas, so you've got the template itself, the template master, here, but the scripting of it all lives somewhere else and then if you try to export this template and share it with somebody, they're not going to have the scripting unless you separately export the scripting. So, by adding the Custom method to directly under the template, all of the interior components inside of that template can just call that method directly on the template itself, and you get to really encapsulate all of the logic, both bindings and scripting all inside of the template master.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.

close

Share this video