PDA

View Full Version : Scripting language for C game


Ummon
2007.01.22, 10:37 PM
Is there a way to easily combine a scripting language with C? I want to be able to call C functions that I right thru the scripting language and vice versa. Is it even possible to call your own C functions from a scripting language?

OneSadCookie
2007.01.22, 10:49 PM
Yes . . .

AnotherJake
2007.01.22, 11:10 PM
. . . That's what scripting languages are all about. I learned how to do it initially from a book called Game Scripting Mastery (not Mac-oriented, but still applicable, and all C). This subject has been beat up plenty already. Search the forums for more info on how to do it.

unknown
2007.01.23, 04:52 AM
Im not sure why those two are trying to make you feel like youve asked a silly question, but take it from me you havent. Doing that kind of thing can really improve the efficency of writing a game (think compile & test vs quit & test ;) )

This is a great language for exactly that purpose
http://www.lua.org/

Its really easy to get set up with, and you can integrate it with your game engine for speed & efficiency.

This game I wrote
http://fax.twilightcoders.net/N0x35C49E/
the engine is C and the game logic is lua, it worked out really well. (and the source code is available as well)

igame3d
2007.01.23, 06:18 AM
We use C with Lua too.
Have a peak at the innards of this _ iFly_Universal012207app.zip (http://www.igame3d.com/DL/iFly_Universal012207app.zip)

Somewhere on lua.org is a list of products using it, astounding.

Did I see that 70% of Adobe Lightbox is coded in LUA? I think so.

djork
2007.01.23, 09:15 AM
I third a vote for Lua.

I think the combination of Lua and C is something that is better than C++ for making games. C gives you the flexibility to tie in with practically any OS and call OpenGL natively, as well as use libraries like libpng and OpenAL. It's also fast as heck. All of the logic in Psychonauts was done in Lua. Only the rendering/sound engine was done in C++.

I <3 Lua.

I have successfully replicated the basic functionality of Processing with C and Lua as of yesterday (setup, draw, keydown, stroke, fill, line, rect, beginShape, etc.). It's good stuff! I've got some great ideas with Lua that I think could work really well.

Joseph Duchesne
2007.01.23, 03:56 PM
I have to say, I'm most happy with C and Lua. I had full lua support in my game inside of 4 hours. It's flexible, powerful, fast and easy to pick up. Give it a shot ;) It's worth the money :P *




*it's free

GolfHacker
2007.01.24, 10:26 AM
C gives you the flexibility to tie in with practically any OS and call OpenGL natively, as well as use libraries like libpng and OpenAL. It's also fast as heck.

C++ works with every OS that C does.

And I'm not sure what you mean by "natively". A C++ program can use OpenGL, OpenAL, or any other C library without any problems at all. There's no performance penalty for calling C functions from C++ programs. In fact, the resulting machine code is the same regardless of whether you're you're writing C or C++.

As far as speed goes, modern C++ compilers generate very efficient machine code, so there really isn't any difference in speed between C or C++. Granted, an object-oriented C++ program can be slow if it isn't well written, but I've seen many badly written C programs that are slow. It all comes down to knowing what you're doing; it has nothing to do with C versus C++.

djork
2007.01.24, 10:45 AM
C++ works with every OS that C does.

And I'm not sure what you mean by "natively". A C++ program can use OpenGL, OpenAL, or any other C library without any problems at all. There's no performance penalty for calling C functions from C++ programs. In fact, the resulting machine code is the same regardless of whether you're you're writing C or C++.

As far as speed goes, modern C++ compilers generate very efficient machine code, so there really isn't any difference in speed between C or C++. Granted, an object-oriented C++ program can be slow if it isn't well written, but I've seen many badly written C programs that are slow. It all comes down to knowing what you're doing; it has nothing to do with C versus C++.

I wasn't saying that C was better than C++ for making games. I was saying that C with Lua scripting is better for making games than C++. :)

Trust me, I know the difference between C and C++, and I know what they share as well.

Ummon
2007.01.26, 09:10 PM
Thanks I guess it is just Lua as far as scripting goes. I'll check it out.

AnotherJake
2007.01.26, 09:20 PM
Im not sure why those two are trying to make you feel like youve asked a silly question, but take it from me you havent...

I'm surprised I missed this bit of your response earlier, but I take it `those two' includes me. I'm a little offended by your insinuation that I implied he was being silly. My `. . .' prefix was to denote that I wished to add at least a little more to OSC's curt response without going into exhaustive elaboration. Yeah, sometimes I restrain myself from blabbering believe it or not. However, if I were to blabber a bit more about it, I'd say Lua is my preferred language of choice as well.;)

