# [C++/Lua] Keeping every script's declarations in their own script

## Recommended Posts

I'm currently working on a 2d platformer project and trying to integrate lua into it to make adding content a ton easier and all that. However I'm running into issues such as if a script I load doesn't have "onWhatever()" defined lua will give me the onWhatever() of the most recent script I loaded that did have this callback instead of simply telling me it isn't there. Pretty much what I'd like to achieve here is make every script independent of each other while maintaining the libraries I've loaded and c functions I've registered in the main lua state. So far the simplest idea I've come across would simply be calling lua_newstate for every script I load, but that would come with issues such as having to reload all c functions and libraries per script, sounds like it'd be kinda slow, and if I want to push a global variable for all scripts to access I'd have to do this across each state (or thats how I understand it). I've looked into this and reading through documentations and stuff have left me fairly confused. It seems lua has stuff like coroutines, threads, and environments I can mess around with and if used properly might achieve what I want, but this leaves the question of which I'd want and how do I achieve this. I've tried messing around for a good while to no avail, which I guess would leave me to why I'm making this thread. So I'll try and summarize this since it looks like I've managed to ramble pretty far on. What I want: -anything defined in a script does not exist in any other script -be able to define global variables & functions in C that are accessible from any script, and if tampered with in a certain script would not affect any other scripts -not having to use lua_newstate per script because of speed and complexity issues (although I might be wrong in thinking pushing values for every state would really slow stuff down, and depending on how I work stuff out this might be something I want to do frequently)

##### Share on other sites
using the keyword "local" will instantiate variables and functions at file scope or function scope depending on ur current scope.

So for example in scriptA

local myVal = 0;local function CommonFuncName()  print("I am common function number 1! myVal "..myVal);endreturn function() CommonFuncName(); end

in scriptB

local myVal = 1000;local function CommonFuncName()  print("I am common function number 2! myVal "..myVal);endreturn function() CommonFuncName(); end

in your main script u could have something like this.

local a = require "scriptA"local b = require "scriptB"local function CommonFuncName()  print("I am common function number 3!");endCommonFuncName();a();b();

Each script will call its own unique CommonFuncName. It's not normal to return a function at the end of a script, we just do it for demonstration purposes.

I'm not in a position to test this, but it should work ( print out uniquely for each call )

Good Luck!

-ddn

##### Share on other sites
Ah right I probably should've mentioned that I tried using local instances before, but the way I have it setup doesn't seem to co-operate with this. What I've been doing is loading each script ahead of time and then using lua_ref to store references to each function that I'll be calling later on from C++ when certain conditions are met (like for onCollide, onDeath, etc) because as far as I can tell this seems to be the way to call functions quickly without having to reparse a script. Unfortunately lua_ref and lua_pcall do not seem to like calling local functions and I'm not sure if I'm calling/storing things wrong or just going about this the wrong way in general.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628307
• Total Posts
2981972

• 9
• 13
• 11
• 12
• 11