I've been using the luaL_loadbuffer for many years to load Lua code from within a C++ program. Suddenly I find I need the script to know its own name. Sure, the script in an anonymous function as far as the Lua context is concerned but the C++ framework around it keeps it in a hashmap with a name, the name of the file from which it was loaded to be precise.
I passed that file name into luaL_loadbuffer when I originally wrote the code but I never actually used it. I now need that name so I can have the script compute metrics about its own execution.
luaL_loadbuffer(LuaContext, code, strlen(code), name)
I now need to use that name from with the Lua context. What's the easiest way to do that?
I'm going to tap the Lua debug function documentation in the meantime while waiting for an answer.
When that code is running, debug.getinfo(1).src will give you name.
Related
I would like to get the C++ function names in a file. I tried do this with the command diff because I only need get the name of the function which is modified but I could not get it.
I know that with python files it is possible using git with the option 'git diff file.py'.
Is it possible do it with c++ files in Mercurial?
I think you are looking for the config option diff.showfunc. The doc (hg help config) says:
"showfunc"
Show which function each change is in.
It's working fine for Python functions and I think the heuristic should work similarly for C++ files. Maybe it will need functions body to be indented to correctly detect C++ functions.
I'm not even sure if my plan for doing things is the best way, so I apologize if this post is a bit vague. Also, I understand that similar questions have been asked before. However, I haven't been able to find anything that pertained to my situation and that made sense to me.
So me and my friends from school are building arcade machine, and I'm planning on putting together the main GUI that allows the user to select different games and load them if they have enough tokens. However, these separate windows will have to share some variables, mainly the number of tokens in the machine. I figured a separate Lua program could store such variable, and also have requests sent to it to perform other functions like opening and closing the different windows. Also, in case it's important to note, we will be using the Love2D engine for the games and be running all this on a Linux machine.
By what I've read, there seems to be some C and C++ code involved in this. I know next to nothing about C or C++, and we're trying to get this project moving along, so if you could include some code in your answer and instruct me on how to use it, that'd would be amazing. I can come back later and learn some C or C++, but right now Lua is my top priority.
My questions:
Is there a better way to accomplish what I'm trying to do?
How should I go about doing this?
Can this be done solely with Lua, or is some C, C++, or any other external programming language/utility/etc. required?
Also, incase anyone brings it up, I have tried using global variables, but I couldn't seem to get two programs/scripts to use the same variable at once.
Again, sorry if I'm being a bit vague.
Thanks in advance!
(this method is a combination of #Puzzlem00n's suggestion and reading the comments, so I shouldn't take much credit from this at all)
Putting multiple games in one lua program!
In main.lua:
require("spaceInvaders") --require all the other Lua files
-- without the ".lua" at the end!
gamestate=spaceInvaders
function love.load()
--all the setup things
end
function love.update()
gamestate.update()
end
function love.draw()
gamestate.draw()
end
function love.keypressed(key)
gamestate.keypressed(key)
end
--and so on for the rest of the love callbacks that you need (such as love.keyreleased())
Then in each game (which is a separate Lua file such as spaceInvaders.lua):
spaceInvaders = {}
function spaceInvaders.draw()
--your draw code
end
function spaceInvaders.update()
--your update code
end
--and so on for every other callback you want to use
What this code does is it gives each game its own set of love functions. When you want to play that game, you should set gamestate to that game's name. The spaceInvaders={} line defines spaceInvaders as a table, which stores each function in place. When you define a variable as an existing variable, you are actually creating a reference to it, for example:
t = {}
table.insert(t,1) --place 1 inside t
table.insert(t,3) --also place 3 inside t
s = t
print(s[1]) --will print 1, the first value of t
t[1]=2
print(s[1]) --will print 2, as it refers to t[1], which has just been changed
Sharing variables! (I worked out this bit!)
NOW, this means you can send variables around the program with another function. If you want to share score between games, you could do the following:
in main.lua:
function love.update()
gamestate.update()
score = gamestate.returnScore() --score will always equal to the current score being returned by the game
end
in a game, such as spaceInvaders.lua:
function spaceInvaders.returnScore()
return score --or whatever the score is stored in
end
This will allow you to get a variable, such as score, from one game to main.lua!
I'm sorry if this is a little confusing but hopefully this is what you're looking for! :)
I am writing a drop-down console for my app. Suppose I want to output the value of variable myvar by using the following command:
]/get myvar
Is there a better way than to create a map so that the output is
return mymap[argv[0]]; ?
In other words, can I associate the input char array "myvar" to the variable named myvar without doing it manually for all the variables in the program.
Short answer:
No.
Long answer:
Pfff, no way! After your piece of code is build as a binary, there is no such thing as a variable name. Just some pointers, values on the stack, and so on...
If you want to implement something like this, I would recommend you to go for a scripting library (Lua, for example), and manually map some variables so you can read/change those variables via scripts. In this case the console input is basically what you are feeding to the script engine.
This might be a good reference.
UPDATE:
In fact, just found the project called Lua Console.
Seems like it's not maintained anymore, but it doesn't mean it will not work.
I know how I can bind C++ functions to AngelScript, but in my C++ code, how do I load an .as script file? How can I say in my C++ "Execute myscript.as now!" ?
In the AngelScript API I don't find any function like "LoadScript" or "ExecuteScript".
Or do I have to define a path somewhere from where AngelScript loads all scripts and I don't need to tell it the exact files?
Just found it out (in a small side sentence in the docs):
AngelScript doesn't provide a build in file loading. That's why there is no API function. So the manual loading is indeed the only way.
asIScriptModule::AddScriptSection will load a script string. asIScriptContext::Execute will execute a function from a script. The documentation was pretty clear about all of this; you might want to give it a look.
My problem is that I'm having trouble specifying paths for Lua to look in.
For example, in my script I have a require("someScript") line that works perfectly (it is able to use functions from someScript when the script is run standalone.
However, when I run my app, the script fails. I believe this is because Lua is looking in a location relative to the application rather than relative to the script.
Hardcoding the entire path down to the drive isn't an option since people can download the game wherever they like so the highest I can go is the root folder for the game.
We have XML files to load in information on objects. In them, when we specify the script the object uses, we only have to do something like Content/Core/Scripts/someScript.lua where Content is in the same directory as Debug and the app is located inside Debug. If I try putting that (the Content/Core...) in Lua's package.path I get errors when I try to run the script standalone.
I'm really stuck, and am not sure how to solve this. Any help is appreciated. Thanks.
P.S. When I print out the default package.path in the app I see syntax like ;.\?.lua
in a sequence like...
;.\?.lua;c:...(long file path)\Debug\?.lua; I assume the ; means the end of the path, but I have no idea what the .\?.lua means. Any Lua file in the directory?
You can customize the way require loads modules by putting your own loader into the package.loaders table. See here:
http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders
If you want to be sure that things are nicely sandboxed, you'll probably want to remove all the default loaders and replace them with one that does exactly what you want and nothing more. (It will probably be somewhat similar to one of the existing ones, so you can use those as a guide.)