Does vc support object oriented programming? Why not?

No, vc doesn't have direct support for OO programming. In particular, there are no classes, structs can't have member functions and there is no such thing as inheritance or polymorphism.

This is mostly because those features are complex, and therefor difficult to implement and hard for new users to understand.

You can use vc in an OO style, much as you can with languages like C. For example, if you have a global array of structs, you can treat each one of those as an object, referenced by its index. You can then create functions like NewMyObject() that return references (ints) to unused objects and functions like DoSomething(int obj) that take in indexes into that large global array and do something with them.

Rather than talk even more about it, here's some example code of what I mean:

struct point {
    int x, y;
    int color;
    int in_use;
}
#define MAX_POINTS 100
point points[MAX_POINTS];

// first, here's some code that uses this "class"
// see how it looks kinda OO-like?
// see the functions below for how it works
void autoexec() {
    initPoints();

    int p1 = newPoint(2, 3);
    int p2 = newPoint(4, 5);

    debugPoint(p1);
    debugPoint(p2);

    Log("Distance to origin of p1: " + str( distanceToOrigin(p1)));
    Log("Distance to origin of p2: " + str( distanceToOrigin(p2)));

    // add p2 to p1
    addPoint(p1, p2);

    Log("Distance to origin of p1 + p2: " + str(distanceToOrigin(p1)));
   
    // clean up
    deletePoint(p1);
    deletePoint(p2);

}


// call before using any other methods
void initPoints() {
    int i;
    for(i = 0; i < MAX_POINTS; i++) {
        points[i].in_use = 0;
    }
}

// call to get a new point
int newPoint(int x, int y) {
    // go through all of them, looking for one not in use
    int i;
    for(i = 0; i < MAX_POINTS; i++) {
        if(points[i].in_use == 0) {
            points[i].in_use = 1; // mark as used
            points[i].x = x; // initialize values 
            points[i].y = y;
            return i;
        }
    }
    exit("Out of points!");
}

// call when done with a point
void deletePoint(int p) {
    if(p < 0 || p >= MAX_POINTS) {
        exit("Invalid point in deletePoint.");
    }
    points[p].in_use = 0;
}

// call to find the distance to the origin from this point
int distanceToOrigin(int p) {
    if(p < 0 || p >= MAX_POINTS) {
        exit("Invalid point in distanceToOrigin.");
    }
    return sqrt(pow(points[p].x, 2) + pow(points[p].y, 2));
}

// adds the values in p2 into p1
void addPoint(int p1, int p2) {
    if(p1 < 0 || p1 >= MAX_POINTS) {
        exit("Invalid point in addPoint.");
    }
    if(p2 < 0 || p2 >= MAX_POINTS) {
        exit("Invalid point in addPoint.");
    }
    points[p1].x += points[p2].x;
    points[p1].y += points[p2].y;
}

// prints out the point
void debugPoint(int p) {
    if(p < 0 || p >= MAX_POINTS) {
        exit("Invalid point in debugPoint.");
    }
    Log("(" + str(points[p].x) + ", " + str(points[p].y) + ")");
}
Talkback

Post a new comment?

Talkback #1 written by Sungam on 2004-11-07.

While V3 doesn't support OOP natively, Feyr's ClassBuilder (http://www.verge-rpg.com/files/detail.php?id=545) takes a very good stab at it. There's a tutorial for ClassBuilder here: http://fractured-brilliance.com/code/classbuilder/

Post a new comment?

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.