Python Processing refer to an object from main script

OK, I just started learning Python & I never liked how people had the classes on same page everything else went on. So I placed them on another page, but sadly I don’t know how to actually get to them. I liked one of the tutorials I was doing and tried to make it more. I tried using import sounded good but still running into errors.

here is sketch, the main page that I don’t want the classes on

def setup():
    size(600,600)
    
def draw():
    background(0)
    
def fight(unit1,unit2):
    round = 0;
    if round == 0:
        unit2.health -= unit1.attack
        if unit2.health > 0:
            round = 1;
        else:
            unit2.is_alive = false
    if round == 1:
        unit1.health -= unit2.attack
        return True;
        if unit1.health > 0:
            round = 0;
        else:
            unit1.is_alive = false
            return False;
        
if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing

    chuck = Warrior()
    bruce = Warrior()
    carl = Knight()
    dave = Warrior()
    mark = Warrior()

    assert fight(chuck, bruce) == True
    assert fight(dave, carl) == False
    assert chuck.is_alive == True
    assert bruce.is_alive == False
    assert carl.is_alive == True
    assert dave.is_alive == False
    assert fight(carl, mark) == False
    assert carl.is_alive == False

for knight & warrior I know they short for now but I plan to add more later

here is knight.py

class Knight(Warrior):
    attack = 7

here Warrior

class Warrior:
    health = 50
    is_alive = true;
    attack = 5;
2 Likes

There is an old topic from old processing forum

from Name_of_the_Tab import * (@GoToLoop answer)

you need to save the tab first time to get this work

4 Likes

do I need to import on every tab?

yes. you treated them like modules

1 Like

I tried import here Knight

from Warrior import *
class Knight(Warrior):
    attack = 7

as you can see I placed important in it so he can get Warrior class, but it still says Warrior undefined, guessing it can’t find it

:thinking: for me it just work fine. Try to delete ~$py.class things in the folder and rerun? got it work?
Screenshot%20from%202019-08-09%2017-54-02

1 Like

TY, it worked after I closed and opened it now I need to try and solve the next part after I understand what it wants me to do. TYVM

2 Likes

IMO, the Python Mode’s dev made a bad decision on how to deal w/ multi-tabbing in Processing! :astonished:

In Java Mode, when we create a new tab, by default that tab is of extension “.pde”.

And when we run the sketch, all “.pde” files are concatenated as 1 “.java” file. :heavy_plus_sign:

So there’s no import among “.pde” files, b/c they’re treated as 1 file after all. :relieved:

However, does that mean there’s no way to have some kinda module system under Java Mode? :package:

Actually there is! When we create a new tab, we have the undocumented option to name it w/ a “.java” extension. :open_mouth:

Let’s say we’d name the new tab “Warrior”. That’ll result in a “Warrior.pde” named file. :file_folder:

However, if we name it instead as “Warrior.java”, that’ll result exactly in a “Warrior.java” file. :crossed_swords:

Therefore, it’s not a “.pde” file anymore; and now import is necessary in order for it to be available for the “.pde” files, just like any other Java library! :coffee:

So I wonder why the Python Mode’s dev decided not to follow such simple & flexible scheme… :disappointed:

Instead, he’s forcing all additional tabs to be treated as a Python module rather than letting us decide which 1s we want so! :confounded:

It should just default all extra tabs to be of extension “.pyde”, and concatenate them all as 1 file when we run the sketch, just like Java Mode. :roll_eyes:

Actually, both modes should offer 2 ways to create a new tab: a regular 1 and a module 1. :bulb:

3 Likes