Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 253 254 [255] 256 257 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 884719 times)

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3810 on: February 05, 2013, 09:33:29 am »

I'm not familiar enough with python to know how to do it properly, but one way I have to avoid using globals is to create an object (I call it Game) that contains everything important that you want everyone to have access too.  Then whenever creating an object, I pass in the game object and assign it to self.game.  Eventually every object has access to the game object, which has access to pretty much every thing else. 

So I've got something like:

Code: [Select]
class Game:
  def __init__(self):
    self.dungeon = dungeon.Dungeon(self)
    self.item_manager = item_manager.ItemManager(self)
    self.monster_list = []
    self.whatever = {}

class Monster:
  def __init__(self, game)
    self.game = game
    //blah blah blah

So basically I make one instance of game, and just pass it around to everything.

Where will be this class be? game would be an instance of the game class right? How do you make sure all the other modules have access to this class?
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3811 on: February 05, 2013, 10:30:11 am »

"Yo lawg, I put a lawg in your lawg so you can lawg while you lawg."
*literalheaddesk*

(( my logger was logging all changes, including logs. No wonder I was running out of memory at every turn ))
Logged

This one thread is mine. MIIIIINE!!! And it will remain a happy, friendly, encouraging place, whether you lot like it or not. 
will rena,eme sique to sique sxds-- siquo if sucessufil
(cant spel siqou a. every speling looks wroing (hate this))

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3812 on: February 05, 2013, 11:41:25 am »

I'm not familiar enough with python to know how to do it properly, but one way I have to avoid using globals is to create an object (I call it Game) that contains everything important that you want everyone to have access too.  Then whenever creating an object, I pass in the game object and assign it to self.game.  Eventually every object has access to the game object, which has access to pretty much every thing else. 

So I've got something like:

Code: [Select]
class Game:
  def __init__(self):
    self.dungeon = dungeon.Dungeon(self)
    self.item_manager = item_manager.ItemManager(self)
    self.monster_list = []
    self.whatever = {}

class Monster:
  def __init__(self, game)
    self.game = game
    //blah blah blah

So basically I make one instance of game, and just pass it around to everything.

Where will be this class be? game would be an instance of the game class right? How do you make sure all the other modules have access to this class?

The game object will be created in your main loop.  Whatever objects you create there, just make sure you pass game in.  Then when those objects create classes, they make sure to pass along game into the new objects.


So something like:

Code: [Select]
#main.py:

game = Game()
dungeon = Dungeon(game)
game.dungeon = dungeon

#dungeon.py

class Dungeon:
  def __init__(self, game):
    self.game = game
    self.monster_list = []
    self.monster_list.append(Orc(game))
    self.monster_list.append(Goblin(game))
    self.monster_list.append(Knoll(game))


Um, with the relevant imports, which I temporarily forget the syntax this early in the morning.   :P

There are probably lots of ways to do this.  You can have the game object handle the creation of the dungeon.  You could even set it up so the object that receives the game object to automatically add its own information to the game object.  In which case it would look more like:

Code: [Select]
#main.py:

game = Game()
dungeon = Dungeon(game)

#dungeon.py

class Dungeon:
  def __init__(self, game):
    self.game = game
    self.game.dungeon = self
    self.monster_list = []
    self.monster_list.append(Orc(game))
    self.monster_list.append(Goblin(game))
    self.monster_list.append(Knoll(game))

« Last Edit: February 05, 2013, 11:46:04 am by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3813 on: February 05, 2013, 02:29:48 pm »

I didn't actually fixed I see now... Just removed '()' after a function which made it not work at all >.<


My problem:
Code: [Select]
# classfile.py
class classone:
    def __init__(self):
        <yadayada>

    def one(self):
        if two(self): <== causes the error
            <yadayada>

    def two(self):
        <yadayada>
Code: [Select]
# main.py
import classfile

thing = classfile.classone()
if thing.one():
    <yadayada>
Gives me this error:
Quote from: ERROR
NameError: global name 'two' not defined

Halp D:
I defined it >.<
EDIT2: Spouted bullshit, python doesnt give a shit.

Anyway, asked my brother about python. In function one you have to use self.two(self), rather than just two(self).


two(self) should be self.two().  self.two() basically means "call the method two which belongs to the object self."  two(self) means "call the function two with self as the first argument."

