Shop OBEX P1 Docs P2 Docs Learn Events
How to get variable from another cog — Parallax Forums

How to get variable from another cog

Lets say I have some methods running in a cog. In that cog I have a byte variable called poll.

Now in my main cog I want to get the value of of "poll". Is there a simple way of doing this? Do I use the @ sign somehow?

Thanks.

Comments

  • Add the method below to the child object.
    PUB GetPoll
    
      result := poll
    

    The parent object calls this each time it wants to know the value of poll.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2016-05-18 16:36
    Alternatively you could return the address of poll to the parent object.
    PUB GetPollAddress
    
      result := @poll
    

    In this case the parent object just needs to call this method once:
    pollAddress := GetPollAddress
    

    When the parent object wants to know what the value of poll is it uses:
    long[pollAddress]
    

    Let's say the parent wants to take some action when poll equals two. It would use code like this:
      if long[pollAddress] == 2
        ' do stuff when poll equals 2
    
  • Don MDon M Posts: 1,652
    Excellent Duane. Thanks!
Sign In or Register to comment.