Jump to content

Calling C++ functions from Sandbox LUA script


Chris Paulson
 Share

Recommended Posts

Guest Red Ocktober

i was just searching for that Rick... i remembered you had done some work along these lines a lil ways back... ;)

 

this might work here...

 

 

Chris... is Recast your work...

 

 

--Mike

Link to comment
Share on other sites

i was just searching for that Rick... i remembered you had done some work along these lines a lil ways back... :D

 

this might work here...

 

 

Chris... is Recast your work...

 

 

--Mike

 

Sorry for the delay, I didn't have access to the this forum. Recast is NOT my work, I've intergrated the library with Leadwerks. Recast is the work of chap who did the AI for crysis, so fairly good pedagree.

Link to comment
Share on other sites

Chris is Recast your handy work?

 

I've been writing my own navigation mesh generator that runs in realtime, but its a long way behind the quality and speed of recast at the moment!

 

Word of warning, I spent six months working on my navmesh generation before I chucked my stuff for recast. You might be more successful as your probably brighter (thats not hard), however recast is done and working...

 

I'll share all my C++ code happily.

Link to comment
Share on other sites

Is that code meant for realtime use, or is it for precalculating navigation waypoints? It's very interesting.

 

Both.

 

Have you worked much with this system?

 

 

I have done a lot with it. I’ve got it to process levels created from the sandbox and I have used to for my NPCs to move around, going from A to B. The path finding is really fast, enough to handle many requests per frame. The generation of the static mesh can take a while, for example the island level as I remember took about 10 minutes. However when I wrote my own navmesh stuff it took in excess of 24hours to do the island level. For a level the size of the island it would take a lot longer to do manually lacing AI nodes I guess.

 

The generation can be refined with different config such as voxel size, slope angle, agent size etc. A Larger voxel size would speed the mesh generation.

 

Generation of a tile using the tile mesh method depends on the size of tile, you could probably only get away with doing 1 -2 tiles per frame. I guess this where threading would be really useful.

 

 

I’ll try and do a video of it working, however my AI stuff is still very flawed so I wasn’t proud enough of it yet to show it off. I’ll also publish all my code for anybody who wants to use it.

Link to comment
Share on other sites

Here is the post on the old forum:

 

 

Here is an example of how to get your own C++ code from a DLL for use in lua which can be called from the new editor.

 

1) Create a DLL project in VS. What you can do is create a new console app and during the wizard select the DLL option along with Empty Project.

 

2) In this project you'll want to add the lua include directory and the lua libs for linking.

 

3) Use this code as a test. Note that the prototypes of the one main method that loads the other methods should be inside extern "C". If it's not your function names will be mangled. The general ideas is that you expose 1 function that will load a table of all the other functions in your DLL. This makes it so you can access all the methods in lua.

 

#define LUA_API_EXP __declspec(dllexport)

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

  // prevents the function name from being mangled
  LUA_API_EXP int luaopen_test (lua_State *L);
}


#define PI (3.14159265358979323846)

static int miles_to_km (lua_State *L)
 {
 double miles = luaL_checknumber (L, 1);
 double km = miles * 1.609;
 lua_pushnumber (L, km);
 return 1;   /* one result */
 } /* end of miles_to_km */

static int circle_calcs (lua_State *L)
 {
 double radius = luaL_checknumber (L, 1);
 double circumference = radius * 2 * PI;
 double area = PI * radius * radius;
 lua_pushnumber (L, circumference);
 lua_pushnumber (L, area);
 return 2;   /* one result */
 } /* end of miles_to_km */

static const luaL_reg testlib[] =
{
 {"miles_to_km", miles_to_km},
 {"circle_calcs", circle_calcs},
 {NULL, NULL}
};


/*
** Open test library
*/
LUA_API_EXP int luaopen_test (lua_State *L)
{
 luaL_openlib(L, "test", testlib, 0);
 return 1;
}


 

4. Now in lua, you load the DLL saying you want to start with the luaopen_test() method. Note that I made root a global variable that points to the directory of my game exe. So in this case the dll needs to be in the same directory as my exe.

 

assert(package.loadlib(root.."Test.dll", "luaopen_test"))()
-- if you don't use extern C your function name would end up looking something like
--assert(package.loadlib(root.."Test.dll", "?luaopen_test@@YAHPAUlua_State@@@Z"))()

 