Ummon
2007.01.27, 11:50 PM
Oops I didn't realize I was posting to Porting. I thought I was posting to Programming Languages. I'm very sorry.

TomorrowPlusX
2007.01.30, 08:09 AM
Are you all happy with Lua as a language? I bought PIL a year or so ago and read through it ( and did some testing of my own ) and by the end I was thinking: "I wouldn't touch this language with a 10 foot clown pole".

Python & Ruby are beautiful languages. Lua may be practical, but it smells to me.

unknown
2007.01.30, 11:21 AM
I much prefer lua over python and ruby especially for this purpose, but overall as well. You might be confusing a language with the libraries it has, its important to make the distinction.

Fenris
2007.01.30, 11:27 AM
Agreeing with Shamyl here, Lua's syntax is downright dysmal.

AnotherJake
2007.01.30, 12:06 PM
I guess that's why they make ice cream in different flavors :)

I like Lua because it's small, simple, doesn't try to be everything to everyone, and is real easy to integrate. I have a few gripes about the syntax too, but it'll get you off the ground quickly if you want to add scripting to your C game, which is what this thread is all about. Besides, Lua's where all the cool kids hang out! :cool:

Skorche
2007.01.30, 12:54 PM
I much prefer lua over python and ruby especially for this purpose, but overall as well. You might be confusing a language with the libraries it has, its important to make the distinction.

Though I think that the libraries it provides are important too, especially the ones that are built in. It might be fast and have a well written C API, but as a language it just seems mediocre to me.

OneSadCookie
2007.01.30, 02:08 PM
My general take on it is, if you want to write small amounts of code as scripts, or are desperate for security (eg. you intend to transmit user-written scripts across a network and run them on other players' machines) then Lua is a good choice.

If you want to write substantial portions of your application code in your scripting language, you want something that actually has useful language features, object-orientation, and a decent standard library. Python (particularly Stackless) and Ruby are both excellent choices for this use.

Also IM(NS)HO, if you're in the first camp, you should seriously consider being in the second!

mattz
2007.01.30, 07:00 PM
Are you all happy with Lua as a language? I bought PIL a year or so ago and read through it ( and did some testing of my own ) and by the end I was thinking: "I wouldn't touch this language with a 10 foot clown pole".

Python & Ruby are beautiful languages. Lua may be practical, but it smells to me.

lua's so dang easy to embed, I can hold my nose regarding the syntax. plus the VM fits in a tiny little static library, so it feels much more managble than an entire python/ruby VM.

that said, I've never embedded python/ruby in C, just the other way around

TomorrowPlusX
2007.01.31, 07:58 AM
I'm talking about Lua the language. The kind of meta understanding you need just to do polymorphism and OO programming in general is deeply offputting.

That being said, it's clearly very performant and has great embedding.

I don't care. I'm going to use SWIG to generate python bindings.

AnotherJake
2007.01.31, 10:41 AM
I'm talking about Lua the language. The kind of meta understanding you need just to do polymorphism and OO programming in general is deeply offputting.
Well, but, we're talking about embedding a scripting language in C, not C++. Lua tends to present a more procedural way of doing things, like C. You, OTOH, if I may see it this way, deeply understand polymorphism and OO programming in general, and prefer C++ to begin with. It makes sense to me that you wouldn't like Lua from the beginning.

Which brings up another point of debate about scripting languages in general. If the application would allow for the use of a procedural language or an OO language, which would end-users prefer? My guess would be procedural, since that tends to be easier to digest upon first approach.

unknown
2007.01.31, 10:56 AM
Lua adapts well to the OO model actually, I used embedding, object orientation and polymorphism extensively in N0x35C49E (http://fax.twilightcoders.net/N0x35C49E/)

Theres also another great thing about the "lightweightness" of it, you can really alter the basics of it if you need to, for example when I was trying to bridge lua, io and ruby together I found a very simple way to make a function call in a lua script fallback to the interpreter and then be tested to see if an io or ruby function of the same prototype existed.

mattz
2007.01.31, 05:16 PM
TomorrowPlusX raises the very good point that SWIG does a lot of heavy lifting for you when you want to mix Python or Ruby with C/C++, which is nice. Currently, SWIG doesn't do Lua... I developed a decent set of C++ template files that make it really easy to wrap C++ objects in Lua (I'm sure other people have done the same).

Also, I'm interested in how people do embedding... is your game "engine" made up of C/C++ objects wrapped inside of a scripting environment, or do you use scripting just to handle a few things (i.e. level loading & NPC AI) with most of the code living in compiled modules? On the few projects where I've done scripting, I use the latter approach.

OneSadCookie
2007.01.31, 06:11 PM
In my experience, SWIG never does anything for you but waste your time generating shit, or failing to generate anything at all...

I have a thin layer of ObjC at the top for windowing and events, then a thick layer of object-oriented scripting where the bulk of the game is, then a thin but heavy-lifting layer of C at the bottom for the bits that are too slow or too low-level to do in the scripting language.

DoG
2007.02.01, 12:04 PM
I have to say, OneSadCookie is mostly right. Lua is more useful as a configuration language than for full game logic.

Personally, I've been using the IoLanguage, which while being a bit slower than Lua, probably surpasses it on every other front.

Also, there are other alternatives to Lua, besides Python, Ruby, or Io. Pike comes to mind, and I am sure there's a million and one other such projects out there.

bronxbomber92
2007.02.01, 03:30 PM
How do you embed python in Objective-C code?

djork
2007.02.01, 03:49 PM
I've been thinkinga bout my current aspirations. I have been playing with embedding Lua in C, and have had quite a bit of success with drawing polygons, loading images, rendering them to the screen, taking keyboard commands, and the like.

My goal was to write the whole game in Lua, but now I think that it might not be the right way to go. I think it may be better to use Lua to inform the C engine of what is going on or what to do. For instance: loading levels should be in Lua, but iterating through the grid, culling out the non-visible tiles, and rendering the whole shebang is a better task for C.

To make the engine as flexible as possible would be a daunting task in itself. I should just make a game and use C/Lua wherever it fits instead of a grander idea.

Skorche
2007.02.01, 03:51 PM
How do you embed python in Objective-C code?

The exact same way you embed it in C.

bronxbomber92
2007.02.01, 03:58 PM
The exact same way you embed it in C.

Which is how?

I tried this: #include <Python.h>

int main(int argc, char *argv[])
{
PyMac_Initialize();
PyRun_SimpleString("from time import time,ctime\n"
"print 'Today is',ctime(time())\n");
Py_Finalize();
return 0;
} But it couldn't find Python.h...

OneSadCookie
2007.02.01, 04:03 PM
Try Python/Python.h ?

bronxbomber92
2007.02.01, 04:27 PM
All right, I got it to work :)

