Jump to content

  • Log In with Google      Sign In   
  • Create Account

Scourage

Member Since 29 Jan 2000
Offline Last Active Yesterday, 10:22 AM

#5287818 Object reference not set to an instance of an object

Posted by Scourage on 20 April 2016 - 01:24 PM

So the problem is that you are expecting the elements of the array to be there, but they're not.  You're only allocating the space for the elements in your array, you then need to put something there.  

PlanetDetails[] Planet = new PlanetDetails[10];
for(int i=0; i<10;i++)
  Planet[i]=new PlanetDetails();

Cheers, 

 

Bob




#5283221 Engine RPM and wheel angular velocity

Posted by Scourage on 24 March 2016 - 01:29 PM

I would make engine RPM be some function of fuel flow or normalized throttle input.  Maybe something linear where 0 throttle = 500rpm (idle) and a throttle of 1.0 = 5000rpm.  You could then convert RPM to torque using some kind of mapping function, maybe use a datafile that defines the graph so you can have different types of engines.  Here's an example image that I found showing RPM -> torque

 

MU-X+torque_curve.png

 

Once you have a torque value from your rpm value, then you need to apply your "transmission." Based on the current gear, maybe it's in reverse, you get a wheel revolution speed out of it and use that value to move your model.  To implement a clutch, you would have a function before your transmission that could simply either pass through the torque value or pass a 0 when the clutch pedal is pressed.

 

I'm sure I'm missing a bunch of things, but does that help answer your question?

 

cheers, 

 

Bob




#5278178 Embedding Lua in C# with P/Invoke

Posted by Scourage on 25 February 2016 - 03:51 PM

