Home > Games development > Using functions in Blitz3D / BlitzPlus

Using functions in Blitz3D / BlitzPlus

I’ve had a request to show how to use functions in blitzbasic from TheLividePianoist, I’ve been slow to answer, so here we go:

simple maths function:

purpose of function is to return double the number you send it:

<code>

x = 2

y = double(x)

print y

waitkey

function double(z)

return z*2

end function

<end code>

The function header: function double(z) just means that the function is called double (you can call functions what ever you like, as long as there’s not already a function called that), and the (z) tells it to expect a variable called z. z could be anything, in this case it’s a number.

when a function exits, it can either just end using the “end function” line, or it can return a value to the part of the program that called it in this case it’s “y=double(x)”, x gets passed to the function, which get’s written in z, which the function doubles and returns to that part of code assigning the return value to y.

Here’s another quick example:

Say you always centre your text, using text x,y,”hello”,1,1

Instead of having to write ,1,1 on the end everytime, why not write a function called ctext that automatically centres your text?

<code>

graphics 800,600,32,2

setbuffer frontbuffer()

ctext 400,300,”This is the centre of the screen!”

waitkey

end

function ctext(x,y,mytext$)

text x,y,mytext$,1,1

end function

<end code>

The function declaration: function ctext(x,y,mytext$) tells the function to expect to be given an x number, a y number and a string of text (hence the $ after mytext):

Then instead of returning a value, we simple call the text function with these values and put ,1,1 on the end to centre the text over the x and y coords chosen.

Hopefully this has cleared things up a bit for you, if you’re still unsure, please let me know.

  1. October 31, 2008 at 7:24 pm

    Hey, thanks man. Nice tut and very understandable. This is exactly what I needed, the second example answers my question better. Keep up the great tuts and good luck on the game.

  2. October 31, 2008 at 7:41 pm

    Uh, could you elaborate though by telling me how to create my own symbol commands. Take for example the + sign adds. I want to know if I can create my own sign and what info I’d need to include for it. Thanks again

  3. September 3, 2009 at 4:26 pm

    Cool site, love the info.

  4. July 20, 2010 at 1:08 pm

    width = 800 ;set the width of screen
    height = 600 ;set height
    bit = 32 ;set the bit rate

    ;assign the width, height and bit
    Graphics width,height,bit,2
    setbuffer frontbuffer()

    ;pass centerText the width & height divided by 2
    centerText width/2,height/2,”This is the real center of the screen!”
    WaitKey
    End

    Function centerText(x,y,mytext$)
    Text x,y,mytext$,1,1
    End Function

  5. July 20, 2010 at 1:13 pm

    width = 800 ;set the width of screen
    height = 600 ;set height
    bit = 32 ;set the bit rate

    ;assign the width, height and bit
    Graphics width,height,bit,2
    setbuffer frontbuffer()

    ;pass centerText the width & height divided by 2
    centerText width,height,”This is the real center of the screen!”
    WaitKey
    End

    Function centerText(x,y,mytext$)
    x = x / 2 ;set the center based on half x’s coordinates
    y = y / 2 ;set the center based on half y’s coordinates
    Text x,y,mytext$,1,1
    End Function

  6. July 20, 2010 at 1:21 pm

    One more for the road….

    width = 800 ;set the width of screen
    height = 600 ;set height
    bit = 32 ;set the bit rate

    ;assign the width, height and bit
    Graphics width,height,bit,2
    setbuffer frontbuffer()

    ;pass centerText the width & height divided by 2
    centerText width,height,”This is the real center of the screen!”, 1, 1, 2, 2
    WaitKey
    End

    Function centerText(x, y, mytext$, xtf, ytf, x1, y1)
    x = x / x1
    y = y / y1
    Text x,y,mytext$,xtf,ytf
    End Function

  7. July 20, 2010 at 1:27 pm

    With input
    width = 800 ;set the width of screen
    height = 600 ;set height
    bit = 32 ;set the bit rate

    ;assign the width, height and bit
    Graphics width,height,bit,2
    SetBuffer FrontBuffer()

    newText$=Input$(“Please type anything to center it on the screen. “)

    ;pass centerText the width & height divided by 2
    centerText width,height,newText$, 1, 1, 2, 2
    WaitKey
    End

    Function centerText(x, y, mytext$, xtf, ytf, x1, y1)
    x = x / x1
    y = y / y1
    Text x,y,mytext$,xtf,ytf
    End Function

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: