"C" code in BGE

I have not found anything on the possibility of using “C” in the GE, can someone point me in the right direction?..if there even is a possibility.(currently)

Currently, you can only use Python in the GE. I don’t think there are any plans to change that in the future. However, if you know C already, you should find Python very straightforward.

If you know C well enough, you could go bumping around into the source code, but that’s real complicated and i can’t really give you any suggestions for that.

Couldn’t agree more. When I first saw that the BGE was based on Python, I said something like “Awww… why not C?”. But after checking some tutorials, find out that it’s mostly the same, just interpreted and with a couple of additional features.

I was under the impression that there were ways to call C code from python?

Try googling it.

Is it possible to call compiled Python modules from the game engine? Those .pyc files should run faster, right? I’ve been wondering this for a while.

Edit:
@JustinBarrett: Maybe ctypes is what you’re looking for?

ctypes is an advanced ffi (Foreign Function Interface) package for Python 2.3 and higher. In Python 2.5 it is already included.

hacking GE source isn’t as complicated as it seems to be :wink:

Some documentation on extending GameEngine in C++ and writing Python Wrapper
just my experience written down.
http://bitbucket.org/moerdn/blender_wiiusewiimote/wiki/Home

Here a short tutorial on how to write a Python Wrapper Interface
http://www.vimeo.com/4746906

greetings, moerdn

it still gets me a bit confused is the reason… oddly enough it seems easier, and that kills me…???I’m used to defining my variables, as in telling them they are int, long, float…char whatever…I have not done much research(I will now I guess)…from what I’ve seen(briefly)you just say here is the name, and here is the initial value…like…

credits = 0;
…although much simpler things like this happening over and over confuse me…
like…
#include<>…
because this “#” is for commenting…lol anyway…I will go and check some stuff out, I’m looking for very specific things so it should not be super difficult…one more question(silly one)python does suppord multi dimensional arrays right?
Does it use structs…or something similar?
ok, that was 2…thanks guys…

-Nowherebrain/Justin

Yeah, Python supports multidimensional arrays. You can declare filled arrays, or empty ones, and you can add and remove entries using append() and del. Here’s an example:

my_array = []
the_answer = 42
the_question = "unknown"
my_array.append(the_question) 
my_array.append(the_answer)
print my_array

# output: ['unkown', 42]

del my_array[1]
print my_array

# output: ['unknown']

ultimate_question = ['life', 'the universe', 'everything']
my_array[0] = ultimate_question
my_array.append(the_answer)
print my_array

# output: [['life', 'the universe', 'everything'], 42]

Alternately, you can use dictionaries to refer to values in Python. You can even nest arrays inside of dictionaries:

my_dictionary = {}  # curly braces
my_dictionary['utlimate_question'] = ['life', 'the universe', 'everything']
my_dictionary['the_answer'] = 42
print my_dictionary

# output: {'the_answer': 42, 'utlimate_question': ['life', 'the universe', 'everything']}

I don’t know if there is an equivalent of struct in Python.

Edit: BTW, I forgot to mention that Sam was right. You can import pre-compiled C libraries using Python. You need to follow some special rules to make your code Python friendly, though. Also, the Blender Foundation seems to be of the opinion that importing libraries this way could cause your .blend to come under the GPL, so you’d need to be careful how you go about it. Anyway, if you want to pursue that, here are some links:
http://docs.python.org/c-api/
http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/
http://www.blender.org/education-help/faq/gpl-for-artists/#c2129

Justin, if it’s only a problem of not being used to the language then for all means, learn python, it’s worth your time. Which is pretty fast. Once you know one programming language, learning another is much much easier. Python is based on C, many things there will remind you of C. So if you do know C very well it should only take you a couple of hours to learn python. Get started at:
http://docs.python.org/tutorial/

That’s the official tutorial, it’s very easy to follow, specially if you’re a C programmer. He often compares C to python in that tutorial so you can understand the difference. For example, he even talks about your question of how to use C structs in python, for which he recommends using python Classes with only variables but no functions.

Once you learn python, it’s much faster to code in than in C. And the biggest advantage is that it’s an interpreted language, not a compiled language. Meaning you can make changes in real time and check the results. Which makes debugging much more efficient and fast. So learn python, it’s worth it.

The big advantage of C over python are running speed and memory usage. And if that’s what you’re concerned with, then you can just check which parts of your python code are slower and write those as C libraries that you’d access in python. Like others already showed, python has a specific prebuilt module only for handling C libraries, which makes it very easy to use C code in python.

@FunkyWyrm:
.pyc files are automatically created and run from on the BGE. So you’re already using them when you use the BGE. But they don’t really “run” faster than .py files. They only load faster to memory, but once loaded, they do the same thing.

Ah. Now that makes sense. I never read all the way through the official tutorial. Guess that’s why I didn’t know that.

Why why why would you want to use C in blender? I can understand not wanting to take the time to learn a new language. But the thing is if you already know C then if you just start learning python now you’ll already be coding some pretty complex stuff by tomorrow. Python is alot like C only with as much BS cut out as possible.

If you have a python program and a C program that both do the same thing, odds are the python program will be like 1/3 of the length, if that. It also probably takes a lot less time to debug python scripts since the language is so much more straightforward than C.

While python has a faster development time. C has a faster run time. There are areas of game code that can slow your game and could be optimized. Usually those are physics and AI. Fortunately we already have a physics library written in C available to use in the BGE. So we’re left only with AI brute force code such as search algorithms.

It’s very understandable that someone would like to write that part of the code in C instead of python. Tho I wouldn’t recommend doing it for your entire code.

If you’re using C because you’re serious about speed, why use BGE? I mean I know it’s very capable, but I doubt it can compete with something like OGRE/CrysalSpace/various open-sourced iD Software engines… [edit] in terms of raw speed/performance[/edit]

Because as it was already said 83 times in this thread, you don’t need to write your whole code on one or another. You can write most of your code in python which is fast to develop and debug. And then you can write specific slow areas in C to improve performance, if needed.

@JustinBarrett, reading over replies - did anyone say this?

  • Write code in C, add calls to it from python as a part of the GameLogic API.
  • Write code in C, add it as a logic brick or an option on an existing logic brick\
  • Write code in C, and have it as an external module.

I say C but I mean C/C++, when I do C/Python BGE-API dev in C++ it ends up being much the same as in C since python doesnt make use of any C++ classes.
For example some of the BGE API functions would compile in C too.

Look in KX_PythonInit.cpp