Chunky by FelipeFS
Chunky by FelipeFS
GPWiki.org
It is currently Thu Jul 24, 2014 12:18 pm

All times are UTC




Post new topic Reply to topic  [ 43 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
PostPosted: Sun May 04, 2014 9:17 am 
Grand Optimizer

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 358
Location: Here (where else?)
Salamander wrote:
Mind=blown.
Sorry for making you loose your mind :p

Salamander wrote:
Now, to a more technical question that raised itself when I started looking into this:
Cython or SWIG?
They are different things afaik, but I haven't followed this area.

The "core" way of doing things is using the C-Python API, as described in the "extending" document. If you write some code in that API, you'll notice there are loads of patterns in the code. And thus there are code generators, such as SWIG, that aim to reduce the amount of work.
(Now that you mention SWIG, you might want to have a look at the Micropolis project at googlecode, which uses SWIG.)

So far so good. This is all fine, but you still have to write C or C++ code for the "fast" functions. What if you don't want to do that (or don't know C/C++)? That's what Cython aimed to fix afaik. You can write Python, and add C-ish code. Cython compiles that blend into an executable. It gives you much more fine-grained control of what is Python and what is C than a single function.

It is however in my view not glueing C/C++ code to Python, it is writing faster Python code.
(But as I said, this was about 10 years ago, when Cython just appeared, and was all beta and experimental. The landscape may have changed dramatically in those years.)

Salamander wrote:
I'll assume asking "which is better" won't get me anyway. But which do you personally prefer, and why? Which one would be more friendly to someone new to the bindings concept? Other things I should know before making the choice?
I don't believe there is a "best", unlike what vendors are trying to make you believe. The open source eco system generally has 5+ solutions to every problem, and they all serve a market where their solution excels.

The applications that I generally write are code generators, which are typically small but complicated. Together with my desire to have full control over what happens in my code, the solution for me is the official C-Python API. I'll take the patterns, or write a code generator to eliminate them :p
On the other hand, when confronted with SWIG in Micropolis, I had some trouble convincing it to work, but it generally did its job. If your interface is big (and it will be), SWIG may be a lot simpler in the end. (If you're like me, and you want to understand the code generated by SWIG, I'd recommend at least playing with the official API for a bit. It'll give you a good idea of what SWIG is actually solving.)

I don't know about Cython. I played with it one time. It is on my list of things to consider for "really fast Python code", based on information that is 10 years old. However, with the speed of current machines, and me moving in a different direction, it's unlikely I'll ever end up with Cython in the near future.

EDIT: While re-reading my answer, you should check that Cython can handle pure Python code. If you want to allow user-contributed scripts, but Cython requires re-compilation to add that code into the system, you might have a problem.

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sun May 04, 2014 12:56 pm 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Oh by all means, don't be sorry. This is one of the best things that have happened to me during my career as a game dev (well, since transferring from high-level javascript and HTML5 to low-level Java and even more so c++, at least), 'tis just a lot to digest. It's the mixed feeling of completely new horizons and possibilities, but on the other hand, oh-man-I-still-have-so-much-more-to-learn. And here I was thinking I had programming pretty much figured out.
This, on a side note, is how scientists must have felt when they discovered quantum mechanics ;) .

Well from what you write, it seems SWIG would be the one more suited one for my case, since I do know c++ and that is my "homeland", if you so will. However, I will probably go with the "core" approach using the C-Python API, because yeah, I too like to have full control over what's happening. It is the reason I love in low-level langs.
All that is, of course, if I actually decide to go with Python at all, because squirrel is still catching my eye. Just can't get it to build, tho (is it even supposed to work in console?).

Anyways, the reason I got into Cython/SWIG was that the first lines of the c-python API doc were basically:
"Please use third-party software [like Cython, SWIG, some others were mentioned] to do this.
Pretty please.
If you want to see how they do it, you can look below.
But you should user the third-party solutions."

