Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Poll

What programming topic would you want the next challenge to be about?  (It might be a good opportunity to focus on a subject you're not familiar with or to reinforce knowledge on one that you already know)

Control Flow
- 2 (2.2%)
Arrays, Strings, Pointers, and References
- 8 (9%)
Functions
- 4 (4.5%)
Basic object-oriented programming
- 30 (33.7%)
A bit more advanced OOP (Composition, Operator overloading, Inheritance, Virtual Functions)
- 18 (20.2%)
Templates
- 8 (9%)
Other (Explain)
- 4 (4.5%)
Working with files?  (Streams)
- 15 (16.9%)

Total Members Voted: 89


Pages: 1 ... 69 70 [71] 72 73 ... 78

Author Topic: Programming Challenges & Resources (#bay12prog) Initiative  (Read 95683 times)

Levi

  • Bay Watcher
  • Is a fish.
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1050 on: March 14, 2011, 12:09:19 pm »

I found this example online, but I can't verify as I'm still getting the hang of python.

a = "MyClass"
y = eval(a)()

The comments after this were all "OMG, eval is evil don't use it", so try at your own risk?  :)


Logged
Avid Gamer | Goldfish Enthusiast | Canadian | Professional Layabout

Frajic

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1051 on: March 14, 2011, 12:47:33 pm »

Pyrate here with a crash course on classes in Python!

A class is sort of like a blueprint. It's used to make objects starting with a certain amount of attributes(variables within an object) and a certain set of methods(functions within an object).
To make a class, you use:
Code: [Select]
class Testclass:followed by the __init__ method(which gets ran as soon as you use the class to make an object):
Code: [Select]
    def __init__(self, x, y):Here, 'self' is, well, the object itself, and 'x' and 'y' are the parameters used in object creation. Before we move on, I'd just like to show how the parameters work when you create an object:
Code: [Select]
testobject = Testclass(1, 2)So when you put in the parameters to make an object, you're entering the parameters for the __init__ function. Note that the 'self' attribute is ignored, so you only need to fill in the parameters after it. This goes for all methods within a class. Now, back to the __init__ method...
Code: [Select]
        self.x = x
        self.y = y
Here the attributes are assigned, and you can use them by entering something like 'testobject.y'.
Now, methods. They're functions you create within a class, and are made just like you would otherwise, but usually you include the object itself with it, by entering 'self' as an attribute:
Code: [Select]
    def testmethod(self):
        print self.x + self.y
And you use the method by entering:
Code: [Select]
testobject.testmethod()That covers most of the class-related things. However, it doesn't end once you've written the class. You can assign methods and variables after you've made the object:
Code: [Select]
testobject.z = 13
testobject.secondtestmethod = examplefunction
Or you can even assign new methods and edit the class!
Code: [Select]
Testclass.__init__ = newinit
Testclass.thirdtestmethod = anotherfunction
But what if, say, you put an object within an object? Then you access them like you would with any other attribute, and then do what you'd normally do:
Code: [Select]
print testobject.lowertestobject.x
There. That should cover what is to cover on classes. Hope I didn't cram the info too tightly.

As for assigning strings as variable names... doesn't work, and you don't need it. Just make the class have a 'name' attribute.
« Last Edit: March 14, 2011, 01:32:21 pm by Pyrate »
Logged
EoS company name: Vikings Inc.

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1052 on: March 14, 2011, 12:49:34 pm »

in C++:
which compiler do you guys prefer?
what is the best way to make a class that I will access from another language (a .dll library)?
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

Frajic

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1053 on: March 14, 2011, 12:50:23 pm »

I like Bloodshed Dev-C++ myself, but then again, I've only dabbled with C++.
Logged
EoS company name: Vikings Inc.

lordnincompoop

  • Bay Watcher
  • Allusionist
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1054 on: March 14, 2011, 01:56:13 pm »

in C++:
which compiler do you guys prefer?
what is the best way to make a class that I will access from another language (a .dll library)?

Do you mean IDEs, or compilers?
I much prefer VS2010 if you mean IDEs. As for compilers, I don't know enough about them to make a decision.

I have no idea how you would do that.
Logged

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1055 on: March 14, 2011, 01:58:46 pm »

yeah ide's
well Visual C++ is kinda annoying in that it's managed...
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1056 on: March 14, 2011, 04:14:20 pm »

Alright, I'm totally stumped, been at this for hours.

I'm on Python 2.41.

What I'm trying to do: I've set it up so players enter a name for their character. I want these custom names to then be used as the names of classes that hold all their character data. (Creating unique player instances from a racial class file.) This way I can export the class as a file using the character names and not some generic file name I have to assign to it.

My problem: I cannot find a way to have a variable be used as a class name (or be read as input for a lot of other things.) I first create a function with raw_input, and assign the returned raw input a variable name. But when I try to do:

Name_Variable = Human() (which is an instance of the class "Creature")

I get a class named Name_Variable instead of, say, Trogdor. (The name the player entered.) 

I know the function is properly returning the value to 'Name_Variable', because everywhere else within the code it correctly reports its value.

I obviously still don't get some fundamental logic of Python, or even if I'm trying to create the right data structure for my goals, but my questions are:

How do I create a class whose name is generated from player input? Can variables not be used at all for class names?
I think you're looking for this (second page has an example of using metaclasses to create classes at runtime)? Note that metaclasses are a fairly advanced topic, so you'll probably need some familiarity with the concepts behind object orientation.
« Last Edit: March 14, 2011, 04:19:31 pm by Virex »
Logged

eerr

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1057 on: March 14, 2011, 04:35:45 pm »

"This way I can export the class as a file using the character names and not some generic file name I have to assign to it. "
Generic file name you have to assign to it?

It sounds like you're doing something backwards.

Just give it the name when you save it.
Logged

olemars

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1058 on: March 14, 2011, 06:22:50 pm »

in C++:
which compiler do you guys prefer?
what is the best way to make a class that I will access from another language (a .dll library)?

Although I'm all for linux and open source and all that, I must say I much prefer visual studio over any other environment. The biggest upside is perhaps the debugger. Gdb doesn't compare at all.

As for your second question, the interface for accessing C++ objects tends to vary from language to language, but it's usually straight forward enough.
Logged

nenjin

  • Bay Watcher
  • Inscrubtable Exhortations of the Soul
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1059 on: March 14, 2011, 07:26:17 pm »

Quote
I think you're looking for this (second page has an example of using metaclasses to create classes at runtime)? Note that metaclasses are a fairly advanced topic, so you'll probably need some familiarity with the concepts behind object orientation.

Thanks, I think this is the beginnings of what I was looking for.

Spoiler (click to show/hide)
« Last Edit: March 14, 2011, 10:15:35 pm by nenjin »
Logged
Cautivo del Milagro seamos, Penitente.
Quote from: Viktor Frankl
When we are no longer able to change a situation, we are challenged to change ourselves.
Quote from: Sindain
Its kinda silly to complain that a friendly NPC isn't a well designed boss fight.
Quote from: Eric Blank
How will I cheese now assholes?
Quote from: MrRoboto75
Always spaghetti, never forghetti

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1060 on: March 14, 2011, 09:23:52 pm »

yeah ide's
well Visual C++ is kinda annoying in that it's managed...
What do you mean? You're saying that using c++ in VS2010 means you have to go for managed c++?  I must have missed that the last 2 years... O_o
Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.

Normandy

  • Bay Watcher
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1061 on: March 14, 2011, 09:39:30 pm »

yeah ide's
well Visual C++ is kinda annoying in that it's managed...
What do you mean? You're saying that using c++ in VS2010 means you have to go for managed c++?  I must have missed that the last 2 years... O_o
Totally incorrect on multiple levels, FYI.
Logged

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1062 on: March 15, 2011, 09:44:57 am »

in C++:
which compiler do you guys prefer?
what is the best way to make a class that I will access from another language (a .dll library)?

Although I'm all for linux and open source and all that, I must say I much prefer visual studio over any other environment. The biggest upside is perhaps the debugger. Gdb doesn't compare at all.

As for your second question, the interface for accessing C++ objects tends to vary from language to language, but it's usually straight forward enough.

Unless I'm remembering wrong, VS has a project template (or whatever they're called for DLLs). Never used it, but it should make things pretty simple.

