Please try out following lines
Defparam DRAWONLASTBARONLY = TRUE
M1 = Barindex
once M2 = M1
DRAWTEXT("M1=#M1#", BarIndex[1], 0.1)
DRAWTEXT("M2=#M2#", BarIndex[1], -0.1)
Return
I would expect that M2 is getting the value of M1 but the value stays zero!
It DOES do what it is expected from it!
That’s because ONCE is executed only ONCE; when you launch that code BARINDEX=0, so will be M1 and, finally… M2, which will NEVER be executed again (while M1 continues growing).
Thanks for your prompt answer. Could you please help me to understand the following:
M1 = barindex
if barindex >2 then
once M2 = M1
endif
return M2
It still returns zero!
Not sure why it does not pick up the value but you do not really need the once. This code will fix the value as you want it. The Once is not necessary if you use an =
if barindex = 3 then
M2 = Barindex
endif
return M2 as "M2", barindex as "BarIndex"
p.s. – it is preferred if you use the ‘Insert PRT Code’ button when posting code in the forums. I’ve tidied up your posts for you!
ONCE is read by the system ONCE at launch time, so your code could be written as
M1 = barindex
if barindex >2 then
//once M2 = M1
endif
return M2
When BarIndex is ZERO the IF…ENDIF block will be skipped, later, when executed, line 3 will be skipped because of the ONCE keyword.
Thanks for your alternative code. You are right there are often many ways around. My sample code had to discover the missfunction of the command ‘once’ just in principle and my hope is to get it working in future. It was thought as a request to the programmer. I obvioused that the missfunction is limited to barindex but it works fine with other values.
ONCE is read by the system ONCE at launch time
Which so obviously why it does not work at BarIndex 3! I must be tired….
Do I get it right that ONCE is executed just one time in the very first loop of a program? So that it is not useful to use ONCE it in a later loop independent on its position? Thanks for your support to getting a better understanding.
ONCE is used to initialize a variable (PRT doeas not tell between variables and constants), as soon as the code is launched, after that it will simply be ignored, no matter where you write it.
So it is no missfunction it was just my misunderstanding of the comand ONCE.
Thank so much for your patience!