Jump to content
Aaron Symons

More Precise Timer

Recommended Posts

Hello there!

 

I require a more precise timer that deals with millionths of a second, or more. I've played around with default Leadwerks Time commands, but the most precise I can get is a thousandth of a second, eg:

 


Time:GetCurrent() / 1000

-- 0.654

 

Does anyone know a way in which I can handle millionths or billionths of a second with Leadwerks and Lua?

Share this post


Link to post

You would probably have to do something in C++ and expose it. You'd be limited to what the OS can handle. Can I ask why you need such precision?

Share this post


Link to post

Hi Rick,

 

I thought I'd have to go over to C++. Even though I don't have the C++ version of Leadwerks, would I be able to reference my own C++ code via Lua somehow, or would that be naughty?

 

I'm wanting to make things happen in-game to the BPM of the played music. I have all the calculations ready, but I need to be more precise with the timing because, currently, I'm out of time with the music by quite a bit. I also have other ideas for a BPM-based timer that I'd love to implement.

Share this post


Link to post

You can always write a LUA plugin (DLL) in C. I did one my self and it works just fine. Remember to switch off LUA sand-boxing when "requiring" a module.

Share this post


Link to post

@Olby I'll look into that if nothing else. Thanks.

 

@Rick I'll definitely look into that, thank you!

 

I'll update this topic with my findings when I get the chance. smile.png

Share this post


Link to post

You can always write a LUA plugin (DLL) in C. I did one my self and it works just fine. Remember to switch off LUA sand-boxing when "requiring" a module.

 

I'm interested in trying out creating a DLL module for Lua. Do you have any pointers or tips? smile.png

 

I'm guessing I'll have to download Lua source in order to get the header files. Looks like I'll have to install mingw as well. I'm sure this is all basic stuff, but I've never dabbled in creating a DLL file before.

Share this post


Link to post

You don't need mingw. Make a DLL project in Visual Studio, include the newest Lua (Lua-JIT maybe) source, and have at it. Note that this will be specific to Windows though. If you want it cross platform you'll need a different compiler (I think mingw is windows only too). Maybe gcc.

Share this post


Link to post

Hey there,

 

I've had some issues with creating my DLL. Namely, the "LNK2019" issue. For some reason lua_pushnumber() and lua_tonumber() are an "unresolved external symbol". I've had a read online, but I'm just getting back into C/C++ and most of my knowledge of it has gone! xD

 

I've "installed" Lua into the Visual Studio project via NuGet.

 

My "simpleDLL.h":

extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}

namespace myNameSpace
{
 class MyClass
 {
	 public:
		 static __declspec(dllexport) int isquare(lua_State *L);
 };
}

 

My "simpleDLL.cpp":

#include "simpleDLL.h"

namespace myNameSpace
{
 int MyClass::isquare(lua_State *L)
 {
	 float rtrn = lua_lua_tonumber(L, -1);
	 printf("Top of square(), nbr = %f\n", rtrn);
	 lua_pushnumber(L, rtrn * rtrn);
	 return 1;
 }
}

 

I'm just aiming to create a very simple DLL at the moment to get the hang of how they work. Any help will be appreciated!

Share this post


Link to post

This installation of Lua, did it include the entire source or just the lib or lib/dll?

Share this post


Link to post

You have to link against the Lua included with Leadwerks

 

What do you mean by this? Do you mean just the same version or are you talking about exposing this stuff in an LE C++ project? Because you don't have to include your stuff inside the LE C++ project. You can make your entirely separate DLL that has nothing to do with LE at all. As long as you are using the same Lua version as LE (which you can just download from the lua website. don't know about using nuget with C++ for lua. I just downloaded the lua source and put it into my C++ DLL project.) you can load your separate DLL in from a Lua file in an LE project. I've done this before when I made a joystick DLL for someone and exposing networking via RakNet for myself.

Share this post


Link to post

This installation of Lua, did it include the entire source or just the lib or lib/dll?

 

As far as I can see, NuGet installed both lib and dll files for Lua. NuGet is a tool in Visual Studio 2013 Express. You just type into the NuGet console:

 

 Install-Package "Lua" 

 

and it finds the files from some online repository (I guess) and downloads them for use in the current project. smile.png

Share this post


Link to post

Yeah, I've used NuGet in VS with .NET stuff but never in C++. Personally I think you should download the Lua source and just include it into your project. It's only like 2-3 files. You might need to get LuaJIT as I believe LE uses that.

Share this post


Link to post

What do you mean by this? Do you mean just the same version or are you talking about exposing this stuff in an LE C++ project? Because you don't have to include your stuff inside the LE C++ project. You can make your entirely separate DLL that has nothing to do with LE at all. As long as you are using the same Lua version as LE (which you can just download from the lua website. don't know about using nuget with C++ for lua. I just downloaded the lua source and put it into my C++ DLL project.) you can load your separate DLL in from a Lua file in an LE project. I've done this before when I made a joystick DLL for someone and exposing networking via RakNet for myself.

 

