What is an object?

For an official definition see: http://searchsoa.techtarget.com/definition/object

This sounds like gobblty-gook to a proceedural programmer.

An object is really just a sub-program and group of sub-routines that can be called from the main program.

Yab Being a BASIC language, and therefore to a yab programmer, the idea of objects seems foreign, but as a yab programmer, one uses objects.

All of the GUI bindings in yab are objects.

WINDOW OPEN x1,y1 TO x2,y2, ID$, Title$


This is a call to create a window object, it creates a new window object, gives it its boundries, and identifies it.

WINDOW SET WindowID$, "MoveTo", x,y

Is a call to the window object named WindowID$, this one moves the window object to a new location.

OK, we have established that yab programmers use objects. These are, however part of the yab interpreter. now, lets define and use an object in yab.

Our object will be a di for a game that needs one to roll the dice.

Since objects are often re-used, that is the whole ides of objects, it makes sense to use a yab library file for our object.

First is the class, a definition of what a di object is and what it can do.

a Di:

A di object is usually a representation of a six faced physical object.

The number of faces can be different depending on the need for the particular game, but four is the minimum.

Each face can have a letter, number, word, color, picture, or a number of dots, etc.

A di can:

Show one of the faces.

Be shaken and rolled ( randomly pick another face and show it.)

Be interogated for the number of the face.

now for the code:

name the file di.yab and place it in the yab library folder:

either

/boot/home/config/settings/yab

     (PM Haiku)

or/boot/home/cinfig/lib/yab

     ( pre PM haiku)

 

//______________________________________________
//copy the lines below for the di object (di.yab)
//______________________________________________

doc an object programmed in yab
doc to demonstrate the idea of programming an
doc object in a proceedural language.
doc
doc by Jim Saxton, April 10, 2014, MIT license

// we need a subroutine to make a new di object
// num, field() and field$() do not need to be known by the calling program.

export sub di_new$(sides, id$,x,y, view$)

    static num
    if num = 0 then
        bitmap 680,680, "Di"
        err = draw image 0,0 to 675,450, "/boot/home/Dice.png", "Di"
    end if

    num=num+1

    dim field$(num,2) // id$, view$
    dim field (num, 3) // sides,x,y

    // set the default number of sides     if sides < 4 sides = 6
        field$ (num,1)=id$
        field$ (num,2)=view$
        field (num,1)=sides
        field (num,1)=sides
        field (num,2)=x
        field (num,3)=y
        VIEW x,y TO x+225,y+225, id$, view$
        for n=1 to field(num,1)
            switch n
                case 1
                    x=0:y=0
                    break
                case 2
                    x=225:y=0
                    break
                case 3
                    x=450:y=0
                    break
                case 4
                    x=0:y=225
                    break
                case 5
                    x=225:y=225
                    break
                case 6
                    x=450: y=225
                    break

    // This may be modified to allow for more sides than 6.
    // If so, the image file and Di bitmap need to be changed as well.
    //
    // The bitmap needs to be larger than the image or the bitmap get will
    // error with "out of bounds" once your di face is at an edge of the image.

            end switch

            bitmap get x,y to x+225, y+225,str$(n),"Di"
    next

    // seed the random generator
    x1 = ran(num+1)

    // roll the di and chose a side
    field (num,0) = int(ran(sides))+1

    //The info for the new di object is saved, return "ok"
    return "ok"
end sub

//________________________________________________

export sub di_get_face(id$)
    s=arraysize(field$(),1)
    for x=1 to s
        if (field$(x,1)=id$) num = x
    next
    return field(num,0)
end sub

//________________________________________________

export sub di_show_face(id$)
    n = di_get_face(id$)
    DRAW BITMAP 0,0, str$(n), "copy", id$
end sub

//________________________________________________

export sub di_roll(id$)
    s=arraysize(field$(),1)
    for x=1 to s
        if (field$(x,1)=id$) num = x
    next
    field (num,0) = int(ran(field(num,1)))+1
    di_show_face(id$)
    return
end sub

//________________________________________________

This object was set-up for a six-sided di, but could be easilly expanded to all sorts of di, say for D&D, etc.

The object relies on a hard-coded path to /boot/home/Dice.png. This could deasilly be changed to use a path relative to the program location.

I used http://upload.wikimedia.org/wikipedia/commons/4/4c/Dice.png and slightly re-sized it to 675 X 450 for the dice face image.

each di face is 225 X 225

To test the object:

Copy the lines below into "Roll-Dice.yab"

#!yab

import di

doc a test program
doc to test the di.yab object.
doc It never uses one of the exported subs, di_get_face,
doc but di_show_face does.
doc
doc Jim Saxton, April 10, 2014, MIT license

// set DEBUG = 1 to print out all messages on the console
DEBUG = 0
sleep .01

OpenWindow()

di_show_face("first")
di_show_face("second")

// Main Message Loop
dim msg$(1)
    while(not leavingLoop)
        nCommands = token(message$, msg$(), "|")

        for everyCommand = 1 to nCommands
            if(DEBUG and msg$(everyCommand)<>"") print msg$(everyCommand)

            switch(msg$(everyCommand))
                case "roll":
                    di_roll("first")
                    di_roll("second")
                    break
                case "_QuitRequested":
                case "MainWindow:_QuitRequested":
                    leavingLoop = true
                    break
                default:
                    break
            end switch

    next everyCommand
wend

CloseWindow()

end

// Setup the main window here
sub OpenWindow()
    window open 100,100 to 570,365, "MainWindow", "Main Window"
        di_new$(6,"first",10,10,"MainWindow")
        di_new$(6,"second",235,10,"MainWindow")
        BUTTON 10,238 TO 460,263, "roll", "R O L L", "MainWindow"
        return
end sub

// Close down the main window
sub CloseWindow()
    window close "MainWindow"
    return
end sub


Here you can download the complete code: Archive

Tutorial By Jim Saxton, 10 April 2014 - MIT license
Made available by BeSly, the Haiku, BeOS and Zeta knowledge base.