Results 1 to 12 of 12
  1. #1
    Blue Kirby's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Posts
    181
    Reputation
    49
    Thanks
    600
    My Mood
    Psychedelic

    How to load lua files and call runstring directly from lua_State

    Loading lua files directly from ILuaInterface is shitty and has a lot of bullshit you don't need to deal with. There's a much better method.
    Get your lua state however you normally would.

    Code:
    lua_State* state = g_Lua->GetLuaState();
    Then make sure nothing is on the stack so you don't fuck anything up. Using loadstring and loadfile only puts the chunk on the stack, then you have to manually call it using call or pcall.

    Note: pcall is extremely recommended. The difference is pcall is just protected call and will stop if there is an error.

    Code:
    if (!lua_gettop( state ))
    {
     luaL_loadstring( state, "print(\"My message\")" );
     lua_pcall( state, 0, 0, 0 );
    
     luaL_loadfile( state, "file.lua" );
     lua_pcall( state, 0, 0, 0 );
    }
    It pops the chunk off the stack, so you don't need to worry with that.

    Enjoy having tons of swag.

  2. The Following User Says Thank You to Blue Kirby For This Useful Post:

    TacosCANwrap1 (07-25-2016)

  3. #2
    TheyCallMeDaz's Avatar
    Join Date
    May 2013
    Gender
    female
    Location
    Ukraine
    Posts
    367
    Reputation
    58
    Thanks
    279
    My Mood
    Yeehaw
    Hmm, whenever I tried this the lua state got destroyed and never got called. I'm probably being an idiot and doing it wrong, though.

  4. #3
    Azrius's Avatar
    Join Date
    Jun 2013
    Gender
    female
    Posts
    238
    Reputation
    10
    Thanks
    251
    My Mood
    Bashful
    i'm a c++ baby, so excuse my stupid questions

    what headers do you need to compile this? how do you get it into the game? injection or require?

  5. #4

  6. #5
    Azrius's Avatar
    Join Date
    Jun 2013
    Gender
    female
    Posts
    238
    Reputation
    10
    Thanks
    251
    My Mood
    Bashful
    Quote Originally Posted by LennyPenny View Post
    thanks, i'll assume that it's require then?

  7. #6
    LennyPenny's Avatar
    Join Date
    May 2013
    Gender
    male
    Posts
    314
    Reputation
    10
    Thanks
    381
    My Mood
    Sad
    Quote Originally Posted by Azrius View Post
    thanks, i'll assume that it's require then?
    Sry, can't help with that. I tried making modules for gmod but I just can't get my head around all this "stack based development".
    But this might help you getting round having to run a lua file with require https://www.mpgh.net/forum/713-garrys...s-require.html.

  8. #7
    Azrius's Avatar
    Join Date
    Jun 2013
    Gender
    female
    Posts
    238
    Reputation
    10
    Thanks
    251
    My Mood
    Bashful
    Quote Originally Posted by LennyPenny View Post
    Sry, can't help with that. I tried making modules for gmod but I just can't get my head around all this "stack based development".
    But this might help you getting round having to run a lua file with require https://www.mpgh.net/forum/713-garrys...s-require.html.
    thanks for trying to help anyway, i'll look into it

  9. #8
    animedad's Avatar
    Join Date
    Aug 2013
    Gender
    male
    Posts
    6
    Reputation
    10
    Thanks
    0
    Quote Originally Posted by Azrius View Post
    thanks for trying to help anyway, i'll look into it
    this is injection based not require based
    (at least that's how i do it)
    Last edited by animedad; 09-02-2013 at 01:02 PM.

  10. #9
    TheyCallMeDaz's Avatar
    Join Date
    May 2013
    Gender
    female
    Location
    Ukraine
    Posts
    367
    Reputation
    58
    Thanks
    279
    My Mood
    Yeehaw
    Lenny is an idiot, get the SDK from AlliedModders and compile it with BlackAwps backwards compat headers.

  11. #10
    Blue Kirby's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Posts
    181
    Reputation
    49
    Thanks
    600
    My Mood
    Psychedelic
    Quote Originally Posted by Azrius View Post
    i'm a c++ baby, so excuse my stupid questions

    what headers do you need to compile this? how do you get it into the game? injection or require?
    Either one works, but I use injection. You need to use GetProcAddress for the lua functions.

  12. #11
    Azrius's Avatar
    Join Date
    Jun 2013
    Gender
    female
    Posts
    238
    Reputation
    10
    Thanks
    251
    My Mood
    Bashful
    thanks guys, i really appreciate it

  13. #12
    Blue Kirby's Avatar
    Join Date
    Apr 2011
    Gender
    male
    Posts
    181
    Reputation
    49
    Thanks
    600
    My Mood
    Psychedelic
    Quote Originally Posted by Azrius View Post
    thanks guys, i really appreciate it
    No problem. I'm always willing to teach those that want to learn.

Similar Threads

  1. [Video] Loading lua files before autorun/client
    By Blue Kirby in forum Garry's Mod Discussions & Help
    Replies: 14
    Last Post: 05-08-2013, 02:06 AM
  2. How to use GSC files? and .iwd mods?
    By Armanii167 in forum Call of Duty Modern Warfare 2 GSC Modding Help/Discussion
    Replies: 1
    Last Post: 01-29-2011, 12:12 AM
  3. How to start Combat Arms from a file and not from the internet
    By shooterman80 in forum Combat Arms Discussions
    Replies: 20
    Last Post: 02-10-2010, 04:53 AM
  4. [Question] How To Inject LTB FILES? And How To Swap Guns
    By stanley2428 in forum Combat Arms Mod Discussion
    Replies: 2
    Last Post: 12-19-2009, 11:33 PM
  5. How to add an attachment file and how to make CA hacks.
    By Klumzy in forum Programming Tutorial Requests
    Replies: 2
    Last Post: 07-15-2009, 12:52 PM