Collect component before or after OnRun?

Hi there!

I’m working on a Python Script behavior for my robot. I store the robot component BEFORE the OnRun function is called (so when compiling). I noticed an example script in the VC Academy stores the robot component in a variable INSIDE the OnRun function. I’m wondering, are there situations when one is recommended instead of the other?

My code:
image

VC Academy code:

If you need the variable comp in other methods, declare the global variable comp at the top of your script before defining any methods. If you declare the variable comp in OnRun, print_my_behaviours will not know what the variable comp is.

from vcScript import *

comp = getComponent()

def OnRun():
  print_my_behaviours()
  
def print_my_behaviours():
  for behaviour in comp.Behaviours:
    print behaviour.Name
1 Like