Thanks...

TomorrowPlusX
2007.02.05, 04:32 PM
Has anybody used Boost.Python?

http://www.boost.org/libs/python/doc/tutorial/doc/html/index.html

I just stumbled across it. After OSC "glowing" report on SWIG, I thought perhaps I might look in other directions...

Joseph Duchesne
2007.02.05, 05:33 PM
I might add that sometimes things seem simple and brilliant to people who are brilliant enough to reduce them to simplicity... ;) Of course I could be wrong and it's really simple for everyone :P

Lua is simple for me though, and compared to many programmers I'm relitively inexperienced.

Ummon
2007.02.07, 11:49 AM
I would really want to do everything in a scripting language. However, I believe that Writing a collision detection system in a scripting language would be computation expensive. Am I correct?

AnotherJake
2007.02.07, 12:09 PM
Everything done in a scripting language is computationally expensive, relatively speaking. Circle and rectangle collision detection are pretty easy though, so you'd probably be able to do that without much problem -- maybe even some polygonal collision detection. Try it and find out!

Ummon
2007.02.07, 05:59 PM
Sorry, I'm doing 3d games. Should have clarified that.

Still, It doesn't hurt to try scripting based collision

AnotherJake
2007.02.07, 07:53 PM
I would imagine that AABB and bounding sphere checks could be done pretty easily. I would be skeptical past that. I do my 3D collision in the engine, not the scripts, but like you said, it surely couldn't hurt to try. Good luck with it, and let us know how it goes!

Ummon
2007.02.11, 12:58 PM
Well, you see, the things is, I'm extremely busy with my science fair project right now. However, I promise I'll come back to trying to do 3d collision detection in Python using scipy as soon as I'm done (which should take about 2 weeks).

Joseph Duchesne
2007.02.11, 01:19 PM
I would suggest leaving physics to a compiled language like C. In my game here's what i do:
C Code:
Handles image loading/saving
Physics
Keyboard Input/Output
Drawing functions
Most other stuff

Lua Code:
Menus and Prefs through graphics and file Lua wrappers to my C functions
Setting up each level ("Player 1 Start!")
Game guides/special events (run into a special package and get certain bullets)
Special Collision events (hitting asteroid X runs Win.lua or something)

