Lemma Soft Forums

Supporting creators of visual novels and story-based games since 2003.


Visit our new games list, blog aggregator, IRC, and wiki.
Activation problem? Email [email protected]
It is currently Wed May 22, 2013 1:42 am

All times are UTC - 5 hours [ DST ]


Forum rules


Ask questions about one topic per thread, and use a descriptive subject. "NotImplemented error in script.rpy" is a good subject, "Tom's problems" is not. Remember to include all of traceback.txt or error.txt when reporting a problem, as well as the relevant lines of script. Use the [code] tag to format scripts.



Post new topic Reply to topic  [ 34 posts ]  Go to page Previous  1, 2, 3  Next
Author Message
PostPosted: Sat May 12, 2012 3:28 am 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
Or a more simple version, so that I could figure out what is wrong with my logic:

Suppose I have these lines of code.

Code:
init python:

    class a:
        def  __init__(self, name, n, m):
            self.name = name
            self.n = n
            self.m = m
        def n (self, m, n):
            while n > 0:
                m = m + 1
                n = n - 1
                         
label start:
   
    python:
        X = a ("X", n = 3, m = 0)
   
    $ X_n = X.n
    $ X_m = X.m       
    "X.n = %(X_n)d"
    "X.m = %(X_m)d"   
               
label end:
    "bye"


The goal: I want the game to display each value of X.n and X.m until X.n reaches zero, one at a time. Which is to say, I want to make it go "X.n = 3", "X.m = 0", "X.n = 2", "X.m = 1" and so on and so forth. How should I make it work?

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Sat May 12, 2012 9:24 am 
Veteran
User avatar

Joined: Wed Nov 18, 2009 11:17 am
Posts: 359
Location: Germany
Completed: Loren
Projects: PS2
This would be the/a correct way to do this.
Code:
init python:
    class CounterTest: # Class names should begin with an uppercase letter, they should also be descriptive.
        def __init__(self, pName, pN, pM): # That's just a convention from my CS class. But it might help you with keeping parameters separate.
            self.name = pName
            self.downCount = pN +1 # Again, variables and fields should have descriptive names to improve readability.
            self.upcount = pM - 1 # The modifications are needed for the loop, since we call the counter method before we display the values.

        def count(self): # You don't need any parameters for this method, we can access the class fields with self directly.
            if self.downcount > 0:
                self.upcount += 1 # That's the short notation for: self.upcount = self.upcount + 1
                self.downcount -= 1
 
label start:
    $ counter = CounterTest("Testcounter",3,0) # Variables should start lowercase to separate them from classes.
    while counter.downCount > 0: # The display loop is best done in Ren'Py instead of python code.
        $ counter.count() # That's a method call, so the code in count is executed.
        "Downcount: %(counter.downCount)i" # We can access objects fields directly to display them. Storing them in global variables is not necessary.
        "Upcount:   %(counter.upCount)i"
       
label end:
    "Bye"
    return # Explicit end of the game.

I think your greatest problem is still understanding how names work.

_________________
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase II


Top
 Profile Send private message  
 
PostPosted: Sat May 12, 2012 1:34 pm 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
It works, thanks!

I think I've figure out what I need to do with my jumbled codes now. Commencing complete overhaul :D

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Sun May 13, 2012 12:32 am 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
Sorry again for the double post, vut I've got another question:

Suppose that in the example above, I have multiple instances of the CounterTest class, say counter1, counter2, ...countern, and wish to do the same with all of them, do I have to repeat this part
Code:
label start:
 $ counter = CounterTest("Testcounter",3,0) # Variables should start lowercase to separate them from classes.
 while counter.downCount > 0: # The display loop is best done in Ren'Py instead of python code.
     $ counter.count() # That's a method call, so the code in count is executed.
     "Downcount: %(counter.downCount)i" # We can access objects fields directly to display them. Storing them in global variables is not necessary.
     "Upcount: %(counter.upCount)i"