No, user-contributed scripts were never part of my plan, I needed something for me and possibly for my level designer. However, these scripting possibilities vastly extend what I was planning to do with them so who knows, maybe I will allow users to add their own scripts? On second thought, if I am to release a level editor, and use scripts for scripted events, That would indeed have to be the case.


Top
 Profile  
 
PostPosted: Sun May 04, 2014 3:44 pm 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Hey guess what? All this business made me so curious as to what scripts might be used for, I decided to download Unity and see for myself. And I found the scripts being everywhere.

But this is the thing, neither Squirrel nor Python were allowed languages, there was javascript, c#, and some more. So I thought "well, if Unity is doing it, why not me?" And, judging from this link, which later goes here, this seems to be really, really easy. And since I already have some experience in JS (actually made a few games, fractal renderers, and what not about a year ago), this seems like the ultimate solution for me.

What do you think about this? Are there any reasons I should stick to the languages discussed/avoid JS like the plague? Or do you think is is a good idea?

And by the way, Unity rocks.


Top
 Profile  
 
PostPosted: Sun May 04, 2014 5:33 pm 
Grand Optimizer

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 358
Location: Here (where else?)
I have never used Javascript, the language examples don't look very appealing to me, but that may be just me preferring languages with a light syntax.

I also don't know Unity. The name pops up regularly here, so it must be useful in some way. By the looks of it I would probably never use it (I don't want to rely on a commercial vendor for my programming language or framework.), but then again I don't write games for a living.


Other than that, the general principle of where to use a scripting language and where to use some low level machinery are still all valid as far as I can see. So if this is what makes your heart go faster, by all means use this :)

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sun May 04, 2014 5:56 pm 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Unity is a game engine (well, more than that, I'd say) that's quite powerful but also relatively easy to use (it has an awesome editor, from my few hours of testing it, anyways). I was never planning on using it for my products, rather, I wanted to see whether they use scripts. And they do. Extensively.

Thb I don't like javascript (that comes from me not liking high-level languages at all) either but at least I know it, and it's not so bad. Plus, the google V8 engine is said to compile it to bytecode before execution, so it should be pretty fast. And, the binding seems easy. So yeah, I think I'll go with that one.

Many thanks for your help :).


Top
 Profile  
 
PostPosted: Wed May 28, 2014 8:02 pm 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Hey all, I thought I'd let you know how it went for me. So it turned out that no matter what scripting language I was trying to embed, things were going wrong. In V8 I was not able to build the engine (albeit trying multiple multiple times), Python I don't remember what went wrong but I was not able to link my program to python. In my desperation I turned to less popular languages such as Squirrel; no luck there, and just before trying http://www.angelcode.com/angelscript/, I decided to give lua a try.

I instantly started to like Lua (part of that due to it being developed by a catholic university, and me being catholic ;) ) and altho there were some severe build problems there aswell, being helped by a russian guy on stackoverflow did the trick (apparently I had the wrong lua package on my linux). So yeah, @theJosh, here I am, starting out in lua. I'm sitting right now and thinking about how I should incorporate lua in my game. Should I make a class "Lua" and let it be the gateway other classes use to access lua functionality? Or should I bake it into the enemy class? But no, I want to use scripting for scripted events too, not only for enemy AI... I'm analyzing you chaotic rage code by the way, it's much more helpful to me now than it was then. Thanks for posting it. Hmm, will probably implement some kind of system of triggers and lua being able to hook up to those. Having lua be "in charge" totally would require completely revising my game structure, but perhaps it's worth it? We'll see. Also, I have to go through this thread again, there are probably tons of great tips that I didn't pick up when I was blinded by the realization that I can actually have runtime scripts, but that will be useful now.

So yeah, I hit a brick wall for a week or three, but now I'm back on track and can start coding again. Great!


Top
 Profile  
 