What Lua allows me to do is extend and expand the game without recompiling the whole thing and because it is simpler it allows me to focus on doing the task at hand.

But for fun, here's Pong.lua that clicking the app's icon in the prefs triggers:

--pong in lua...
draw.loadImage("flares.tga",14)
-- set up the variables
playFieldWidth=384
playFieldHeight=240

playFieldX=1024/2-384/2
playFieldY=768/2-240/2

player1x=10
player1y=120

player2x=playFieldWidth-10
player2y=120

score1=0
score2=0

ballx=384/2
bally=120
ballxv=-64
ballyv=64
r=0

function main(timer)
draw.pushMatrix()
r = r + 4*timer
draw.translate(playFieldX,playFieldY) --translate everything to the playfield
-- Draw the background
draw.color(0.0,0.2,0.0,1.0)
draw.rect(0,0,playFieldWidth,playFieldHeight)
draw.color(0.0,1.0,0.0,1.0)

draw.pushMatrix() --push a matrix for draw.text
draw.rotate(math.sin(r)*3) -- rotate the text
draw.text(0,playFieldHeight+10,"Score: "..tostring(score1)..","..tostring(score2))
draw.popMatrix()

-- Draw/move Player 1
draw.color(1.0,0.0,0.0,1.0)
draw.rect(player1x-5,player1y-30,player1x+5,player1y+30)
if player1y<playFieldHeight-32 and madtak.getKey(119)==1 then
player1y = player1y + 512 * timer
end
if player1y>32 and madtak.getKey(115)==1 then
player1y = player1y - 512 * timer
end
if player1y>playFieldHeight-32 then player1y=playFieldHeight-32 end
if player1y<32 then player1y=32 end

if ballx<15 and bally>player1y-30 and bally<player1y+30 then
ballxv = ballxv * -1
ballx=15
end

if ballx<2 then
score2 = score2 + 1
ballx=384/2
bally=120
ballxv=(math.random(1,2)*2-3)*64 -- -64 or 64
ballyv=(math.random(1,2)*2-3)*64
end

-- Draw/move Player 2
draw.color(1.0,0.0,0.0,1.0)
draw.rect(player2x-5,player2y-30,player2x+5,player2y+30)
if player2y<playFieldHeight-32 and madtak.getKey(4)==1 then
player2y = player2y + 512 * timer
end
if player2y>32 and madtak.getKey(3)==1 then
player2y = player2y - 512 * timer
end
if player2y>playFieldHeight-32 then player2y=playFieldHeight-32 end
if player2y<32 then player2y=32 end

if ballx>playFieldWidth-15 and bally>player2y-30 and bally<player2y+30 then
ballxv = ballxv * -1
ballx=playFieldWidth-15
end

if ballx>playFieldWidth-2 then
score1 = score1 + 1
ballx=384/2
bally=120
ballxv=(math.random(1,2)*2-3)*64 -- -64 or 64
ballyv=(math.random(1,2)*2-3)*64
end

-- The ball
draw.color(1.0,1.0,1.0,1.0)
draw.pushMatrix()
draw.translate(ballx,bally)
draw.scale(4*math.cos(r),4*math.sin(r))
draw.rect(-2,-2,2,2)
draw.popMatrix()

ballx = ballx + ballxv*timer
bally = bally + ballyv*timer
if bally<2 then
ballyv = ballyv * -1
bally=2
end
if bally>playFieldHeight-2 then
ballyv = ballyv * -1
bally=playFieldHeight-2
end

draw.popMatrix()

if madtak.getKey(114)==1 then
madtak.console("luamenu menu.lua")
madtak.setKey(114,0)
end

draw.text(20,20,"A,S,D,W and Arrow keys to move, r to return to menu")

return 1;
end

All draw.something functions are openGL tie ins, and all madtak.something functions tie into my game engine, for example:

if madtak.getKey(114)==1 then
madtak.console("luamenu menu.lua")
madtak.setKey(114,0)
end

This checks to see if key 114 (ASCII: r) is pressed, and if it is, enters the command "luamenu menu.lua" in my game's console (which predates has nothing to do with the lua implementation, it's simply my way of switching between in game modes and toggle frames per second counters and other such tasks) before turning off the flag that key 114 was pressed.

I'm not saying that this setup is the best possible scenario (I don't have the experience to back that claim up), but I absolutely love it. I presume that other scripting languages could be integrated in similar ways :)