-2

Basically I don't know how to do something like that:

    lua_pushcfunction(L, int func(lua_State* L) { 
      printf("hello");
      return 0;
    });

I have tried many stuff but doesnt just work

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Void E
  • 1
  • 1
  • Possible duplicate: https://stackoverflow.com/questions/9410/how-do-you-pass-a-function-as-a-parameter-in-c – alter_igel Mar 07 '19 at 22:43
  • not what I mean, they create functions then pass them and I want to do it the way there – Void E Mar 07 '19 at 22:44
  • Perhaps you need [lambda expressions](https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11)? – alter_igel Mar 07 '19 at 22:45

1 Answers1

2

Two ways:

  1. Define the function then push it.

    int func(lua_State* L) { 
      printf("hello");
      return 0;
    };
    
    // later...
    lua_pushcfunction(L, func);
    

    This is the only way you could do it in C, or before C++11.

  2. Use a lambda expression (aka. anonymous function):

    lua_pushcfunction(L, [](lua_State* L) -> int { 
      printf("hello");
      return 0;
    });
    
user253751
  • 57,427
  • 7
  • 48
  • 90