I was able to get the mangled function name from dependency walker. A program that lets you see the inside of DLLs.

 

5. Now that the dll is loaded you can use the methods in it like so:

 

print(test.circle_calcs(15))

 

So you might ask why you want to do this? Well, let's say you want to create some AI code that is speed sensitive. You don't want to do that in lua because it'll be way slower than if you did it in C++. What you want to do is the majority of the code in C++ and expose some functionality to act on the C++ to lua. So you create a DLL with all the C++ code to do the AI speed sensitive code.

 

The other cool thing about this is that Josh had a vision of packaging models with lua scripts that act on those models. This fits right into that also. You can package your dll along with your model and lua script so others can use it. So basically your DLL will be a library that your lua script uses functions from to act on the models.

 

Note: This is all going off the assumption that Josh hasn't already created a way for us to use our own C++ functions from within the editor.

Link to comment
Share on other sites

Sorry for the delay, I didn't have access to the this forum. Recast is NOT my work, I've intergrated the library with Leadwerks. Recast is the work of chap who did the AI for crysis, so fairly good pedagree.

 

This game?: ;)

 

 

Sorry, couldn't resist. This looks really interesting. Maybe I can integrate it into the engine.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I've seen Chris's work first hand and it's pretty impressive. Recast is probably as good as anything else out there including what's currently available in the big AAA design houses. Chris has not only taken this and integrated it with Leadwerks but also integrated Open Steer and the AI++ lib to give a more comprehensive AI solution rather than just straight path finding. To be honest his own navmesh solution was pretty good too! I believe people have already done integrations of Recast for OGRE and UNITY and probably other engines too.

Intel Core i5 2.66 GHz, Asus P7P55D, 8Gb DDR3 RAM, GTX460 1Gb DDR5, Windows 7 (x64), LE Editor, GMax, 3DWS, UU3D Pro, Texture Maker Pro, Shader Map Pro. Development language: C/C++

Link to comment
Share on other sites

This game?: :)

 

 

Sorry, couldn't resist. This looks really interesting. Maybe I can integrate it into the engine.

 

haha, nice video. Josh I think that is great idea - The software license certainly permits it. It would be a fantastic addition to the engine.

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

Link to comment
Share on other sites

I've seen Chris's work first hand and it's pretty impressive. Recast is probably as good as anything else out there including what's currently available in the big AAA design houses. Chris has not only taken this and integrated it with Leadwerks but also integrated Open Steer and the AI++ lib to give a more comprehensive AI solution rather than just straight path finding. To be honest his own navmesh solution was pretty good too! I believe people have already done integrations of Recast for OGRE and UNITY and probably other engines too.

 

Wow nice. Chris you should chuck up a bit of a blog entry on what you've done thus far - It sounds very interesting!

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

Link to comment
Share on other sites

He is hiding the fact that Crysis 2 was infact made using Leadwerks 2.28, and that CryEngine 3 is really Leadwerks Engine 2.3.

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

This game?: :(

 

 

Sorry, couldn't resist. This looks really interesting. Maybe I can integrate it into the engine.

 

That would be brill!

 

It took me quite a long time, but only because I'm not very bright and I do all this between my wife nagging and finding me DIY to do.

 

Every game AI has it failure point see: -

 

http://aigamedev.com/open/articles/bugs-caught-on-tape/

 

 

For what it's worth I'll help anyway I can, but that would be like me trying to give Stephen Hawkins tips on black holes.

Link to comment
Share on other sites

I'll put in a zip file all my code so far and put on my blog. I just need to tidy the VC++ project a little to strip out the third party libraries into LIB files.

 

It's a bit WIP rats nest a the moment so I may have give lots of explanations.

 

I did attempt to get recast to compile from with blitzmax but failed, that's why I moved onto C++. However I now realise I could have just compiled recast with VC++ into a LIB file and than use it from within blitzmax. I believe the failure was probably to do with compiler differences in the minGW and how into inits variables.

Link to comment
Share on other sites

Brucey on the BlitzMax forum would be the best person to talk to.

 

I think compiling in BMX would be best because then I could add this functionality into the editor. I don't know enough about how it works yet, so your experimentations are really helpful.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...