*shrug* I could only ever make it work by linking to the specific files included inside the leadworks dir

Share this post


Link to post

*shrug* I could only ever make it work by linking to the specific files included inside the leadworks dir

 

Did you try downloading LuaJit or just plain Lua? I seem to recall plain Lua didn't work, but LuaJit did as I believe the state is different between the 2.

 

Either way it sounds like you meant just using the same files and not requiring an LE project so yeah, same same there. I just wasn't sure what you were meaning by your fist sentence.

 

 

OP Josh seems to include the source as well, so you can either include lib or source: E:\Steam\steamapps\common\Leadwerks Indie Edition (this is my path via steam but you can find yours). There is an include and library folder that has the libs/sources for the Lua library.

Share this post


Link to post

Yeah, I've used NuGet in VS with .NET stuff but never in C++. Personally I think you should download the Lua source and just include it into your project. It's only like 2-3 files. You might need to get LuaJIT as I believe LE uses that.

 

Yeah. I think I'm missing something here, because I definitely can't seem to use the lua_pushnumber method, and other lua_ prepended methods.

 

Where do you download your Lua source? On the Lua.org website, I only see downloads available that will give me more than the 2-3 files you mention.

Share this post


Link to post

It might be more than a few. I don't recall. Download this http://luajit.org/download/LuaJIT-2.0.3.zip and just copy them to your VS project dir. Then go into VS and include all these existing files into your project. It should work then.

 

Include all header files, or just the three I include already? I'm still having difficulties using the lua_ methods. I must be missing something!

 

If I redefine them in my DLL project, I get an error stating as such. I don't want to redefine them, but that's one of the "solutions" I've tried.

 

I'm really not sure where to go from here. It's obviously doable, as many people have created DLL files for Lua, but I can't seem to get things working.

 

It could be:

  • The setup of my VS project, including;
  • The compiler used in my VS project isn't suitable
  • Missing included files
  • Missing definitions

 

I don't know, and I'm sure the list could go on! lol

Share this post


Link to post
Include all header files, or just the three I include already? I'm still having difficulties using the lua_ methods. I must be missing something!

 

In C++ there are a couple options to including a library. You can include a .lib file and the headers only OR you can include all source if it's available to you. That's cpp and h files. I suggested including all the source (.h & .cpp) because it's easier for someone new than going into the config properties and adding .lib file (I think anyway). Did you copy all source and header files to your local project AND add all of those files to your project?

Share this post


Link to post

In C++ there are a couple options to including a library. You can include a .lib file and the headers only OR you can include all source if it's available to you. That's cpp and h files. I suggested including all the source (.h & .cpp) because it's easier for someone new than going into the config properties and adding .lib file (I think anyway). Did you copy all source and header files to your local project AND add all of those files to your project?

 

I see now.

 

I just copied all the downloaded files to my project folder and then I just #include the three header files I stated in the code snippet, pointing to the downloaded ones, of course, like:

 

// Path to downloaded header
#include "C:\path\lua.h"

 

So you're saying I should include all Lua / LuaJIT files into my project as if I created them: so they show in the "solution explorer" in VS, then include the three headers I included in my snippet?

Share this post


Link to post
o you're saying I should include all Lua / LuaJIT files into my project as if I created them: so they show in the "solution explorer" in VS, then include the three headers I included in my snippet?

 

Yes. Those functions are actually defined in the .cpp files which is why you were getting those errors, because you didn't have them included. If you include all the headers and source to your project folder then you don't do C:\ anything in your include. If you type #include you should see them. It'll be relative to your project.

Share this post


Link to post

I've been following this thread, but I'm getting a bit confused now. Are you trying to include a c library in your lua project to use from lua. Or are you trying to expose a c function to lua to be used from a lua script?

 

If its the first look into "luajit ffi" ffi is the key as it allows you to directly load c code into luajit. The other way I can't help you.

Share this post


Link to post

You might be able to use http://luajit.org/ext_ffi_api.html (C directly in Lua). Do a search on these forums as I think there was 1 guy doing this already.

 

I did point him to trying that, but the other option is making a DLL project and loading that DLL in Lua, so that's what he's trying to do now.

Share this post


Link to post

Hi,

 

Got my DLL compiled with no warnings or errors. Woop!

 

@Rick, I did as you suggested and included all header and source files into the project, as well as including the LIB files. smile.png

 

The only thing now is: where do I put the DLL in my Leadwerks project (if it matters), and how do I require() it from within Leadwerks? lol

 

At least I have my DLL now! biggrin.png

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...