PostPosted: Thu May 29, 2014 3:20 am 
Ankle Nibbler
User avatar

Joined: Tue Sep 25, 2012 12:57 pm
Posts: 137
Location: Adelaide, Australia
It's good that I could help.

I've just started doing work to change some of my code to use the excellent LuaBridge header-only library. My triggers are still managed with "standard" Lua interfaces, and will probably remain like that. It's the various function calls which I am now exposing to Lua using LuaBridge.

It also supports OO, so I can return a pointer to an object from a function, and it's an object in Lua so then the Lua code and modify it and call methods on it. You've still got to define the properties and the methods which are exposed, but it's heaps better writing one line to expose a method than writing dozens that I used to have to write.

_________________
Chaotic Rage - a fast paced, shooter game that's a little odd, but still fun.


Top
 Profile  
 
PostPosted: Thu May 29, 2014 7:10 am 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
LuaBridge ey? Looks promising, going to look into it. And header-only, that's a big plus for me; won't be able to stall my development for two weeks more with make errors and linking problems ;)


Top
 Profile  
 
PostPosted: Sat May 31, 2014 8:57 am 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Hey Josh (or by all means anyone else who knows lua-c api well), I still have a few questions about the lua-c api. The documentation is not, sadly enough, top notch, or at leas I have not encountered any really good sites.

1.
Consider following function (from your code, but it's a general question)(TL;DR you don't actually have to consider the code to understand the question):

Code:
LUA_FUNC(add_timer)
{
   if (! lua_isnumber(L, 1)) {
      lua_pushstring(L, "Arg #1 is not an integer");
      lua_error(L);
   }

   if (! lua_isfunction(L, 2)) {
      lua_pushstring(L, "Arg #1 is not a function");
      lua_error(L);
   }

   int time = lua_tointeger(L, 1);

   lua_pushvalue(L, -1);
   int func = luaL_ref(L, LUA_REGISTRYINDEX);

   LuaTimer* t = new LuaTimer();
   gl->timers.push_back(t);

   t->due = gl->st->game_time + time;
   t->lua_func = func;
   t->interval = 0;

   lua_pushnumber(L, gl->timers.size() - 1);
   return 1;
}


When using lua_tointeger() and lua_pushvalue(), are we grabbing from and pushing to two separate stacks? or is there only one?

2. In the same code, what is LUA_REGISTRYINDEX?


Top
 Profile  
 
PostPosted: Sat May 31, 2014 9:25 am 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Also, as a side-note, is there a reason you use a vector instead of a list for the timers? Since you're going to be manipulating items in every place in the container, but only need to access them through iterating over all of them, it would have been faster to use a list, right? Or maybe you need the continuous storage locations for something?


Top
 Profile  
 
PostPosted: Sat May 31, 2014 9:55 am 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Ah, it's necessary in the deletion, ok. But that does leave the possibility for a unrestricted growing vector of nulls... I'll think about if I can implement it in a better way myself...


Top
 Profile  
 
PostPosted: Sun Jun 01, 2014 8:49 am 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3795
Location: South Africa
Salamander wrote:
Also, as a side-note, is there a reason you use a vector instead of a list for the timers? Since you're going to be manipulating items in every place in the container, but only need to access them through iterating over all of them, it would have been faster to use a list, right? Or maybe you need the continuous storage locations for something?

Linked lists are pretty terrible for memory access because the elements can become spread out from each other. If you're wanting to access the list in order, worst case, you have to fetch each thing from memory one at a time. With a vector or array, when you access the first element, the elements next to it tend to get read into memory at the same time, which means reading them in order is much faster (don't have to keep going back to memory, it's already in the cache).

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Sun Jun 01, 2014 9:08 am 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Ah I did not know about that, thanks. Nevertheless, What I wonder about most is still how the stacks work more precisely with lua-c API.


Top
 Profile  
 