for each of these instances, or is there a way to condense them together?

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Sun May 13, 2012 2:23 am 
Veteran
User avatar

Joined: Wed Nov 18, 2009 11:17 am
Posts: 359
Location: Germany
Completed: Loren
Projects: PS2
You put all your CounterTest instances in a list and then iterate over it:

Code:
label start:
    $ counter = [] # That's an empty list.
    $ counter.append(CounterTest("Testcounter A",3,0)) # Adds a new element to the end of a list.
    $ counter.append(CounterTest("Testcounter B",5,0))
    for entry in counter: # A for loop goes over a collection like a list and assigns one element to the loop variable in turn until it reaches the end of the collection.
        while entry.downCount > 0: # Instead of accessing counter directly, we now use the loop variable.
            $ entry.count() # The loop variable can be freely named, but is not unique to the loop. So be careful what name you use, you might overwrite a different variable.
            "%(entry.name)s Downcount: %(entry.downCount)i" # Adding the name to show how the for loop works.
            "%(entry.name)s Upcount: %(entry.upCount)i"

_________________
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase II


Top
 Profile Send private message  
 
PostPosted: Sun May 13, 2012 1:31 pm 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
If we have already defined the instances before, do we have to define it again?

As in, is this a valid structure?

Code:
label start:
   $ counter = []
   $ test_counter_a = CounterTest("Testcounter A",3,0)
   $ test_counter_b = CounterTest("Testcounter B",5,0)
   $ counter.append(test_counter_a)
   $ counter.append(test_counter_b)
   for entry in counter:
       while entry.downCount > 0:
       $ entry.count()
      "%(entry.name)s Downcount: %(entry.downCount)i"
      "%(entry.name)s Upcount: %(entry.upCount)i"

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Sun May 13, 2012 1:38 pm 
Veteran
User avatar

Joined: Wed Nov 18, 2009 11:17 am
Posts: 359
Location: Germany
Completed: Loren
Projects: PS2
Yes that's how it's done. Please remember though that the list will have the object not the variable.
Say you have code like this:
Code:
label start:
 $ counter = []
 $ test_counter_a = CounterTest("Testcounter A",3,0)
 $ test_counter_b = CounterTest("Testcounter B",5,0)
 $ counter.append(test_counter_a)
 $ counter.append(test_counter_b)
 $ test_counter_a = CounterTest("Testcounter C",7,0)
 for entry in counter:
  while entry.downCount > 0:
   $ entry.count()
   "%(entry.name)s Downcount: %(entry.downCount)i"
   "%(entry.name)s Upcount: %(entry.upCount)i"

The list will still reference Testcounter A instead of Testcounter C. Just something to keep in mind.

_________________
Avatar created with this deviation by Crysa
Currently working on:
  • Winterwolves "Planet Stronghold 2" - RPG Framework: Phase II


Top
 Profile Send private message  
 
PostPosted: Sun May 13, 2012 1:42 pm 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
Right, I understand. Thanks again!

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Sun May 13, 2012 2:35 pm 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
Strange... When I tried to simulate your example, the editor returns an "expected statement" error. I tried to copy-paste your code wholesale, and the same thing happens. What could have been wrong here?

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Mon May 14, 2012 9:39 am 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
To clarify a bit, here's what I have after piecing the codes in the last few posts together:

Code:
init python:
    class CounterTest:
        def __init__(self, pName, pN, pM):
            self.name = pName
            self.downCount = pN +1
            self.upcount = pM - 1

        def count(self):
            if self.downcount > 0:
                self.upcount += 1
                self.downcount -= 1
 
label start:
    $ counter = []
    $ counter.append(CounterTest("Testcounter A",3,0))
    $ counter.append(CounterTest("Testcounter B",5,0))
    for entry in counter:
        while entry.downCount > 0:
            $ entry.count()
            "%(entry.name)s Downcount: %(entry.downCount)i"
            "%(entry.name)s Upcount: %(entry.upCount)i"
           