Which OS are you using TolyK? I'd assume Windows since you mentioned DLLs. The consensus seems to be "VS > all".

If you've got an email address in a .edu domain (e.g. yourname@yourschool.edu) you can get a legit, full, free copy of VS Professional (08 or 2010) from Microsoft's Dreamspark. A couple other cool bits available from there too.

Personally, I run Linux and use vim and GCC 8), but I honestly wouldn't recommend my setup to anyone: if you're asking, you're probably looking for something easier than command-line.
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

TolyK

  • Bay Watcher
  • Nowan Ilfideme
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1063 on: March 15, 2011, 09:57:20 am »

yeah ide's
well Visual C++ is kinda annoying in that it's managed...
What do you mean? You're saying that using c++ in VS2010 means you have to go for managed c++?  I must have missed that the last 2 years... O_o
Totally incorrect on multiple levels, FYI.
not always, but iirc VC++ code is not completely compliant to the C++ standards, and it has lots of stuff other IDE's don't have (plus some parts have a different command etc.)

in C++:
which compiler do you guys prefer?
what is the best way to make a class that I will access from another language (a .dll library)?

Although I'm all for linux and open source and all that, I must say I much prefer visual studio over any other environment. The biggest upside is perhaps the debugger. Gdb doesn't compare at all.

As for your second question, the interface for accessing C++ objects tends to vary from language to language, but it's usually straight forward enough.

Unless I'm remembering wrong, VS has a project template (or whatever they're called for DLLs). Never used it, but it should make things pretty simple.

Which OS are you using TolyK? I'd assume Windows since you mentioned DLLs. The consensus seems to be "VS > all".
I run XP (work) and Ubuntu (web and play), code::blocks on both and VB/VC++ 2008 (compatability) on XP.

Quote
If you've got an email address in a .edu domain (e.g. yourname@yourschool.edu) you can get a legit, full, free copy of VS Professional (08 or 2010) from Microsoft's Dreamspark. A couple other cool bits available from there too.
I already got most of it. Multipoint is interesting, haven't worked in it yet.

Quote
Personally, I run Linux and use vim and GCC 8), but I honestly wouldn't recommend my setup to anyone: if you're asking, you're probably looking for something easier than command-line.
ow, yeah command-line i don't like :P
I'll probably go with VS for my windows-only apps.
what's the best IDE for linux though?
(I use GCC also)
Logged
My Mafia Stats
just do whatevery tolyK and blame it as a bastard mod
Shakerag: Who are you personally suspicious of?
At this point?  TolyK.

Siquo

  • Bay Watcher
  • Procedurally generated
    • View Profile
Re: Programming Challenges & Resources (#bay12prog) Initiative
« Reply #1064 on: March 15, 2011, 09:58:11 am »

The only other popular vote around here is Code::Blocks+MinGW/GCC. Some people still use bloodshed/devcpp but that's not being maintained anymore, and I don't know anyone who uses eclipse for cpp (I tried and honestly couldn't get it to work right).
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))
Pages: 1 ... 69 70 [71] 72 73 ... 78