Shop OBEX P1 Docs P2 Docs Learn Events
Propeller and shared variables — Parallax Forums

Propeller and shared variables

danthonydanthony Posts: 45
edited 2008-12-23 22:24 in Propeller 1
Does anyone know how to reference a variable outside of the top object from within the top object?

For instance if you had a main object and wanted to get the value from a variable in a child object of that main parent.

Thanks in advance for any help

Comments

  • John AbshierJohn Abshier Posts: 1,116
    edited 2008-12-23 21:18
    You could put and use a accessor function in the child object:

    PUB GetX
    return x

    John Abshier
  • danthonydanthony Posts: 45
    edited 2008-12-23 21:34
    Would you still have to use a VAR X declaration in both parent and child? I tried it both ways and could not get it to compile.
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-12-23 22:17
    Hello Danthony,

    variables from the VAR-section are only direct accessible from INSIDE that object.

    let's say you have a Topobject

    MyTopObect.spin

    inside the file MyTopObect.spin you have declared another object in the OBJ-section

    OBJ
      MyChild : "MyChildObject"
    
    
    



    inside the file MyChildObject.SPIN you have a variable in the var-section




    VAR
      long MyX 
    
    
    



    and you want to READ this variable in the file MyTopObect.spin
    for this you have to define a method inside the MyChildObject.SPIN

    PUB GetMyX
      result := MyX
    
    



    now you can READ the value of variable MyX inside the MyTopObect.spin
    by coding


      Y := MyChild.GetMyX
    
    



    if you want to WRITE into variable MyX you have to define a method inside the MyChildObject.SPIN

    PUB PutMyX (value)
      MyX := value
    
    



    and use it inside the the MyTopObect.spin

      MyChild.PutMyX(123)
    
    



    which wil stor evalue "123" into variable MyX

    The names of the variables, objects and methods are free to choose
    within the limitations of names in SPIN in general start with letter (and not a digit etc.)


    best regards

    Stefan
  • danthonydanthony Posts: 45
    edited 2008-12-23 22:24
    Thanks. That was super helpful.
Sign In or Register to comment.