• Advertisement
Sign in to follow this  

Unity How do game engines compile?

This topic is 2551 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I've been trying to deal with this problem for a while now, I'm currently working on a game engine I could use to help myself later on, the thing is made in .NET C++, and is similiar in design plan to RPG Maker, and GameMaker and the such in the way that it handles the creation and handling of maps and events in the UI from .NET.

After the project is done, it is exported to code files in the format of native C++ using SDL.

Now, my problem is this part, I have the code generator mostly ready, it creates all the neccassery files for compilement and works well, now my problem is with actually compiling it automatically from within the GUI in .NET, I was thinking of using the CMD and vcvarsall.bat, but that didn't work, and thought about using makefiles, but that doesn't work on Windows, and I don't want the program to depend on too much external software by using NMake or something.

So, I guess, what I've come here to ask would be what are common methods for this type of thing? How do the popular engines, like the aformentioned ones, or Unity, or UDK, or any of the handful out there, work? How do they compile the work on them into and executable?

Share this post


Link to post
Share on other sites
Advertisement
Unity has an embedded Mono runtime, which JIT-compiles .NET bytecode. The UDK uses UnrealScript, which is compiled to bytecode, and interpreted by Unreal's virtual machine. I'm not really understanding what exactly you have made. A code generator? Please elaborate.

Share this post


Link to post
Share on other sites
Yes, it's a code generator, it takes all the data you input to it when you make the maps, events, and various other data and puts it in C++ format code through the algorithms of the program, is there any way to compile such a thing at run time?

And could I ask for some more information about those 2 methods you mentioned? Are they plausible for hobbyist use? How do you use them? Where can I read more, tutorials peraphs?

Thanks for replying.

Share this post


Link to post
Share on other sites
You can't compile C++ at runtime.

That being said, I have seen the Tiny C Compiler (found here) used to compile C code at runtime and execute it. It's reasonably fast, but still not a popular solution when programming games. I'm still not quite understanding this code generator you have made. Did you write it yourself, or are you referring to the "designer view" in Visual Studio .NET (or something similar)?

Both Unity and the UDK are free to use. You will not be able to take whatever C++ code you're generating and run it in either of these engines. Unity is programmed using C#, JavaScript, or Boo. The UDK is programmed using UnrealScript and Kismet. I would advise you to check these engines out and learn how to use them before you try to write your own engine (if you write an engine at all - you should be writing games instead). Both of these engines have large communities and great resources to help you learn. They are both suitable for commercial and small-scale projects.

Share this post


Link to post
Share on other sites
I'm writing the engine myself, it's using the .NET form project as a GUI, but implements algorithms for outputting .cpp and .h files for every map and event on the map, as well as graphics management and the such, I'm sorry if I haven't made that clear, and it has nothing to do with neigther UDK nor Unity, I was just giving examples as to what I mean in the way I want to compile.

As for TCC, it's nice, but I need a way to compile C++ code, not C, searching around about it suggest that it doesn't support that.

Share this post


Link to post
Share on other sites
You could compile the C++ as a DLL using Visual C++, GCC, or something similar, then link it at runtime. Just know that there are much better and more practical approaches to this. Consider a scripting language like Lua or Python. Or have your engine generate XML or JSON when storing level data - these are much easier to load up and use.

Share this post


Link to post
Share on other sites
Hi KazenoZ,

I'm not sure what you'd call an application which generates the c++ code for a game, but I don't think that "game engine" is entirely fitting - it's more like a "game generator" (although the common code this generated source code relies upon might be called a game engine). I'm also not familiar with any similar products - AFAIK this isn't such a common approach to a game-creation tool.

That said, you have a few options:

  • Write your own c++ compiler
  • Bundle an existing compiler with your product (ensuring licensing and such as all above-board)
  • Require the user to have a compiler already available
  • Scrap the c++ code generation/compilation model - you might generate code in a proprietary domain-specific language and write a VM. You could take an entirely data-driven approach (where a specific game consists entirely of "configuration"/content, and the same "host" application is always used to run the game - if you allow lots of complicated and varied options then this might become practically indistinguishable from creating a language/VM)

Share this post


Link to post
Share on other sites
Thanks alot for the comments guys, especially you, Virus, I've taken fancy to the 4th approach and already started work on it and it seems great so far, perfect for my cause, thanks much for the tip.

Share this post


