Author Topic: Becoming A Scripter 101  (Read 1262 times)

Offline Mario

  • Senior Software Developer
  • Administrator
  • Posts: 2186
  • Cookies: 1706
  • Life is life
Becoming A Scripter 101
« on: January 10, 2011, 06:35:58 PM »
I'd like to point out that this is only a longer version of the reply: "Start learning python or start learning the basics of programming to be able to create scripted mods for BC". Since I've noticed that most people don't have a clue at all where to actually start. Don't mind my ranting as I'm trying to explain why it's so difficult to put most of what we know into tutorials as the sheer scope of it is beyond words. All I can do is point people in the right direction and help out by giving advice.

I'm going to attempt to create some basic guidelines on helping people become scripters and write some small series of tutorials. This tutorial is not for you if you already have programming experience. But to actually sum all of this up is impossible and finally what will create a good scripter is the good old process of trial and error.

First the things I'd recommend are:
  • Python, which can be downloaded from python.org. I strongly recommend downloading no higher than v2.5.
  • BCSDK which can be downloaded from bcfiles. This you can use as a resource to view what types of events BC can fire (seen in App.py), access to python source code and the likes.
  • PyLint http://www.logilab.org/857 this is a small python tool which I love to use. It will analyze your source code and point out errors such as undefined variables, it will check your code strength and it will point out missed imports and the likes.

Any serious developer will utilize the strengths of pylint kind of tools. In Microsoft Visual Studio features such as what pylint offers are in there by default and I strongly rely on them. Of course debugging BC is a whole new process since I can't like in VS press the debug button and I can't view in real-time what happens. So if you make a mistake in your code it's a pain in the ass process to correct it.

Now a very important question: "What does make you a scripter"?
  • Being able to read and edit code does not make you a scripter, it's a start but does not make you a scripter.
  • Being able to write code on your own and understanding the basics of programming does make you a programmer\scripter. The deeper understanding you have about it makes you a better and more advanced programmer.
  • Understanding the concepts of programming does not mean that you will be a good programmer or scripter. Like in the old saying: There are 2 kinds of people, one who understand binary and the ones who don't lol Some people are simply not meant for this.

One of the most popular resentments from people which I've noticed over the years is that some people believe that experienced modders don't want to pass on their knowledge. Every now and then a person will show up claiming that we're withholding information.
This of course is not true as you can't possibly sum years or months of experiences into a few pages long tutorial. Above all:
  • We have our lives to live and while creating mods in our spare time, we should find time to write now even tutorials. While some people do find time for this, most do not and are instead blamed for withholding information. Some people easily forget indeed.
  • What is possible for more experienced modders is to point newcomers in the right direction, but you're on your own from then. You can still ask experienced modders for help and most will happily reply and offer guidance but they can't do their job for you if you decided to take on a project.

I know based on my own experiences that I learned this all on my own, I asked if something wasn't clear to me but I didn't blame anyone for withholding information. I speak from my programmer's point of view of course; there are people which believe that we're hiding some important secrets from all of you, to stop you from becoming a successful scripter. I don't have any comments on that since it is absurd to even comment on it.

Now let's start after my little rant. First where I'd like to get started above all are the simplest basics. I will post some short examples to point you in the right direction. If you've no clue what these are then there is really no point in attempting to write scripts. In no particular order:

  • Variables
Code: [Select]
pVar = "this is a string"
print pVar

  • Basic operations
Code: [Select]
1+1
int(1) + int(2)
3.0 + 4.0
float(3) + float(4)
s1 = "this"
s2 = " is"
s3 = " a"
s4 = " string"
print s1 + s2 + s3+ s4

  • Functions
Code: [Select]
def myFunc():
print "If you don't understand what is all of this then don't claim you're a scripter"


  • Loops
Code: [Select]
for i in range(1, 5): print i


  • Conditionals
Code: [Select]
i = 0
while i != 5:
    print i
    # beware of infinite loop
    i = i+1


  • Lists
Code: [Select]
def output(l):
    print 'start'
    for i in l:
        print i    


l = [1,2,3,4,5]
output(l)
l.remove(3)
output(l)
l.append(3)
output(l)
l.sort()
output(l)


  • Dictionaries
Code: [Select]
d = {1:1, 2:2, 3:3}
for k in d.keys():
    print k

for v in d.values():
    print v

for k,v in d.items():
    print k,v

if d.has_key(1):
    d[1] = 7 # change value
    
if d.has_key(2):
    del d[2] # delete value from dictionary

print d

  • Recursion
Code: [Select]
def x(i):
    if i <= 0:
        return 0
    retVal = x(i - 1)
    return retVal

print x(5)
To simply explain recursion in a funny sort of way. I shall quote one of the most hysterical source code comments for me personally...
Code: [Select]
def x(i):
    # If you want to understand recursion view the bottom of this method
    if i <= 0:
        return 0
    retVal = x(i - 1)
    # If you want to understand recursion view the top of this method
    return retVal
    
print x(5)

  • Classes
Code: [Select]
class something:
    def __init__(self, message):
        self.message = message
        
    def showMessage(self):
        return self.message

obj = something("some message")
print obj.showMessage()

  • Importing modules
Code: [Select]
import math
print math.sqrt(99)

    For as long as you've no clue what these are and what these do, don't call yourself a scripter and don't even attempt to start writing code. You can try but you'll end up being frustrated. I cannot stress enough how important is to understand this.

    With this very basic and short intro I shall end this tutorial as for a newcomer to scripting this can and will be overwhelming at first. So let's call it a day.

    For any additional information regarding these very basics you can find in the actual python documentation. But this is where you start, this is nothing BC specific.

    You may post additional questions if you have them.
    Acta, non verba.
    aka USS Sovereign

    Offline Defiant

    • Posts: 398
    • Cookies: 1105
      • BC: Kobayashi Maru
    Re: Becoming A Scripter 101
    « Reply #1 on: September 19, 2011, 02:26:20 PM »
      There are 2 kinds of people, one who understand binary and the ones who don't
      Uhm, I know this one only with "There are 10 kinds of..."

      One tiny note:
      [/list]
      Code: [Select]
      pVar = "this is a string"
      print pVar
      In BC variables beginning with a p are usually used for instances of user defined Objects, s for strings. I think the tutorial should not break the style.

      Offline Mario

      • Senior Software Developer
      • Administrator
      • Posts: 2186
      • Cookies: 1706
      • Life is life
      Re: Becoming A Scripter 101
      « Reply #2 on: September 19, 2011, 03:10:54 PM »
      Each programmer defines his own style or adopts the style used by the teammates (depends really on the situation). In truth you're probably correct, but this is harmless. Hungarian Notation to me is pretty much obsolete and useless, aside from BC I've never used it. If we start this discussion it's never going to stop.
      Acta, non verba.
      aka USS Sovereign

      Offline cordanilus

      • Posts: 492
      • Cookies: 608
      Re: Becoming A Scripter 101
      « Reply #3 on: February 09, 2012, 10:41:26 PM »
      Yeah, I don't think I was cut out for it.  lol  I can follow some of it, edit parts of it...but that's it. :-P