Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 262 263 [264] 265 266 ... 796

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

Andrew425

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3945 on: February 17, 2013, 07:27:49 pm »

Thanks guys for the help. I couldn't figure out your way but I managed to make mine work.
Logged
May the mass times acceleration be with you

fergus

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3946 on: February 17, 2013, 09:13:37 pm »

Python trouble again D:
(please forgive my non-existent knowledge about how to call everything :P)

I have 'game' which is an (also the only) instance of the Game class, and contains all default variables and other thins like the current year.
I also have a Person class, and a bunch of instances of that (people). All of these instances also contain a reference to 'game'.

Now when I do this (in an instance of a Person class):
Code: [Select]
self.age = self.game.personAge # personAge is the default age of a newly created personself.age just refers to the value in the game class. It is not 'independent', and thus when anyone ages, they all age Dx, including the default starting age itself. Any idea how to fix this?
It sounds like your changing a mutable object.
Is game.personAge an integer? How are you increasing self.age?
Logged
BY THE GODS! THIS QUOTE MADE MY SIG BOX HAVE A SCROLL BAR! HAPPY DAYS INDEED!
BY THE GODS! YOU HAVE TOO MANY SIGS!

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3947 on: February 18, 2013, 03:24:56 am »

Why are you setting age first in your game class rather than in your person class?
Because I thought it'd be good to have all the default variables in one place, so I wouldn't lose track of them.
It sounds like your changing a mutable object.
Is game.personAge an integer? How are you increasing self.age?
It is a list of two integers. And I increase the second integer once for every person every month.