Link to post
Share on other sites

  • Write your own c++ compiler
  • Bundle an existing compiler with your product (ensuring licensing and such as all above-board)
  • Require the user to have a compiler already available
  • Scrap the c++ code generation/compilation model - you might generate code in a proprietary domain-specific language and write a VM. You could take an entirely data-driven approach (where a specific game consists entirely of "configuration"/content, and the same "host" application is always used to run the game - if you allow lots of complicated and varied options then this might become practically indistinguishable from creating a language/VM)

LLVM is a good option for both ahead of time compilation and JIT compiling as part of a VM and it is distributed under an unrestrictive license. Clang is distributed under the GPL because the frontend uses GCC, but you may be able to bundle it anyway as long as you comply with the GPL (afaik since your code is separate from the GCC bianries you can release your code however you like, but you will want to check this yourself).

Of course, if you are embedding a full blown language (rather than data-driven configuration), unless you want something very specific to your engine, it would usually be better (easier and less buggy) to not write your own language and use an existing embeddable language like Lua (and LuaJIT if you want super fast), AngelScript, Squirrel or even Python. Actually, since you said you are using .NET you have a bunch of options there too. You could embed C# or F# or some other .NET language. Of course, then, as you said, you will need to bundle the compiler and will have to check the terms for the specific language. The engine wouldn't need to bundle these tools to the gamer, but the editor/tools would.

Share this post


Link to post
Share on other sites
Ok, I'm getting a little lost here, could you guys sharpen my terminology?

So, a game ENGINE, is actually not a program you build the game through, but rather a set of functions that are externally linked to your program to make it easier to still WRITE in a regular compiler?

What IS such a program called then? How would you define RPG Maker for example?

And exactly is a VM? A virtual machine, I think? What does it even mean?


I'm sorry, it's just the first time I'm dealing with such a project, I'm not quite keen on the terms...

Share this post


Link to post
Share on other sites
First off I should point out that I agree with dublindan that, if you are more interested in getting results than experimenting with programming languages, generating code in an existing scripting language is probably the most practical approach

Ok, I'm getting a little lost here, could you guys sharpen my terminology here?


So, a game ENGINE, is actually not a program you build the game through, but rather a set of functions that are externally linked to your program to make it easier to still WRITE in a regular compiler?

What IS such a program called then? How would you define RPG Maker for example?



The definition of "game engine" isn't unanimously agreed upon, but the following is my attempt to define it:

A game engine is a collection of software components used in the production of games, which is separate from the content, game-world and gameplay rules that define a particular game. It is reusable, sufficiently generic code which aids in the creation of many different games by exposing an interface to common, often lower-level functionality such as rendering, audio or physics systems.

The line between game and engine is often blurry - an engine could be extremely generic, or targeted at a more specific type of game (it might, for example, include the concept of a "ragdoll", a "weapon" or a "quest" - a more generic game engine might only deal with things as specific as a "sprite", "sound" or "mesh"). A game engine might come with a set of tools designed around it, or might be simply a collection of code.


I would call RPG Maker a "game development tool" or something similar. again, I don't know if there are black and white definitions here.




And exactly is a VM? A virtual machine, I think? What does it even mean?[/quote]


A "machine", in this context, can be thought of as a system for processing a sequence instructions encoded in some specific format. Your PC is a machine, with a CPU processing a list low-level instructions from a well-defined set of possible instructions (e.g. "read a byte from location X, storing it in Y", "add value X to the value in location Y"). A "virtual machine" is a machine running on top of a second machine, e.g. a "simulated" machine running on top of a physical machine. A virtual machine is usually a natively-compiled "host" program which interprets and executes a second program made up of instructions defined by the creator of the VM.