So the problem is that in Lua 5.3 lua_pcall is a macro, not a function.  You need to write a function that calls lua_pcallk (notice the K at the end).  I wrote mine like this: 

 

      [DllImport(LUA_DLL, EntryPoint = "lua_pcallk", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern int lua_pcallk(IntPtr state, int nargs, int nresults, int errfunc, IntPtr ctx, LuaContinuationFunction k);


      public static int lua_pcall(IntPtr state, int nargs, int nresults, int errfunc)
      {
         return lua_pcallk(state, nargs, nresults, errfunc, IntPtr.Zero, null);
      }

There were quite a few changes in the API from 5.1 to 5.3.

 

cheers, 

 

Bob




#5278159 Embedding Lua in C# with P/Invoke

Posted by Scourage on 25 February 2016 - 02:30 PM

I'll fill in what I can.   This may not be the best way to do this (and I'm sure there's bugs), but its been working for me so far.  

 

In order to use p/invoke means you need to have a DLL that exports some functions.  Don't worry about the calling convention, the P/Invoke API lets you set that for your DLL you're calling into.  You also don't really need a def file, building your project as a DLL and having the right preprocessor flags set will export the symbols you want to bind to (at least in the Lua source). To build the Lua library as a DLL you simply need to build the lua library with the flag LUA_BUILD_AS_DLL, which if you look in luaconfig.h (line 235) to cause the symbol LUA_API to be defined as __declspec(dllexport).   I didn't change any code and that is all that you need to do to build lua5.3.dll with its symbols exported.  

 

Once you have a dll with symbols (or feel free to take mine, it's a vanilla 64bit Lua dll), you need to write a C# file that can call into it.  I usually create my dll interface as a static class since each of these functions will be static functions.  I simply went through the public Lua header and then copied the functions I want to be able to call to my C# file as comments so I have them there to reference and then started writing out the p/invoke function definition for each of the C functions. 

 

      #region state manipulation
/*
LUA_API lua_State *(lua_newstate) (lua_Alloc f, void *ud);
LUA_API void       (lua_close) (lua_State *L);
LUA_API lua_State *(lua_newthread) (lua_State *L);
LUA_API lua_CFunction (lua_atpanic) (lua_State *L, lua_CFunction panicf);
*/
      [DllImport(LUA_DLL, EntryPoint = "lua_newstate", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern IntPtr lua_newstate(lua_Alloc f, IntPtr ud);


      [DllImport(LUA_DLL, EntryPoint = "lua_close", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern void lua_close(IntPtr state);


      [DllImport(LUA_DLL, EntryPoint = "lua_newthread", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern IntPtr lua_newthread(IntPtr state);


      [DllImport(LUA_DLL, EntryPoint = "lua_atpanic", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
      public static extern LuaCSFunction lua_atpanic(IntPtr state, LuaCSFunction panicf);


     #endregion

Here you can see each of the static functions bind to a function in the C dll.  The DllImport attribute takes the dll filename, the function name as the entry point, the calling convention (see, no need to change how Lua is built, just change how pinvoke calls it), and a flag to suppress unmanaged security features for performance reasons.  Most basic types are handled automatically.  IntPtr is used for the pointer to the lua_State* since that's an opaque object (we don't access anything inside it ever).  If you need to shuffle complex structures between C and C#, you should look into how marshalling and layout is done on the C# side.  For callbacks, you simply need to define a C# delegate, but give it attributes that allow it to be called from C, and respecting the calling convention of your library.  Here's the delegate definition for a typical function pushed into Lua from C#:

   //Delegate for functions passed to Lua as function pointers using cdecl vs the standard stdcall
   [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   public delegate int LuaCSFunction(IntPtr luaState);

You also need to make sure any delegates you pass into Lua from C# don't get garbage collected.  You do this by keeping a reference to the delegate around, maybe as a member of a class (see how I have myPrintFunction as a member of the LuaState below).

 

The dll filename is where I do something a little different.  Its an idea blatenly stolen from another project (OVR C# bindings I believe).  I preload the dll into the application before pinvoke looks for it.  I can specific the architecture I want (x86 vs x64) by prepending a path to the dll name and then using LoadLibrary to bring that dll into the application space before any p/invoke functions are called. When P/Invoke tries to find the function it looks in the application's loaded dlls first (only by name, not path+name which is why this works), then going to the working directory and looking for dlls with matching names, then if one isn't found there looking in the system path.  Pulling the DLL into the application space first can help prevent the wrong DLL being used (can happen when you have 2 versions of lua in your path, or getting a 32bit DLL  when you need a 64bit DLL).  You don't have to do this, just be sure that the DLL you want is in your application working directory or your path.

 

As I worked through the lua C api, I occasionally came across a macro that acted like a function, so I just implemented it as a function. Such as this:

/*#define luaL_dostring(L, s) \
   (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))
*/


      public static int luaL_dostring(IntPtr L, String s)
      {
         int ret = luaL_loadstring(L, s);
         if (ret != 0)
         {
            return ret;
         }


         return lua_pcall(L, 0, LUA_MULTRET, 0);
      }

Once I got through the API (there were a couple of things I either stubbed out or didn't implement, but not many), I started adding some functionality on top of the raw dll calls.  I created a LuaState object that internally has an IntPtr to a lua_State* object that is created from LuaDLL.  This class provides access functions to get/set values by name/index as well as create tables and references to them. The class also provides some nice debug functions such as printing out the lua stack and printing out a lua table to a C# console.

public class LuaState
   {
      LuaCSFunction myPrintFuction;
      IntPtr myStatePtr;
      LuaObject myGlobalTable;


      public LuaState()
      {
         myStatePtr = LuaDLL.luaL_newstate();
         LuaDLL.luaL_openlibs(myStatePtr);


         LuaDLL.lua_getglobal(myStatePtr, "_G");
         myGlobalTable = new LuaObject(this, -1);


         myPrintFuction = new LuaCSFunction(print);


         LuaDLL.lua_pushcclosure(myStatePtr, myPrintFuction, 0);
         LuaDLL.lua_setglobal(myStatePtr, "print");


         printCallback = new PrintCallback(defaultPrint);
      }


...


}

The LuaObject provides an interface to any lua value whether or not it's a number, string, table, or function.  It allows for iterating over a table, retrieving values or calling lua functions.  It's just a nice abstraction. 

 

Let me know if you have any questions.

 

cheers, 

 

Bob

 



#5277991 Embedding Lua in C# with P/Invoke

Posted by Scourage on 24 February 2016 - 08:38 PM

I wanted the same thing a while ago and rolled my own for my game engine.  If you want, please take a look at my Lua bindings for C# at: https://github.com/bholcomb/Lua-  There is a thin layer on top of the pinvoke bindings, which covers about 99% of the Lua API.  I wrote c# functions to implement the macro's in the lua header as well.  If you only want the "pure" C api, then you should only need what is in the LuaDll.cs file.  The LuaState and LuaObject are part of my thin wrapper. 

 

It has the full lua 5.3.2 source built as part of the project, so you should only need to download, then run the build config (premake) then build the project to produce all the dlls you need.  It is expecting lua5.3.dll in a x86 or x64 folder where you run your executable from.  You can change how this works in the dllLoader class in the luaDll.cs file.

 

There is also a simple test console written in C# that uses the Lua#.dll to verify that it's working.  

 

Please let me know if you find any bugs or have any questions.

 

cheers, 

 

Bob




#5270562 Modern OpenGL Transforms, HOW?

Posted by Scourage on 11 January 2016 - 12:33 PM

You could load up your model and indices into a VBO/IBO and then setup your VAO + shader with them.  You would then create a uniform buffer object that holds the transform matrices  as an array for all your entities and update that every frame.  Then render all the models at once using either instancing or multidraw rendering.  In your shader use the draw_ID to index into the transform matrix array to get that particular model's transform.

 

If you have multiple models, as long as they use the same shader and have the same vertex format, you could store them all into a single VBO/IBO/VAO and use mulitidraw with different offsets.  You would need to look at putting all the textures in a texture array or using direct state access for the texture data.

 

Instead of a uniform buffer, you could use a shader storage buffer if you have a lot of data to store per instance (transform, material ids, etc), then index into it using the draw_ID.  If you go the SSBO route, you may want to look at triple buffering it and persistently mapping it so that you don't stall out rendering trying to update it.

 

cheers,

 

Bob




#5174203 Looking for 'Visual' HTML/PHP editor.

Posted by Scourage on 16 August 2014 - 07:53 PM

I did several heavy web apps (like games) for my day job last year.  The best IDE I found for javascript/html/php/SQL was phpStorm by JetBrains (http://www.jetbrains.com/phpstorm/).  It is able to edit everything, deal with remote servers, databases (including mysql like you're using), integrate with version control systems.  There' s a long list of features.  They also have a webStorm without the php stuff or database stuff.  I believe you can try it out for 30 days and a buy it for $99 as an individual. 

 

cheers, 

 

Bob




#5146806 Tips for Programming a GUI

Posted by Scourage on 13 April 2014 - 09:43 PM

You should also look at the "Immediate Mode" GUI talk on Molly rocket (https://mollyrocket.com/861). It's a slightly different way of thinking about GUI development. In my opinion it's easier to use as an end user, but not necessarily easier to build.

It's great for simple in game menus, but apparently can be used In fairly complex systems. Unity uses an IMGUI in their editor.

Cheers,

Bob


#5144111 How to communicate between c++ and c#?

Posted by Scourage on 03 April 2014 - 08:59 AM

Binding C# to C++ is hard and painful.  I recommend not doing it (I will give SWIG another look).  

 

I recently needed to bring a rather large c++ API into Unity (C#).  To do this I built a very minimalist C API (just 4 functions: init, shutdown, send message, tick), which I use P/Invoke to call.  I then pass messages back and forth between managed and unmanaged code using this API.  Messages are code generated classes (from a simple message definition) that serialize to buffers and then passed through the sendMessage function to the other side.  The code generator actually generates C++ and C# versions of the class so they know how to serialize/deserialize on either side.  There's definitely a performance hit for this approach, but it is the cleanest way I came up with that still allows me to expand what is passed between managed/unmanaged code with a minimal amount of work.

 

You might also want to consider doing something similar but with TCP sockets.  I've sat in a couple of sessions at GDC recently where people are building remote debug/edit capabilities into their game engines by passing messages over a socket, even if it's all within the same application.  

 

Cheers, 

 

Bob




#5142725 Exporting "to WebGL"!?

Posted by Scourage on 27 March 2014 - 06:32 PM

You may want to look at glTF, it's a runtime format from Khronos.  It's designed originally for webGL.  Currently the way to get models in the format is to convert from Collada using their tool, but I'm working on a Blender import/export for it.

 

cheers, 

 

Bob




#5141591 Multithreaded drawing and logic

Posted by Scourage on 23 March 2014 - 08:44 PM

Another option is to change your threading model. You could run your update phase in a multithreaded fashion, using worker threads to update multiple groups of entities at the same time. Then if it's time to render the scene, create all your draw calls (also could be done in a bunch of worker treads) and send them off to the gpu.

Cheers,

Bob


#5138790 C++ or C# For Game engine with OpenGL and SlimDX

Posted by Scourage on 13 March 2014 - 06:43 PM

I think C# is a fine choice, especially for learning how a game engine works. OpenTK is a great library to use to get started.  It has openGL/AL/CL bindings + all the utility, input, math classes you can shake a stick at.  My game engine uses an OpenGL 4.3 context just fine with OpenTK and I haven't run into any limitations, aside from my own capability.

 

I've integrated Lua into my C# game engine and if you're looking at other languages, There's the iron languages (IronPython, IronScheme, etc) which integrate really nicely into C# since they're all managed languages using the same runtime.  

 

Cheers, 

 

Bob




#5135032 Fastest map-like collection for <GUID, BaseClass*> lookup

Posted by Scourage on 27 February 2014 - 07:49 AM

I've found 64bit unsigned ints are more than enough for any GUID I've had.  I tend to use them as bit fields as well to help identify type and which machine they come from, so I could imagine I could get away with 32 bits in a simpler game.  The nice thing is that the comparison is simply an integer comparison, which is really fast.

 

As for quick look-up, you need to think about how many objects you have total.  If you have less than 100 objects, a simple array of objects may actually be faster than using std::map (which if I recall, is a red-black tree data structure).  However, if you have many objects (10000-1000000) you may need to test which is better, a std::map or some kind of hash table.

 

Cheers,

 

Bob 




#5133577 How to "reset" a singleton?

Posted by Scourage on 22 February 2014 - 09:36 AM

I have to agree with Danicco, this does not sound like a good use case for Singletons.  Maybe your gamestate manager might be a singleton, but the individual game states probably shouldn't be.  

 

Your gamestates probably should inherit an abstract base class with the following functions (pseudo code follows)

class AbstractGameState
{
  void onEnter() =0;
  void onTick(Time dt)=0;
  void onExit()=0;
};

Now your derived classes implement these functions and as you transition between states, you call onExit() of the state you're leaving and then onEnter() on the state you're going in to.  OnExit() will give your states the opportunity to either save state for when you come back to that particular state, if thats what you want. OnEnter() will give your states the opportunity to either reload a saved state, or reset the state, again depending on what you want.

 

Inside your game state manager, you can have a transition function to manage this for you:

void transition(AbstractGameState* toState)
{
   myCurrentState->onExit();
   toState->onEnter();
   myCurrentState=toState;
}

Cheers, 

 

Bob




#5132978 What can you use C# for?

Posted by Scourage on 20 February 2014 - 10:21 AM

I'm a fan of C#.  It's cross platform, compiles very fast (compared to c++) and you can get more done with less lines of code than C++.  In those regards, it's a more "productive" language.  I use it for all my prototyping, and sometimes the prototype is the final project since it performs well enough.  I thought it was pretty easy to learn, and is more than capable for any number of projects from Game editors, Rendering engines, to simulation backends.

 

The OpenTK library provides bindings for openGL/AL/CL as well as all the math primitives (vector/matrix) that you could need.  It also provides input and windowing API's as well.  

 

Xamarian and Unity both use C# and are able to target mobile platforms.  Unity can target browsers and consoles as well.  If you're looking for a pure browser based game, then C# may not be the right tool.

 

cheers, 

 

Bob






PARTNERS