<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to initializing VAR_PERSISTENT variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/8e024c5679/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/8e024c5679/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/8e024c5679/</id><updated>2020-09-09T14:55:50.949000Z</updated><subtitle>Recent posts to initializing VAR_PERSISTENT variables</subtitle><entry><title>initializing VAR_PERSISTENT variables</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/8e024c5679/?limit=25#b7f6" rel="alternate"/><published>2020-09-09T14:55:50.949000Z</published><updated>2020-09-09T14:55:50.949000Z</updated><author><name>tvm</name><uri>https://forge.codesys.com/u/tvm/</uri></author><id>https://forge.codesys.coma72a854f03c908d08d6b2a00b271bc11f2dca2c7</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I have a question about the best way to initialize persistent variables contained in a function block.  I have the following function block:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;FUNCTION_BLOCK&lt;/span&gt; &lt;span class="nt"&gt;TEST_FB&lt;/span&gt;
&lt;span class="nt"&gt;VAR&lt;/span&gt; &lt;span class="nt"&gt;PERSISTENT&lt;/span&gt;
    &lt;span class="nt"&gt;Setpt&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="nt"&gt;SETPTSTRUCT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;  &lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="nt"&gt;where&lt;/span&gt; &lt;span class="nt"&gt;SETPTSTRUCT&lt;/span&gt; &lt;span class="nt"&gt;is&lt;/span&gt; &lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="nt"&gt;structure&lt;/span&gt; &lt;span class="nt"&gt;containing&lt;/span&gt; &lt;span class="nt"&gt;all&lt;/span&gt; &lt;span class="nt"&gt;my&lt;/span&gt; &lt;span class="nt"&gt;persistent&lt;/span&gt; &lt;span class="nt"&gt;variables&lt;/span&gt;
&lt;span class="nt"&gt;END_VAR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I then go to PersistentVars, and "Declarations/Add all instance paths", and I'll get something like this:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;Generated&lt;/span&gt; &lt;span class="nt"&gt;instance&lt;/span&gt; &lt;span class="nt"&gt;path&lt;/span&gt; &lt;span class="nt"&gt;of&lt;/span&gt; &lt;span class="nt"&gt;persistent&lt;/span&gt; &lt;span class="nt"&gt;variable&lt;/span&gt;
    &lt;span class="nt"&gt;Main_PRG&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Test1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Setpt&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;SETPTSTRUCT&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;That all works fine, but I would now like to initialize one of the variables in Setpt.  So, I do something like this, in Main_PRG&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;    &lt;span class="nt"&gt;Test1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;   &lt;span class="nt"&gt;TEST_FB&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;Setpt&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nt"&gt;Name&lt;/span&gt;&lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s1"&gt;'Test 1'&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; 
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I would expect to see Main_PRG.Test1.Setpt.Name set to 'Test 1' when the PLC boots up.  However, I've discovered that this only works if don't have the instance paths set in PersistentVars.  After I've done step 2 above (Add all instance paths), the initialization doesn't happen anymore.&lt;br/&gt;
I assume this is because the declarations in PersistentVars get initialized after my program variables.  I can sort through my PersistentVars, and initialize everything in there, but that's pretty tedious, as they're not sorted by program.&lt;/p&gt;
&lt;p&gt;Is there way to initialize the persistent variables from the function block declaration in a program POU?&lt;/p&gt;
&lt;p&gt;thanks&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>