calling_a_function.rst 870 B

123456789101112131415161718192021222324252627
  1. .. _embedding_calling_a_function:
  2. ==================
  3. Calling a function
  4. ==================
  5. To call a squirrel function it is necessary to push the function in the stack followed by the
  6. parameters and then call the function sq_call.
  7. The function will pop the parameters and push the return value if the last sq_call
  8. parameter is > 0. ::
  9. sq_pushroottable(v);
  10. sq_pushstring(v,"foo",-1);
  11. sq_get(v,-2); //get the function from the root table
  12. sq_pushroottable(v); //'this' (function environment object)
  13. sq_pushinteger(v,1);
  14. sq_pushfloat(v,2.0);
  15. sq_pushstring(v,"three",-1);
  16. sq_call(v,4,SQFalse);
  17. sq_pop(v,2); //pops the roottable and the function
  18. this is equivalent to the following Squirrel code::
  19. foo(1,2.0,"three");
  20. If a runtime error occurs (or a exception is thrown) during the squirrel code execution
  21. the sq_call will fail.