LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I make an update function?

I have no idea how do I go about implementing it. I have two functions, namely the Initialize and Update Function. I need to execute the Initialize function only once while the Update function would be executed at every tick of the program itself.

I tried to make it so that when I press the button to start, the Initialize function would play and then Timers would be set before a new panel would open and the Update function would now run every tick. Thing is, the for loop inside the Update function does not increment at all. All it did was stay stuck at the start, which is at zero.

How do I fix this and how do I go about implementing this?

 

0 Kudos
Message 1 of 8
(1,138 Views)

I'd investigate the example programs that are installed with the CVI application.  Surely there is an example project for using the GUI Timer control that could help you understand how to make your application function as you intend.

0 Kudos
Message 2 of 8
(1,121 Views)

Alright, where can I find the example projects? Also, why isn't the for loop running in the timer function?

 

0 Kudos
Message 3 of 8
(1,107 Views)

Examples installed with CVI can be located by means of the Example Finder: execute Help >> Find Examples... menu function and the tool will open where you can browse samples of search by keyword and so. Double-clicking on an example listed will open CVI IDE with the sample project loaded.

 

Having said this, you should really spend some time looking at the Getting Started document, that explains the basic paradigm of CVI. Specifically, a timer callback is a function that is fired at fixed intervals by the system, and is recommended that its code is kept the shortest possible to permit the correct scheduling of timer, system and user events in the system: putting a loop inside a timer callback is the worst you can do! Based on what you said, your Update function needs probably to be rewritten.

 

Coming to your problem, a clean way to organize your code could be:

  • Keep the timer disabled
  • On a user Start (usually inside a button callback) execute the initialize function and then enable the timer
  • Inside the timer callback execute the Update function: be sure to keep this callback short avoiding unnecessary tasks such as data display or analysis that can be executed in the main thread
  • When the user wants to stop the process or at program end disable the timer and then clean up all allocated resources


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 4 of 8
(1,093 Views)

I'm going to be honest, I need to execute my function in every tick. Which meant that I need some way to do so, whether through some timers or through some other means. And since using timers would be out of the question since they can only execute short form functions, do you have any other ideas on how I might execute this update function that can be run once the window is open, even if said panel is out of focus? To run continuously until the program exits.

 

 

0 Kudos
Message 5 of 8
(1,069 Views)

I haven't said that you cannot execute the function inside a timer callback, but you must be aware of the scenario you are moving on.

How long does the function take to complete? And how often do you want to execute it?

If the function takes a lof of time, can it be simplified someway e.g. removing tasks like some data analysis or output on screen / on file that can be executed elsewere? There ware ways to do so, you know.

 

We cannot argue on the code since we do not know nothing, but since you mentioned a loop I was under the impression that the function is tailored to be executed interactively under operator's control and can be rewritten to be executed continuously in a timer without operator's notice.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 6 of 8
(1,049 Views)

Pretty much, the idea that I had was for a game that I am currently programming on. I am trying to run a update function to make sure that the environment and gameplay is constantly updated, as well as player movement, variables and key presses.

At the moment, I have my update function within the EVENT_TIMER_TICK, which should run the entire thing per each interval that I set in the timer, even when the window is unfocused. The function should be quick, though it could be longer once I program stuff like enemies and projectiles and what not. And I do want the function to operate per tick, which meant as quickly as possible.

Regarding the for loop problem, it's actually a blunder on my end, since I didn't realize that my variable is of a single value, so now the for loop is working really well. Though timing is going to be an issue in and of itself.

Last miscellaneous question: How do I make it so that the canvas does not flicker or glitch whenever I draw a bunch of stuff and refreshed the canvas to reflect the changes? It's kinda annoying to see the entire game flicker really badly on my end, and I have to wonder if there's a better way of refreshing/displaying stuff in the canvas?

Thanks again for the answers, I really do appreciate them.

 

0 Kudos
Message 7 of 8
(1,037 Views)

A possible fix for the flickering is to hide the canvas control, make your drawing updates, then unhide the control.  Use the following function to do that:

 

SetCtrlAttribute(yourPanel, yourControl, ATTR_VISIBLE,FALSE);

... make drawing updates ...

SetCtrlAttribute(yourPanel, yourControl, ATTR_VISIBLE,TRUE);

 

0 Kudos
Message 8 of 8
(986 Views)