Considering nobody seems to know how to fix this (and 'fixing' it obviously isn't the best thing to do here), I'll just do what everybody seems to want me to do and move the default values of Person to Person.py
« Last Edit: February 18, 2013, 03:46:04 am by Dutchling »
Logged

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3948 on: February 18, 2013, 05:16:36 am »

It is a list of two integers. And I increase the second integer once for every person every month.

Considering nobody seems to know how to fix this (and 'fixing' it obviously isn't the best thing to do here), I'll just do what everybody seems to want me to do and move the default values of Person to Person.py


Ah, a list.
Try:

Code: [Select]
self.age = list(self.game.personAge) # personAge is a copy of the default age of a newly created person

to copy the list
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3949 on: February 18, 2013, 07:37:59 am »

Thanks. I actually did it as everybody suggested (I got it working now) but I was curious how to use variables defined in classes.
Logged

fergus

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3950 on: February 18, 2013, 07:46:50 am »

Why are you setting age first in your game class rather than in your person class?
Because I thought it'd be good to have all the default variables in one place, so I wouldn't lose track of them.
It sounds like your changing a mutable object.
Is game.personAge an integer? How are you increasing self.age?
It is a list of two integers. And I increase the second integer once for every person every month.

Considering nobody seems to know how to fix this (and 'fixing' it obviously isn't the best thing to do here), I'll just do what everybody seems to want me to do and move the default values of Person to Person.py
When you do
Code: [Select]
self.age = self.game.personAge # personAge is the default age of a newly created personyour taking a copy of a reference to a list, not a copy of the list itself, so when you increment the second value, everyone is looking at the same list and getting the incremented version. Instead you should either make self.game.personAge a tuple, which can't be changed in-place, or do:
Code: [Select]
self.age = copy.copy(self.game.personAge)which will make a separate copy of the list.
Logged
BY THE GODS! THIS QUOTE MADE MY SIG BOX HAVE A SCROLL BAR! HAPPY DAYS INDEED!
BY THE GODS! YOU HAVE TOO MANY SIGS!

olemars

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3951 on: February 18, 2013, 10:31:40 am »

People like to complain that pointers and references in C++ are somehow complicated, but at least you can know they're there and what to expect from them.
Logged

SethCreiyd

  • Bay Watcher
  • [VESPERTINE]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3952 on: February 18, 2013, 12:22:41 pm »

It is a list of two integers. And I increase the second integer once for every person every month.

Considering nobody seems to know how to fix this (and 'fixing' it obviously isn't the best thing to do here), I'll just do what everybody seems to want me to do and move the default values of Person to Person.py
When you do
Code: [Select]
self.age = self.game.personAge # personAge is the default age of a newly created personyour taking a copy of a reference to a list, not a copy of the list itself, so when you increment the second value, everyone is looking at the same list and getting the incremented version. Instead you should either make self.game.personAge a tuple, which can't be changed in-place, or do:
Code: [Select]
self.age = copy.copy(self.game.personAge)which will make a separate copy of the list.


You could also use index slicing, the list built-in, or a comprehension (which would be a bit overkill for this):

Code: [Select]
self.age = self.game.personAge[:]
self.age = list(self.game.personAge)
self.age = [n for n in self.game.personAge]

You might prefer using two separate attributes here, rather than storing a list of two integers, unless there's a good reason to keep those values in a sequence.  You could still use multiple assignment with a sequence in the initialization:

Code: [Select]
age_values = [100,1]
self.maxAge, self.currentAge = age_values
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3953 on: February 18, 2013, 12:48:49 pm »

Thanks. Didn't know the comprehension thing. Might prove useful later on.

The list is actually [<year>, <month>]. Don't really see a point in making those two values, unless you have some good reason for it :P
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #3954 on: February 18, 2013, 12:59:36 pm »

People like to complain that pointers and references in C++ are somehow complicated, but at least you can know they're there and what to expect from them.
It's nice to know that if something goes wrong, it's always my fault , not how the high-level feature implements my commands xD
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

alexandertnt

  • Bay Watcher
  • (map 'list (lambda (post) (+ post awesome)) posts)
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3955 on: February 18, 2013, 07:23:50 pm »

People like to complain that pointers and references in C++ are somehow complicated, but at least you can know they're there and what to expect from them.

You know what to expect from them - they just work somewhat differently to C/C++'s pointers. Plus no seg-faults :)

The list is actually [<year>, <month>]. Don't really see a point in making those two values, unless you have some good reason for it :P

I would suggest making a sepeate date class alltogether. It would allow for easy expansion in the future if you wanted to make the date system more complicated (adding day, minutes etc to improve accuracy would have you modifying the date class, rather than a person class with lots of other unrelated-to-date stuff) and it would allow you to give other non-person items a date without having to copy and paste anything if you needed to.

Its totally fine to make seemingly-trivial several-line-long classes, as long as it makes sense.
Logged
This is when I imagine the hilarity which may happen if certain things are glichy. Such as targeting your own body parts to eat.

You eat your own head
YOU HAVE BEEN STRUCK DOWN!

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3956 on: February 19, 2013, 05:50:14 am »

I'm not sure why replacing a variable with a class containing a variable would be useful.

Might as well assume there is a good reason for it and do it anyway :P
« Last Edit: February 19, 2013, 05:51:58 am by Dutchling »
Logged

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3957 on: February 19, 2013, 05:52:44 am »

I'm not sure why replacing a variable with a class containing a variable would be useful.
I can think of many uses...
Making your own casting, operator behavior, caching, event listeners, polymorphism, the possibilities are ENDLESS!
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))

Twiggie

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3958 on: February 19, 2013, 06:17:41 am »

Basically its gonna make things a hell of a lot easier when you want to change your code. Instead of combing though your code looking for every instance of these integers, you'll be able to update the whole functionality from one file (assuming your interface doesn't need to change)
Logged

Dutchling

  • Bay Watcher
  • Ridin' with Biden
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #3959 on: February 19, 2013, 06:18:34 am »

I have no idea what you (Siquo) are talking about :P

Anyway, how do you create new classes inside of a class? I tried to do it like I do it in a function (which works) but I get this:
Code: [Select]
File "C:\<~~>\person.py", line 13, in __init__
    self.age = time.Time(1, 20)
AttributeError: 'module' object has no attribute 'Time'
I can move this to the function which creates the person class, but I'd prefer to do it in __init__.
Logged
Pages: 1 ... 262 263 [264] 265 266 ... 796