Jump to content

Understanding role of C++


Recommended Posts

After 2 years learning and using leadwerks, I decided to refresh with C++. But I still feel unclear about the role of C++ and need to be explained (even I watched Agro's video about Lua and C++)

When I build my game in VS2017 and run it for testing, it does exact the same as running game from Editor with pure Lua script (F6) or running from published .exe.
I don't comprehen the way C++ cooperate with Lua. I want to know more detail about the relationship between Lua and C++. If I write a C++ file and how does that file run in my game.

I am newbie to C++ and there are no many up to date tutorial out there.

Thanks :D

Link to comment
Share on other sites

The executable in your game, the exe file, is built from the C++ source code found in "My Documents/Leadwerks/Projects/<My Project>/Source/". As you know in C++ you have both header files, ".h", and cpp files, ".cpp".

Header files are generally for declarations. (They are where you tell the compiler what functions, classes, methods you are defining, but generally do not assign them a body.)

C++ files are where you either write "static", or local code to the cpp file.... and/or more commonly. To use these functions, classes, and methods that you declared in your header file in some C++ file you must first: (The static keyword has different implications when used in class declarations, a tutorial can explain this just don't be confused if you come across it.)

#include "MyHeaderFile.h"

Be careful when you're including header files in header files, as sometimes this is good practice (I'm not going to go into detail), often times it can lead to circular dependencies.

Again, I'm not going to go into detail on this but look into forward declarations, and when they're allowed.

 

Long story short, the next time you build your project in Visual Studio, or whatever IDE you're using, assuming you've actually used the code you've added somewhere and brought the file into source control the next time you build your program it will be compiled into the executable.

Confused where to start? main.cpp contains the programs entry point, but Josh has projects setup so App.h/App.cpp provide you with a very straight forward entry point for your game. I would recommend looking into some C++ tutorials because at first writing it can be frustrating, but once you learn about all the tools the language provides it is one of the most powerful languages out there. :)

Interested in communications between C++ and Lua? check out the Lua C api for tutorials on how to use the stack to communicate between languages, it can be confusing at first but I assure you it's quite simple. Shoot me a PM, or add me on Steam if you need a hand or some examples with this. I also made an interesting tool that can help you take advantage of ToLua++ to automatically expose your C++ classes, variables, and functions to Lua. Add me on Steam if you'd like help setting up this program.

I'm sorry this post isn't very descriptive, I wanted to try to explain the process and the most common "gotchas" simply without getting into a potentially confusing discussion bout the language itself.

Some debugging tips for those pesky linker errors.

1. Check for any declarations in header files that lack definitions. (Visual studio's intelli-sense will underline these declarations in green.)

    - A call to an undefined function will produce a linker error, though inteli-sense is usually good at telling you exactly what is wrong in this case.

2. Check for circular dependencies.

3. If you're using 3rd party libraries, check if you forgot to add the "lib" file to your project's "Linker--> Input" box in "Project Settings".

Getting an error more-or-less explaining that a symbol is already defined in <>.obj?

- Look into include guards!

  • Like 1
  • Thanks 2
  • Upvote 1
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...