3

Hello i'm trying to get this code to work

extern "C"
{
#include <lua.h>
#include "lualib.h"
#include "lauxlib.h"
}

#include "glew.h"
#define GLEW_STATIC
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <stdlib.h>
#include <iostream>
#include <fstream>




////////////////////////
////////////////////////
test.h
void Test(void)

{
    int status;

**//The Lua Interpreter**
lua_State  *L = lua_open();


**//Open Lua Libarys**
luaL_openlibs(L);



**//Run Lua Script**
status = luaL_loadfile(L,"Test.lua");

printf( "actually getting to this point!");
getchar();
//Close Lua
lua_close(L);



}

this is named test.lua this is my lua file

print"Whats your name?"

function sleep(n)
end

this doesn't work :(

lual_dofile(L,"Test.lua");

the hole program compiles but then doesn't execute the script or show any visual feedback of the lua script running has anyone encountered this problem before? and has any knowledge of why ?**

Saro Taşciyan
  • 5,210
  • 5
  • 31
  • 50
NoobScratcher
  • 139
  • 2
  • 8
  • 2
    Is you sample script named `test.lua` or `Test.lua`? Also, the right function is `luaL_dofile`, not `lual_dofile`. Finally, make sure to handle the return value. – lhf Oct 25 '11 at 16:58
  • 2
    "this doesn't work :(" What do you mean that it doesn't work? _How_ doesn't it work? – Nicol Bolas Oct 25 '11 at 17:20
  • Here is some information that is useful with Lua 5.2 using the Lua 5.2 rather than older calls for integrating with C++ programs. http://www.troubleshooters.com/codecorn/lua/lua_c_calls_lua.htm – Richard Chambers Dec 15 '12 at 20:16

1 Answers1

8

You seem to not understand a few things.

luaL_loadfile loads the script from a file, compiles it, but it does not run it. It just pushes it onto the Lua stack, so that you can run it with whatever parameters you see fit.

luaL_dofile loads the script from a file, compiles it, and runs it, thus popping the script off the stack.

In the case of errors, luaL_loadfile will return an error code explaining what kind of error it is. It also pushes an error message onto the Lua stack if there is an error. If no errors happen, it returns 0.

Similarly, luaL_dofile will return 1 if an error happens, and the error message will be on the Lua stack.

You should always be checking the return value of these functions to see if an error has happened, and acting accordingly.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • how do i check return values i commonly just do if statements in most cases in c++ – NoobScratcher Oct 25 '11 at 17:28
  • @NoobScratcher: You need me to tell you how to check the return value from a function? You store it in a variable and test it against the possible return values. Or you stick it in an `if` statement. Or however else you need to do it. Lua API functions are just C functions. They're not magic; they work like C does. – Nicol Bolas Oct 25 '11 at 17:33
  • int return_value = return if(return_value = 1) { fprintf(stderr,"Hello doesnt work"); } – NoobScratcher Oct 25 '11 at 17:40
  • @NoobScratcher: Your understand of how C works seems to be lacking. Perhaps you should get a [good book on C](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) or [C++](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Nicol Bolas Oct 25 '11 at 17:44
  • Make sure to print the error message in the stack in case of errors: `fprintf(stderr,"luaL_dofile failed: %s\n",lua_tostring(L,-1))`. – lhf Oct 25 '11 at 18:05