delegation.rst 879 B

1234567891011121314151617181920212223242526272829303132333435
  1. .. _delegation:
  2. ========================
  3. Delegation
  4. ========================
  5. .. index::
  6. single: Delegation
  7. Squirrel supports implicit delegation. Every table or userdata can have a parent table
  8. (delegate). A parent table is a normal table that allows the definition of special behaviors
  9. for his child.
  10. When a table (or userdata) is indexed with a key that doesn't correspond to one of its
  11. slots, the interpreter automatically delegates the get (or set) operation to its parent.::
  12. Entity <- {
  13. }
  14. function Entity::DoStuff()
  15. {
  16. ::print(_name);
  17. }
  18. local newentity = {
  19. _name="I'm the new entity"
  20. }
  21. newentity.setdelegate(Entity)
  22. newentity.DoStuff(); //prints "I'm the new entity"
  23. The delegate of a table can be retreived through built-in method ``table.getdelegate()``.::
  24. local thedelegate = newentity.getdelegate();