A natively-compiled program is a chunk of low-level code generated (from source code in some language) to run on a specific architecture, made from a specific set of instructions which the architecture supports. This is why different c++ compilers are required for different operating systems/CPUs. A program compiled to run on a VM usually takes the form of "bytecode" (also compiled from source code in some language), which doesnt care about the specific physical architecture it is running on - the VM itself is compiled for various architectures, and executes the correct architecture-specific code "on behalf" of the program running within the VM. The bytecode instructions which the VM recognises are often more high-level, for example "Add X days to date Y" or even something like "Draw image X at location Y" (how high level these instructions are depends on how general-purpose the language supported by the VM is designed to be.

So a VM is effectively a program which "runs" another program inside of itself, by reading an interpreting instructions/commands made up by the developer of the VM.

This could be applied to your project in a couple of ways -

  • You can embed the VM for an existing language like Lua, generating Lua code and handing it off to the Lua VM to execute for you - you provide Lua with "hooks" into your own code to allow it to call functions to draw stuff etc.
  • You dream up your own set of instructions which you think are flexible enough to allow a game to be expressed in terms of. Your front-end tool ultimately generates a set of instructions in the instruction set you have defined - often intermediate representations of the program (or game in your case) are used before it is "compiled" into the final instruction set. You then implement a "back-end" VM which allows such a game to actually be executed by interpreting the instructions and doing whatever is necessary to "execute" each one

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By Manuel Berger
      Hello fellow devs!
      Once again I started working on an 2D adventure game and right now I'm doing the character-movement/animation. I'm not a big math guy and I was happy about my solution, but soon I realized that it's flawed.
      My player has 5 walking-animations, mirrored for the left side: up, upright, right, downright, down. With the atan2 function I get the angle between player and destination. To get an index from 0 to 4, I divide PI by 5 and see how many times it goes into the player-destination angle.

      In Pseudo-Code:
      angle = atan2(destination.x - player.x, destination.y - player.y) //swapped y and x to get mirrored angle around the y axis
      index = (int) (angle / (PI / 5));
      PlayAnimation(index); //0 = up, 1 = up_right, 2 = right, 3 = down_right, 4 = down

      Besides the fact that when angle is equal to PI it produces an index of 5, this works like a charm. Or at least I thought so at first. When I tested it, I realized that the up and down animation is playing more often than the others, which is pretty logical, since they have double the angle.

      What I'm trying to achieve is something like this, but with equal angles, so that up and down has the same range as all other directions.

      I can't get my head around it. Any suggestions? Is the whole approach doomed?

      Thank you in advance for any input!
       
    • By khawk
      Watch the latest from Unity.
       
    • By GytisDev
      Hello,
      without going into any details I am looking for any articles or blogs or advice about city building and RTS games in general. I tried to search for these on my own, but would like to see your input also. I want to make a very simple version of a game like Banished or Kingdoms and Castles,  where I would be able to place like two types of buildings, make farms and cut trees for resources while controlling a single worker. I have some problem understanding how these games works in the back-end: how various data can be stored about the map and objects, how grids works, implementing work system (like a little cube (human) walks to a tree and cuts it) and so on. I am also pretty confident in my programming capabilities for such a game. Sorry if I make any mistakes, English is not my native language.
      Thank you in advance.
    • By Ovicior
      Hey,
      So I'm currently working on a rogue-like top-down game that features melee combat. Getting basic weapon stats like power, weight, and range is not a problem. I am, however, having a problem with coming up with a flexible and dynamic system to allow me to quickly create unique effects for the weapons. I want to essentially create a sort of API that is called when appropriate and gives whatever information is necessary (For example, I could opt to use methods called OnPlayerHit() or IfPlayerBleeding() to implement behavior for each weapon). The issue is, I've never actually made a system as flexible as this.
      My current idea is to make a base abstract weapon class, and then have calls to all the methods when appropriate in there (OnPlayerHit() would be called whenever the player's health is subtracted from, for example). This would involve creating a sub-class for every weapon type and overriding each method to make sure the behavior works appropriately. This does not feel very efficient or clean at all. I was thinking of using interfaces to allow for the implementation of whatever "event" is needed (such as having an interface for OnPlayerAttack(), which would force the creation of a method that is called whenever the player attacks something).
       
      Here's a couple unique weapon ideas I have:
      Explosion sword: Create explosion in attack direction.
      Cold sword: Chance to freeze enemies when they are hit.
      Electric sword: On attack, electricity chains damage to nearby enemies.
       
      I'm basically trying to create a sort of API that'll allow me to easily inherit from a base weapon class and add additional behaviors somehow. One thing to know is that I'm on Unity, and swapping the weapon object's weapon component whenever the weapon changes is not at all a good idea. I need some way to contain all this varying data in one Unity component that can contain a Weapon field to hold all this data. Any ideas?
       
      I'm currently considering having a WeaponController class that can contain a Weapon class, which calls all the methods I use to create unique effects in the weapon (Such as OnPlayerAttack()) when appropriate.
  • Advertisement