Once function not working!

Forums ProRealTime English forum ProBuilder support Once function not working!

Viewing 7 posts - 1 through 7 (of 7 total)
  • #48564

    I have a really simple code here which doesn’t work as you expect. Can anyone explain why?

    If High > High[1] Then
    Once x = 1
    Endif
    Return x

    • If you delete word “Once” then it works as you would expect!
    • If you change the If condition to something like “If 5 = 5” then it works as you would expect!
    • If you take “Once x = 1” out of “If condition” for example you put “Once x =1” on the first line then it works as you would expect!

    It’s weird! This is part of a complex code which I am writing and trying to debug.

     

    #48565

    ONCE is used to initialize a variable and cannot be used within IF…THEN blocks, it’s not what it is made for!

    I suggest writing your code like that (sorry for not being able to insert PRT code from my smartphone):

    ONCE x = 0
    IF high > high[1] THEN
    x = 1
    END IF
    RETURN x

    Roberto

    #48567

    In my/your example ONCE should not be used at all, just leave x=0, because ONCE is executed only the first time, when x contains 1 it will remain 1 thereafter, thus  returning wrong results!

    #48579

    I was using the above code to find the bug in my main code. Below is the simplified version of the code. I noticed when Once comes after If, it sometimes works as expected but sometimes not. In this code, the result for x is as expected but result for y is zero which is weird!

     

    #48580

    I won’t be at my desk till monday morning.

    I’ll try to test it afterwards.

    #48581

    I think I actually may have found the answer:

    ProRealtime reads the Once function without respecting the If condition. If the If condition is met prior to ProRealtime reading the Once function then Once function would be applied otherwise Once function would never be executed. Pretty interesting huh!

    #48597

    ONCE is only made to give a variable a value one time, it cannot be used many times in a conditional block like you did. If you want that your line 4 conditions give only one time a value to a variable and then compare this value with another one, you should use 2 different variables names and compare them together instead, and get rid of the ONCE instruction.

Viewing 7 posts - 1 through 7 (of 7 total)

Create your free account now and post your request to benefit from the help of the community
Register or Login