The command 'once' is not doing what is expected

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #71154 quote
    Vagus_Lux
    Participant
    Average

    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!

    #71158 quote
    robertogozzi
    Moderator
    Master

    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).

    #71168 quote
    Vagus_Lux
    Participant
    Average

    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!

    #71175 quote
    Vonasi
    Moderator
    Master

    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!

    Nicolas thanked this post
    #71182 quote
    robertogozzi
    Moderator
    Master

    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.

    #71183 quote
    Vagus_Lux
    Participant
    Average

    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.

    #71185 quote
    Vonasi
    Moderator
    Master

    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….

    #71187 quote
    Vagus_Lux
    Participant
    Average

    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.

    #71188 quote
    robertogozzi
    Moderator
    Master

    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.

    #71190 quote
    Vagus_Lux
    Participant
    Average

    So it is no missfunction it was just my misunderstanding of the comand ONCE.
    Thank so much for your patience!

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.

The command 'once' is not doing what is expected


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
Vagus_Lux @vagus_lux Participant
Summary

This topic contains 9 replies,
has 3 voices, and was last updated by Vagus_Lux
7 years, 8 months ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 05/23/2018
Status: Active
Attachments: 1 files
Logo Logo
Loading...