PostPosted: Sun Jun 01, 2014 11:44 am 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3795
Location: South Africa
Salamander wrote:
Ah I did not know about that, thanks. Nevertheless, What I wonder about most is still how the stacks work more precisely with lua-c API.

I don't know anything about lua but, if I were to guess based on other API's, there's probably a single stack that you're dealing with.

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Sun Jun 01, 2014 1:59 pm 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Yeah that's what I thought aswell, but consider this function:
Code:
LUA_FUNC(add_interval)
{
   if (! lua_isnumber(L, 1)) {
      lua_pushstring(L, "Arg #1 is not an integer");
      lua_error(L);
   }

   if (! lua_isfunction(L, 2)) {
      lua_pushstring(L, "Arg #1 is not a function");
      lua_error(L);
   }

   int time = lua_tointeger(L, 1);

   lua_pushvalue(L, -1);
   int func = luaL_ref(L, LUA_REGISTRYINDEX);

   LuaTimer* t = new LuaTimer();
   gl->timers.push_back(t);

   t->due = gl->st->game_time + time;
   t->lua_func = func;
   t->interval = time;

   lua_pushnumber(L, gl->timers.size() - 1);
   return 1;
}
and specifically this line:
Code:
lua_pushvalue(L, -1);
When I google that funciton, I get here, and here is the description:
Quote:
[-0, +1, -]
void lua_pushvalue (lua_State *L, int index);
Pushes a copy of the element at the given valid index onto the stack.
But when using indices, we're accessing the stack, right? So what does this function do, take an item from the stack and put it... on the stack? This is what confuses me.


Top
 Profile  
 
PostPosted: Sun Jun 01, 2014 3:03 pm 
Grand Optimizer

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 358
Location: Here (where else?)
Don't know lua C, but it looks like a "dup" (duplicate item) to me, quite normal in stack-based calculations, for example
(n + n * 15)

Quote:
push n
dup
push 15
mul
add
Especially when n is a result of a long computation.

Remember, you don't have a register to store data in.

For inspiration, read about Forth :)

_________________
My project: Messing about in FreeRCT, dev blog, and IRC #freerct at oftc.net


Top
 Profile  
 
PostPosted: Sun Jun 01, 2014 9:09 pm 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Oh... Yeah, that might be an explanation. Still a bit weird because well, you do have a register to store data in when using lua or c, just create a temporary variable, but this does seem reasonable. Thanks. Still, if someone that knows lua-c has something to say, please do. And to that someone - I'm still not sure what LUA_REGISTRYINDEX is!


Top
 Profile  
 
PostPosted: Tue Jun 03, 2014 4:27 pm 
Gamer Geek

Joined: Thu May 01, 2014 8:18 pm
Posts: 31
Hah, it works! Just wanted to know I just succeeded in implementing lua in my game, that is, my rat is still as retarded as before, but now that comes from a lua script rat.lua. Fantastic! I'm still not sue what the registry is, but I'll tread it as some kind of global map you can put variables in for later use.


Top
 Profile  
 
PostPosted: Wed Jun 04, 2014 6:10 pm 
Dexterous Droid
User avatar

Joined: Wed Aug 18, 2004 7:40 pm
Posts: 3795
Location: South Africa
Cool, well done! :)

_________________
Whatever the mind can conceive and believe, it can achieve


Top
 Profile  
 
PostPosted: Wed Jun 04, 2014 11:06 pm 
Ankle Nibbler
User avatar

Joined: Tue Sep 25, 2012 12:57 pm
Posts: 137
Location: Adelaide, Australia
Quote:
I'm still not sue what the registry is, but I'll tread it as some kind of global map you can put variables in for later use.

Correct.

Sorry about the lateness of my reply, I've been kinda busy recently.

_________________
Chaotic Rage - a fast paced, shooter game that's a little odd, but still fun.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 43 posts ]  Go to page Previous  1, 2, 3  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

Powered by phpBB® Forum Software © phpBB Group