Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 14:28 damir_: so, I have many objects in my c++ code, for which I want to write different ai behaviours in lua. Each behaviour script is a seperate .lua file containing the update_ai() function.
- 14:29 damir_: What is the best way to achieve this? Do I make new lua state for every object and compile the appropriate script every time?
- 14:29 Dylan343_: load each file into a function, set the environment to a blank new table, run the function
- 14:29 pasqoo has joined (~abcd@dynamic-adsl-94-37-84-90.clienti.tiscali.it)
- 14:30 Dylan343_: then save that table, or save the update_ai key from it
- 14:30 cmeid1 has joined (~cmeid@h1822732.stratoserver.net)
- 14:30 Dylan343_: look at sandboxing on the wiki
- 14:34 damir_: aren't sandboxes useful for safety? I don't care for that at this point.
- 14:35 guampa has left IRC (Ping timeout: 260 seconds)
- 14:35 doub: damir_: why do you want to use veral states then ?
- 14:36 doub: *several
- 14:36 Dylan343_: sandboxes are quite useful for unsafe isolation
- 14:37 damir_: I just want to decide at runtime which ai script will I use for each object
- 14:38 damir_: I want to have those ai functions in seperate files but under the same function name
- 14:38 hoelzro: damir_: I recommend Dylan343_'s approach as well
- 14:38 hoelzro: sandboxes are used for more than just security
- 14:39 hoelzro: I guess in this context it's not really a sandbox
- 14:39 hoelzro: but the same idea (and techniques) apply
- 14:39 damir_: Ok, I'll look into it, thanks
- 14:43 JNZ: Kinnison: How do you mean? Are you saying the number of elements returned is pushed last?
- 14:43 doub: damir_: sandboxing is probably over-complicated for your needs
- 14:44 doub: how do you select which script goes to which object ?
- 14:44 damir_: doub, I have my own c++ logic for that, could just be rand()
- 14:44 TheBunny has joined (~CrackBunn@12.54.225.20)
- 14:44 doub: how do you identify a script, is it by filename ?
- 14:45 damir_: yes
- 14:45 doub: and do you need the update function in the script to be a global variable ?
- 14:46 damir_: hmm not sure about that. It just needs to have predefined name and parameters.
- 14:46 doub: why do you need a name ?
- 14:47 damir_: I'm not sure how else to feed it parameters
- 14:47 doub: you can put the functions in a table, and use the filename as key
- 14:47 damir_: otherwise I might need it if in the future the scripts contains more than one function
- 14:47 doub: for example in pseudo-code :
- 14:48 doub: my_ai_functions[filename] = function(arg1, arg2) --[[ ... ]] end
- 14:49 doub: if you need several functions, then it's probably easier to use a setfenv-based solution, be it modules, full-fledged sandboxing, or a simple custom solution
- 14:49 Kinnison: JNZ: No, you record the stack depth before and aafter
- 14:49 guampa has joined (~guampa@unaffiliated/guampa)
- 14:50 doub: i'd pick the last, since it represent the least amount of code on your side
- 14:50 damir_: ok, so I do _loadfile, and then how do I get the 'pointer' to the just loaded function?
- 14:50 guampa has left IRC (Max SendQ exceeded)
- 14:51 doub: after loadfile, none of your user functions has been created
- 14:51 doub: you need to run the script
- 14:51 guampa has joined (~guampa@unaffiliated/guampa)
- 14:52 damir_: right, I do that with _pcall
- 14:52 doub: do you load it from C code of from Lua ?
- 14:52 damir_: from c
- 14:52 doub: if you simply pcall it, the global definitions in the script will go to the shared global environment, and scripts will overwrite each other
- 14:53 doub: you need to redirect those writes to a table per script between the load and the pcall
- 14:54 doub: basically: script = loadfile(filename); globals = setmetatable({}, {__index=_G}); setfenv(script, globals); script(); my_ai_functions[filename] = globals
- 14:54 doub: port that to C (ask if you need help), and you should be set
- 14:55 Vigud has left IRC (Remote host closed the connection)
- 14:55 Vigud has joined (u1143@gateway/web/irccloud.com/x-eeqgzvurbsvtdpjt)
- 14:58 jfi has left IRC (Quit: Ex-Chat)
- 14:58 jfi has joined (~jfi@vbo91-3-82-243-221-231.fbx.proxad.net)
- 14:58 damir_: hmm why is the 'script' variable important to setfenv? It should be always 0 if loading was OK
- 14:59 jfi has left IRC (Read error: Connection reset by peer)
- 14:59 hoelzro: damir_: loadfile returns the function chunk
- 14:59 hoelzro: loadfile ~= luaL_loadfile
- 15:00 doub: oh, right, script is the "logical result", ie. the compiled script, not the status of lua_loadfile (but rather what it left on the stack)
- 15:01 VShell: script is the compiled code. calling script() will run that code.
- 15:01 lazyden has left IRC (Quit: lazyden)
- 15:01 jfi has joined (~jfi@vbo91-3-82-243-221-231.fbx.proxad.net)
- 15:02 jsnyder has joined (~jsnyder@c-98-220-236-228.hsd1.il.comcast.net)
- 15:04 StrangerDanger has left IRC (Read error: Connection reset by peer)
- 15:06 damir_: what's the C counterpart for loadfile?
- 15:07 q66 has joined (~quaker66@213.191.105.214)
- 15:07 VShell: luaL_loadfile
- 15:08 FlipFlop_ has left IRC (Ping timeout: 265 seconds)
- 15:08 VShell: or, at a lower level, if you want to provide the source from something other than a file, lua_load
- 15:17 damir_: ok, I need to read some material on lua stack, tables etc :)
- 15:17 damir_: thanks for now
- 15:21 dnyy has left IRC (Ping timeout: 252 seconds)
- 15:32 ygrex_at_job has left ()
- 15:36 ygrex_at_job has joined (~ygrex_at_@ygrex.ru)
- 15:37 Ketho has joined (~Ketho@ip565063bd.direct-adsl.nl)
- 15:39 damir_ has left IRC (Remote host closed the connection)
Add Comment
Please, Sign In to add comment