label end:
    "Bye"
    return


The editor returns an "expected statement" error at the "for entry in counter" line, and I'm not entirely sure what caused this.

Could anyone help?

(EDIT: Wrong code)

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Mon May 14, 2012 11:00 am 
Regular

Joined: Tue Mar 22, 2011 2:36 pm
Posts: 72
You need a python: before the "for...in" statment
Code:
    python:
        for entry in counter:
            while entry.downCount > 0:
                entry.count()
    "[entry.name] Downcount: [entry.downCount]"
    "[entry.name] Upcount: [entry.upCount]"

_________________
Image
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~


Top
 Profile Send private message  
 
PostPosted: Mon May 14, 2012 11:25 am 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
That works, thanks!

Oh, and BTW, I just realized that this part of the code a few posts above does not work:

Code:
"Downcount: %(counter.downCount)i"


The editor returns a "TypeError: %d format: a number is required, not unicode" error message. What should I do about this?

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Tue May 15, 2012 1:43 am 
Regular

Joined: Tue Mar 22, 2011 2:36 pm
Posts: 72
%(counter.downCount)d maybe?


Why don't you try "Downcount: [counter.downCount]" ?

_________________
Image
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~


Top
 Profile Send private message  
 
PostPosted: Tue May 15, 2012 8:08 am 
Veteran

Joined: Sat Feb 26, 2011 9:36 am
Posts: 264
Location: Κωνσταντινούπολη, Βασιλεύα των Ρωμαύων
Projects: Rzeczpospolita Polska 1647
It doesn't work as well.

A less practical method is to create a number of global variables and stuff them in, but surely there should be some better way than that, yes?

_________________
One Province Minor - 120 class variables and still counting!

Because there is no such thing as too many variables.


Top
 Profile Send private message  
 
PostPosted: Tue May 15, 2012 8:29 am 
Regular

Joined: Tue Mar 22, 2011 2:36 pm
Posts: 72
The "Downcount: [counter.downCount]" works well on my side.
The following code already corrected some mistake to make it work.
Code:
init python:
    class CounterTest:
        def __init__(self, pName, pN, pM):
            self.name = pName
            self.downCount = pN +1
            self.upCount = pM - 1

        def count(self):
            if self.downCount > 0:
                self.upCount += 1
                self.downCount -= 1
 
label start:
    $ counter = CounterTest("Testcounter",3,0)
    while counter.downCount > 0:
        $ counter.count()
        "Downcount: [counter.downCount]"
        "Upcount:   [counter.upCount]"
       
label end:
    "Bye"
    return



and your code, Just modify abit to make it easy to look...
Code:
init python:
    style.default.size = 14
   
    class CounterTest:
        def __init__(self, pName, pN, pM):
            self.name = pName
            self.downCount = pN +1
            self.upCount = pM - 1

        def count(self):
            if self.downCount > 0:
                self.upCount += 1
                self.downCount -= 1
 
label start:
    $ counter = []
    $ counter.append(CounterTest("Testcounter A",3,0))
    $ counter.append(CounterTest("Testcounter B",5,0))
    $ui.hbox()
    $ui.vbox()
    python:
        for entry in counter:
            while entry.downCount > 0:
                entry.count()
                ui.text("[entry.name] Downcount: [entry.downCount]")
                ui.text("[entry.name] Upcount: [entry.upCount]")
            if entry.downCount == 0:
                ui.close()
                ui.vbox(xpos=0.5)
    $ui.close()
    $ui.close()
           
label end:
    "Bye"
    return

_________________
Image
.Traditional Chinese Site.
~Illustrator+Graphic/Web Designer~


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

All times are UTC - 5 hours [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Google [Bot]


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
You cannot post attachments in this forum

Search for:
Protected by Anti-Spam ACP
Powered by phpBB® Forum Software © phpBB Group