You need to tell Python where two() is coming from.  Right now it assumes the object named two has been declared somewhere outside the current scope.  Normally, something.method() is like calling SomeClass.method(something), where something is an instance of SomeClass.  In a normal method, the first argument (self by convention) is treated as a reference to the caller, and should not be passed explicitly when the method is called.  self.two(self) would raise a TypeError (as it's passing self to self.two twice).

Spoiler: code (click to show/hide)
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3814 on: February 05, 2013, 03:17:03 pm »

Levi, so altering the class in one module will also alters that class in all other modules?
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3815 on: February 05, 2013, 03:34:47 pm »

Levi, so altering the class in one module will also alters that class in all other modules?

Yeah, although we call it an object, which is an instantiated class.

Think of a class as the blueprint, and the object as what you made with the blueprint.  You assign the object to a pointer in memory(the variable).  When you do something like:

Code: [Select]
game = Game() #creates an object in memory from the blueprint class, and then points the "game" variable at it.
blah = game
uberblah = blah

All you are doing is pointing the variables game, blah, uberblah at the same object in memory.  So when you are assigning a variable, you aren't copying the object, you are just pointing to it.  This means you can have 5 million variables(in other objects, in lists, in hashes, whatever) all pointing to a single object, and any change you make to that object is reflected no matter which variable you access it with.

The caveat here is that primitive types(strings, integers, truth values) are usually NOT pointed too.  So if you do the same thing as above but with an integer:


Code: [Select]
game = 7
blah = game
uberblah = blah

You actually end up with 3 copies of the number 7.  Changing one won't change the others.  Its a little confusing, but its mostly done this way for efficiency reasons.  Any object that is created from a class you write will work the first way I described though, so don't worry too much about it.
« Last Edit: February 05, 2013, 03:46:22 pm by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Mego

  • Bay Watcher
  • [PREFSTRING:MADNESS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3816 on: February 05, 2013, 04:10:26 pm »

So what to do when you have a problem ... and it seems like it's a complicated one? >_< Just struggle with it on your own? D:
I think about it in the shower or when trying to sleep. Trick is to remember your solution until you're programming again :)
Also I sometimes try to explain it to my girlfriend so she'll understand, and sometimes the chosen metaphor hands me a new insight.

Rubber Duck Debugging!

Absolute Niro

  • Bay Watcher
  • I HATE DOGS
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3817 on: February 05, 2013, 04:33:35 pm »

Trying to learn C++, started today. I feel I understand the very basics, but I'm having trouble with while loops. The loop doesn't stop even if the player inputs a valid race. I'm not going for efficiency here, I just want to know where I made an error. :P

Code: [Select]
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string Name;
string Race;
string Class;

int main ()
{
cout << "Welcome to GenericQuest!\n";
cout << "What is your name?\n";
getline (cin, Name);
cout << "What race are you?\n";
int Confirm(0);
while (Confirm==0)
getline (cin, Race);
if (Race == "Human")
Confirm = 1;
else if (Race == "Elf")
Confirm = 1;
else if (Race == "Dwarf")
Confirm = 1;

cout << "What class are you?\n";
getline (cin, Class);
cout << "You are the " << Race << " " << Class << " " << Name << "!\n";
system("PAUSE");
return 0;
}
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3818 on: February 05, 2013, 04:34:58 pm »

Trying to learn C++, started today. I feel I understand the very basics, but I'm having trouble with while loops. The loop doesn't stop even if the player inputs a valid race. I'm not going for efficiency here, I just want to know where I made an error. :P

It should probably be:

int Confirm = 0;

not

int Confirm(0);


Edit:  Oh yeah, and what Mephisto said.   :)
« Last Edit: February 05, 2013, 04:38:09 pm by Levi »
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Mephisto

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3819 on: February 05, 2013, 04:36:48 pm »

Trying to learn C++, started today. I feel I understand the very basics, but I'm having trouble with while loops. The loop doesn't stop even if the player inputs a valid race. I'm not going for efficiency here, I just want to know where I made an error. :P

Code: [Select]
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

string Name;
string Race;
string Class;

int main ()
{
cout << "Welcome to GenericQuest!\n";
cout << "What is your name?\n";
getline (cin, Name);
cout << "What race are you?\n";
int Confirm(0);
while (Confirm==0)
getline (cin, Race);
if (Race == "Human")
Confirm = 1;
else if (Race == "Elf")
Confirm = 1;
else if (Race == "Dwarf")
Confirm = 1;

cout << "What class are you?\n";
getline (cin, Class);
cout << "You are the " << Race << " " << Class << " " << Name << "!\n";
system("PAUSE");
return 0;
}

You need braces around the body of your loop. C++ is not Python - the indentation in the loop isn't significant.
Logged

Absolute Niro

  • Bay Watcher
  • I HATE DOGS
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3820 on: February 05, 2013, 04:38:35 pm »

I figured it was something about the braces, it's hard to get used to a new language. :-[

Levi, according to my reference it doesn't make a difference if you use = 0 or (0).
Logged

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3821 on: February 05, 2013, 04:39:13 pm »

I figured it was something about the braces, it's hard to get used to a new language. :-[

Levi, according to my reference it doesn't make a difference if you use = 0 or (0).

Interesting.  I've never seen it done with the (0) before.
Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3822 on: February 05, 2013, 05:05:34 pm »

Code: [Select]
>>> c = 'id1'
>>> game.c
<person.Person object at 0x02AC06B0>
>>> game.id1
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    game.id1
AttributeError: 'Game' object has no attribute 'id1'
>>> game.'id1'
SyntaxError: invalid syntax

This puzzles me
Logged

GlyphGryph

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3823 on: February 05, 2013, 05:24:46 pm »

Is there something about that that should seem puzzling? o_O
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3824 on: February 05, 2013, 05:26:02 pm »

Why is the second (or third) call not the same as the first?
Logged
Pages: 1 ... 253 254 [255] 256 257 ... 796