Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: OOP in non-OOP programming languages  (Read 993 times)

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
OOP in non-OOP programming languages
« on: October 17, 2009, 11:26:54 am »

Along with my ever-changing set of projects that I am currently interested in continuing the development of is a recent adddition of a shmup. Considering that I almost exclusively write in C, and this was one idea that would benefit from some sort of class, I ended up(for now?) writing everything as an object struct with an optional pointer to a data struct(void*), and a data type var to differentiate it's type. Is it a good idea to try something crazy like this, merging OOP into a non-OOP language?
Logged
Eh?
Eh!

DJ

  • Bay Watcher
    • View Profile
Re: OOP in non-OOP programming languages
« Reply #1 on: October 17, 2009, 11:29:21 am »

Why would you reinvent the wheel when the existing wheel is perfectly functional?

Just move your project to C++. You won't lose any code, and you can mix C and C++ (not that it's a very elegant thing to do).
Logged
Urist, President has immigrated to your fortress!
Urist, President mandates the Dwarven Bill of Rights.

Cue magma.
Ah, the Magma Carta...

MrLobster

  • Bay Watcher
    • View Profile
Re: OOP in non-OOP programming languages
« Reply #2 on: October 17, 2009, 11:55:47 am »

It depends on how much OOP you really want. Tricks can be done with C structs that allow you some flexibility with your data (interfaces, variable type) that fall below the threshold of what you would need for OOP. Sometimes all you really want is just one table of function pointers in one place, or a single "object" struct that can be dynamically sniffed for its type and cast to some other struct, but you want the rest of your code arranged in a more procedural way.

It's very often easier to start with something familiar and and build up, rather than have to dumb down some complex thing. LISP/Scheme don't really need OOP because it's pretty easy for the programmer to implement as much of it as they want. C is less great at it, but it can be built high if you make sensible divisions about your interfaces. If you're not good at knowing where to put the interfaces, C++ won't prevent you from shooting yourself in the foot, but it will help you limp around afterwards pretending everything is okay while you bleed out.

In the worst case you will start to run into problems where the code you are writing to handle your souped-up structs is eating a lot of your time, and at that point you switch to an OOP language... but actually this is one of the best ways to learn OOP, and if you then move to C++ and re-implement you will probably end up with a better project in a shorter time than if you'd just started out with C++ and made a bunch of bad first assumptions when designing the object hierarchy.
Logged

Eidalac

  • Bay Watcher
  • Ecchi Inside
    • View Profile
    • Facebook
Re: OOP in non-OOP programming languages
« Reply #3 on: October 17, 2009, 03:18:54 pm »

Eh, it all depends.

Languages like C let you do some cool tricks that more advanced languages stop since it's easy to go FUBAR with them.

Conversely, things like C++ and Java make it easier to group/organize things, and do manage a good amount of idiot proofing, but loose some flexibility for it.

It's not that hard to do OOP in C, but the bulk of the work is on your shoulders vs C++ which takes care of it for you.  So long as you take care of the code properly, there is no issue using OOP methods in something like C.

In a sense, C++ is a form of C with OOP structs built in anyway, so you won't be doing anything that wasn't done to make C++, but as MrLobster said, you'll learn more about the systems if you work in your own OOP than if you just jump to an OOP language.

I once took a machine-language class, and I can tell you, I learned more by putting out code to swap 2 numbers at that level than most of the Java classes I was taking that year.
Logged
is he okay?
In the traditional sense of the word?  No, he's been dissolved in magma.

Armok

  • Bay Watcher
  • God of Blood
    • View Profile
Re: OOP in non-OOP programming languages
« Reply #4 on: October 17, 2009, 04:13:43 pm »

Yup, assemblers for the win! ^_^

And among those, Linoleum.
Logged
So says Armok, God of blood.
Sszsszssoo...
Sszsszssaaayysss...
III...

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: OOP in non-OOP programming languages
« Reply #5 on: October 17, 2009, 04:32:23 pm »

As it is, here is what I have done for the structs. Still need to flesh out how graphics will work, and actually code object logic(Even creating stuff, etc.)

As it it, the player will be a specialized AI player type that reads it's actions from the current input data or something, and is a special case for when it "dies". Or at least that is what I think it will probably be.

Spoiler (click to show/hide)

Post-Armok-post appended:
Yes, I know of that. However, I HAVE seen it, and unless it has an extremely powerful optimizer, it relies on the programmer to write fast code. In it's speed contrast, it took a non-optimized output of C, setting a variable, compared to programmer-optimized setting an internal register. However, it is likely that the C optimizer would use an internal register for a common variable in a short function. If you were modifying a global variable in linoleum, you would likely have to go through the whole process that it claims to be faster than.

You underestimate the power of the dark side GCC programmers!
Logged
Eh?
Eh!

Mr Tk

  • Bay Watcher
  • Would you like a mint? It's only waffer thin.
    • View Profile
Re: OOP in non-OOP programming languages
« Reply #6 on: October 18, 2009, 06:52:01 pm »

Another way to make OOP in C is to make functions take pointers to your structs

e.g void doSomething(object *someobject)

Which means that these functions can only do something if they have the object that belongs to it. A form of encapsulation if you will.
Logged
First ten minutes of play I ate my loincloth and then got some limbs torn off by a super friendly rat. Thumbs up from me.

eerr

  • Bay Watcher
    • View Profile
Re: OOP in non-OOP programming languages
« Reply #7 on: October 19, 2009, 01:35:46 am »

"Is it a good idea to try something crazy like this, merging OOP into a non-OOP language? "

why do you need Object-oriented variables? it seems to destroy the point of, uh, pointers.
Logged

Dasleah

  • Bay Watcher
    • View Profile
Re: OOP in non-OOP programming languages
« Reply #8 on: October 19, 2009, 06:50:55 am »

Just... rewrite in a language close enough to your existing codebase. OOP is probably the largest shift in thinking in programming in the last 20 years. Stop trying to re-invent the wheel here for no reason other than you've hit a brick wall in your current skill base and don't want to commit to learning something new or different which isn't all that new or different in the first place.
Logged
Pokethulhu Orange: UPDATE 25
The Roguelike Development Megathread.

As well, all the posts i've seen you make are flame posts, barely if at all constructive.

Muz

  • Bay Watcher
    • View Profile
Re: OOP in non-OOP programming languages
« Reply #9 on: October 19, 2009, 08:30:02 am »

Just move your project to C++. You won't lose any code, and you can mix C and C++ (not that it's a very elegant thing to do).

Lol, you're not supposed to do that? No wonder people keep giving my code funny looks.
Logged
Disclaimer: Any sarcasm in my posts will not be mentioned as that would ruin the purpose. It is assumed that the reader is intelligent enough to tell the difference between what is sarcasm and what is not.

eerr

  • Bay Watcher
    • View Profile
Re: OOP in non-OOP programming languages
« Reply #10 on: October 23, 2009, 02:03:02 am »

unless you're trying to do Oop diffrent than other languages, go use those languages.

but if you're trying to do all sorts of fancy stuff, like a garbage collector or something you might just want to do that for yourself.
Logged

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: OOP in non-OOP programming languages
« Reply #11 on: October 25, 2009, 06:09:20 pm »

A "quick" update:

After some development, things are finally coming together.
There are still inefficiencies that should be eliminated, potential bugs lurking, and other unnamed horrors and overlooked menaces, but I am ready to begin collision detection.

For those of you who want to criticize the mess I have made, download.
Logged
Eh?
Eh!