Syntax sugar project for Module system

Users who are viewing this thread

Invictus

Grandmaster Knight
At the moment it's in a proof-of-concept state. The idea of this project is to extend module system in a way that makes scripts easier to understand, comfortable to write but still maintain compability with the original python code.

So the first thing that I made was to define a wrapper around Troop "class". We don't actually have classes in module system since it is procedure-oriented, but we now that troop instances exist, right?  :wink: It might be somewhat difficult to explain the point, let's see an example instead.
Suppose we want to give some falchions to a looter. We would write such statement in our code:
Code:
(troop_add_items, "trp_looter", "itm_falchion", 10),
But with our wrapper we can express the same statement this way:
Code:
T("looter").add_items("itm_falchion", 10),
Now it looks more "object oriented". T is our troop wrapper class. The trick is that this statement actually evaluates to the same touple from previous case so process_* scripts can't see the difference.
There are 2 main advantages of wrappers:
1) The code becomes easier to read and understand.
2) It gives us a way to exploit advanced code assist features of python script editors. I use pydev(http://www.pydev.org/) myself, for example.
1297070852-clip-10kb.png


1297070943-clip-14kb.png

Code:
class T:
	def __init__(self, ID):
		if (ID[0] == ":") or (ID[0] == "$"):
			self.ID = ID
		else: 
			self.ID = "trp_" + ID 

	def add_items(self, item, number):                       
		return(troop_add_items, self.id, item, number)

I've already created definitions for most troop related operations. To use them you can download and import this python file: http://warband-sugar.googlecode.com/files/sugar_extras.py

If the idea is accepted by the community, I'll continue with defining wrappers for other classes of instances and may introduce more syntax sugar.
If you would like to contribute, join project at http://code.google.com/p/warband-sugar/
Feedback is required anyway :smile:

P.S. I haven't been modding M&B for a while so have no idea if there were any similiar things here. I'd appreciate if you pointed them out in case there were.
 
I'm really pleased, more experienced modders come to the Mount&Blade module editing. Great innovations.
Object oriented programming is time-saver. Really common in the game development and almost indispensable for a readable flow.

And the format of plug-in-able file you're distributing makes it more and more valuable and extend-able. You're good.
I'll use it in SWC. After a while I'll comment you how it goes.

All the community can benefice of your great idea. Both newbies and oldies.


PS: Python IDE from Aptana?. Quite surprised! I thought they were only supporting Web + Ruby on Rails ATM.
 
An OOP module system would be a fantastic. 

I'll be watching this closely. 

EDIT:  Combined with an auto-complete, this would be the next best thing to heaven.  :razz: 
 
I'm 100% for this.
I myself do OOP all over the place and cover it in uni, but the syntax of the M&B scripts scare me away.
This would be amazing.
 
MadocComadrin said:
Interesting. I've been working on something myself, except mine is essentially a new language.
I have been periodically approaching the idea of new language for M&B since 2005, when the module system was released. But every time I gave up, because new language didn't have a serious advantage over python modules. The reason is that significant part of modules describe static data such as items, troops, parties etc. and python touples fit perfectly here. The only thing that needs to be changed is script operation statements that have no clear structure. It's somewhat difficult to remember almost 1k operations! Especially when they are just enumerated instead of being grouped in an easy to explore way.
And yesterday I was enlightened from the above that touple statements in scripts can be replaced by functions that produce them. This is so obvious that I don't see why it hasn't been implemented before. So I just went ahead and started this project. I'm neither python guru, nor python fan btw and this idea was totally random. Said that I expect that people with higher python skills (I emphasize python here, not M&B modding skills) may introduce more elegant approach to the idea. But the main point would remain the same: "replace touple statements with python entities that produce them".
 
nice )
the only suggestion i'd made is:
Code:
    if not globals().has_key(ID) or isinstance(globals()[ID], T_base_class): globals()[ID] = self
    ...
or something like that
 
Back
Top Bottom