NoobScratcher
NoobScratcher

Reputation: 139

lual_dofile(); wont load script with C++ and Lua

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 ?**

Upvotes: 3

Views: 6125

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 473272

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.

Upvotes: 8

Related Questions