Guest User

Untitled

a guest
Jan 26th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.32 KB | None | 0 0
  1. 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.
  2. 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?
  3. 14:29 Dylan343_: load each file into a function, set the environment to a blank new table, run the function
  4. 14:29 pasqoo has joined (~abcd@dynamic-adsl-94-37-84-90.clienti.tiscali.it)
  5. 14:30 Dylan343_: then save that table, or save the update_ai key from it
  6. 14:30 cmeid1 has joined (~cmeid@h1822732.stratoserver.net)
  7. 14:30 Dylan343_: look at sandboxing on the wiki
  8. 14:34 damir_: aren't sandboxes useful for safety? I don't care for that at this point.
  9. 14:35 guampa has left IRC (Ping timeout: 260 seconds)
  10. 14:35 doub: damir_: why do you want to use veral states then ?
  11. 14:36 doub: *several
  12. 14:36 Dylan343_: sandboxes are quite useful for unsafe isolation
  13. 14:37 damir_: I just want to decide at runtime which ai script will I use for each object
  14. 14:38 damir_: I want to have those ai functions in seperate files but under the same function name
  15. 14:38 hoelzro: damir_: I recommend Dylan343_'s approach as well
  16. 14:38 hoelzro: sandboxes are used for more than just security
  17. 14:39 hoelzro: I guess in this context it's not really a sandbox
  18. 14:39 hoelzro: but the same idea (and techniques) apply
  19. 14:39 damir_: Ok, I'll look into it, thanks
  20. 14:43 JNZ: Kinnison: How do you mean? Are you saying the number of elements returned is pushed last?
  21. 14:43 doub: damir_: sandboxing is probably over-complicated for your needs
  22. 14:44 doub: how do you select which script goes to which object ?
  23. 14:44 damir_: doub, I have my own c++ logic for that, could just be rand()
  24. 14:44 TheBunny has joined (~CrackBunn@12.54.225.20)
  25. 14:44 doub: how do you identify a script, is it by filename ?
  26. 14:45 damir_: yes
  27. 14:45 doub: and do you need the update function in the script to be a global variable ?
  28. 14:46 damir_: hmm not sure about that. It just needs to have predefined name and parameters.
  29. 14:46 doub: why do you need a name ?
  30. 14:47 damir_: I'm not sure how else to feed it parameters
  31. 14:47 doub: you can put the functions in a table, and use the filename as key
  32. 14:47 damir_: otherwise I might need it if in the future the scripts contains more than one function
  33. 14:47 doub: for example in pseudo-code :
  34. 14:48 doub: my_ai_functions[filename] = function(arg1, arg2) --[[ ... ]] end
  35. 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
  36. 14:49 Kinnison: JNZ: No, you record the stack depth before and aafter
  37. 14:49 guampa has joined (~guampa@unaffiliated/guampa)
  38. 14:50 doub: i'd pick the last, since it represent the least amount of code on your side
  39. 14:50 damir_: ok, so I do _loadfile, and then how do I get the 'pointer' to the just loaded function?
  40. 14:50 guampa has left IRC (Max SendQ exceeded)
  41. 14:51 doub: after loadfile, none of your user functions has been created
  42. 14:51 doub: you need to run the script
  43. 14:51 guampa has joined (~guampa@unaffiliated/guampa)
  44. 14:52 damir_: right, I do that with _pcall
  45. 14:52 doub: do you load it from C code of from Lua ?
  46. 14:52 damir_: from c
  47. 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
  48. 14:53 doub: you need to redirect those writes to a table per script between the load and the pcall
  49. 14:54 doub: basically: script = loadfile(filename); globals = setmetatable({}, {__index=_G}); setfenv(script, globals); script(); my_ai_functions[filename] = globals
  50. 14:54 doub: port that to C (ask if you need help), and you should be set
  51. 14:55 Vigud has left IRC (Remote host closed the connection)
  52. 14:55 Vigud has joined (u1143@gateway/web/irccloud.com/x-eeqgzvurbsvtdpjt)
  53. 14:58 jfi has left IRC (Quit: Ex-Chat)
  54. 14:58 jfi has joined (~jfi@vbo91-3-82-243-221-231.fbx.proxad.net)
  55. 14:58 damir_: hmm why is the 'script' variable important to setfenv? It should be always 0 if loading was OK
  56. 14:59 jfi has left IRC (Read error: Connection reset by peer)
  57. 14:59 hoelzro: damir_: loadfile returns the function chunk
  58. 14:59 hoelzro: loadfile ~= luaL_loadfile
  59. 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)
  60. 15:01 VShell: script is the compiled code. calling script() will run that code.
  61. 15:01 lazyden has left IRC (Quit: lazyden)
  62. 15:01 jfi has joined (~jfi@vbo91-3-82-243-221-231.fbx.proxad.net)
  63. 15:02 jsnyder has joined (~jsnyder@c-98-220-236-228.hsd1.il.comcast.net)
  64. 15:04 StrangerDanger has left IRC (Read error: Connection reset by peer)
  65. 15:06 damir_: what's the C counterpart for loadfile?
  66. 15:07 q66 has joined (~quaker66@213.191.105.214)
  67. 15:07 VShell: luaL_loadfile
  68. 15:08 FlipFlop_ has left IRC (Ping timeout: 265 seconds)
  69. 15:08 VShell: or, at a lower level, if you want to provide the source from something other than a file, lua_load
  70. 15:17 damir_: ok, I need to read some material on lua stack, tables etc :)
  71. 15:17 damir_: thanks for now
  72. 15:21 dnyy has left IRC (Ping timeout: 252 seconds)
  73. 15:32 ygrex_at_job has left ()
  74. 15:36 ygrex_at_job has joined (~ygrex_at_@ygrex.ru)
  75. 15:37 Ketho has joined (~Ketho@ip565063bd.direct-adsl.nl)
  76. 15:39 damir_ has left IRC (Remote host closed the connection)
Add Comment
Please, Sign In to add comment