Skip to main content
gamedev.net
🔒 Locked

loading a lua script but not executing it

Started by iliak 4 replies 1.9k views
Original Post
iliak
iliak
Hi I want to (pre)load lua script and execute it later not a the load time. Here's what I'm doing :

Lua = luaL_newstate();
luaL_openlibs(Lua);
luaL_loadfile(Lua, "data/script.lua");

lua_pcall(Lua, 0, 0, 0);    // I have to call this function else
                            // the script isn't working

lua_getglobal(Lua, "Init");
if (lua_isfunction(Lua, -1))
{
	lua_call(Lua, 0, 0);
}

Does anyone have a solution ?
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]
Prozak
Prozak
A LUA script is just a string of characters, all you have to do is grab the file and load it onto a big enough buffer, and then run that with RunString, or conversely run the file directly with RunFile.

If what you mean is that the moment you "run" the script, it runs the code, then what you have to do is encapsulate your lua code into a function, so, if you know have this:
_ some_global_vars = value;
_ some_functions
_ code

...then you need to convert that to this:
_ some_global_vars = value;
_ some_functions
_ main_function

...then you can run the script, and initial global variables and functions will get defined. When you want to run the actual body of the script, so that it does whatever it is meant to do, then run the "main_function".
intransigent-seal
intransigent-seal
Quote:
Original post by Prozak
A LUA script is just a string of characters, all you have to do is grab the file and load it onto a big enough buffer, and then run that with RunString, or conversely run the file directly with RunFile.

No (well, of course it's true technically, but it's not really a good way of doing it).

When you load a script file with lua_load (or luaL_loadfile) the code of the file is compiled into a function (or more correctly, a chunk), which is placed on the stack. This value can then be manipulated using all the normal stack manipulation API functions. For example, you can place it in the lua 'registry' table, so that it can be accessed later.

It may be a good idea to run files on load and make sure all active code is kept in global functions that can be called later, but that's a design decision you must make for yourself. There's certainly little point in retaining script files as strings in memory.

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
DakeDesu
DakeDesu
you eval can do?

#include <iostream>// area of code to do soifstream preload;preload.open("file.lua");// end


or reason your hd optimised is not?
trevaaar
trevaaar
Just pre-loading the contents of the script file into memory are probably not what you want to do. If I understand right you actually want to read and prepare the script (i.e. load the script and have the Lua engine compile it).

What you are doing currently looks like your script makes a global function (called Init) to be run whenever it's needed. This should work fine as long as all your script does is define the function.

Another alternative method is to write your script normally, load it and then store it in the registry rather than running it. You can do it something like this:
// load the scriptluaL_loadfile(Lua, "data/script.lua");int ref = luaL_ref(Lua, LUA_REGISTRYINDEX);// the script has been popped off the stack, it's in the registry and// ref is an index into the registry we can use to access it again, like so:lua_rawgeti(Lua, LUA_REGISTRYINDEX, ref);lua_call(Lua, 0, 0);// and to get rid of it when we're doneluaL_unref(Lua, LUA_REGISTRYINDEX, ref);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.