Shop OBEX P1 Docs P2 Docs Learn Events
Slight confusion on how to set var — Parallax Forums

Slight confusion on how to set var

DRMorrisonDRMorrison Posts: 81
edited 2017-10-04 04:21 in Propeller 1
I am having a slight issue with setting a var in my main program from and object. I would like to change the var X from within
an object to affect the X var in the main program. I need to use another cog to change the var X, and then when the main program
uses it during its next iteration, it will see the new value that has been changed by the object. I think I need to use the @ symbol somehow,
but I don't fully understand the syntax. Do I simply write @X in the object?

Thanks, Daniel
(the issue is my lack of understanding, or ignorance. I've tried reading about it, but I just don't grasp it quite well enough.)

Comments

  • You have to pass the address of the variable in your main program to your object. Your object is going to have some sort of initialization method like:
    pub start(a,b,c)
       ... stuff ...
    
    You need to have another parameter like:
    var long addr    { place to save passed address }
    pub start(a,b,c,d)
        addr := d   { save the address passed in }
    
    Wherever you want to change the variable, you'd use the following. If the variable is a byte or word, you'd use BYTE() or WORD() instead of LONG():
    LONG(addr) := value
    
    In the main program, you'd pass the address of the variable like this:
    start(5, 10, 103, @variable)
    
  • DRMorrisonDRMorrison Posts: 81
    edited 2017-10-04 04:44
    So, object looks like...
    PUB start(@X)
     WORD(X) := y + z
    
    And then the main program would see the new value the next time it went to use it?
    Do I have to call the object from main, or will the value just change from within the object due to
    internal processing within the object?
  • DRMorrisonDRMorrison Posts: 81
    edited 2017-10-04 05:01
    I think I may have this...
    MAIN PROGRAM:
    VAR WORD X
    
    OBJ newX: "Xchanger"
    
    PUB Main
    
     newX.Start(@X)
    
    OBJECT Xchanger:
    VAR WORD Xadd
    
    PUB Start(X)
     Xadd := X
    
    PUB xchange
    
     WORD(Xadd) := {some new value based upon an input etc.}
    

    Does this look correct?
    Daniel
  • yes

    Mike
  • The line:
    WORD(Xadd) won't compile, but this will: WORD[Xadd]
    I'm using BST

    Daniel
  • Sorry. I got carried away with the parentheses. It indeed is WORD[Xadd]. Looks good.
  • Thanks to everyone, Daniel
Sign In or Register to comment.