Charas-Project

Off-Topic => Archive => Resources => Topic started by: fruckert on January 04, 2011, 11:14:15 AM

Title: Game Maker Scripts
Post by: fruckert on January 04, 2011, 11:14:15 AM
Here's a collection of useful Game Maker scripts that I've made.

Or, at least, it will be a collection once I get more stuff in.

Message Queue System
Requires Pro.

This system makes it so that you can add a certain amount of messages to be shown at the bottom right corner of the screen.
Tinker with the numbers if you want to change it's position.

[spoiler=Code]
Message Controller Object
This should be persistent.
Code: (Create Event) [Select]
globalvar messageList,messageTimes;

messageList = ds_queue_create();
messageTimes = ds_queue_create();
//These two globals carry the message queue.
//The first one carries the actual messages, while the latter one carries the amount of time that the message should be displayed for, in seconds.

displaying = false //If we are currently displaying a message
timer = 0            //The countdown timer.
message = " "     //Empty message variable.

Code: (Step Event) [Select]
if !ds_queue_empty(messageList) and !displaying{ //If we aren't displaying something, and there's something still in the queue.
    message = ds_queue_dequeue(messageList)     //Load up the new message
    timer   = ds_queue_dequeue(messageTimes)    //As well as the time for said message
    displaying = true}                                           //And make it display.

if displaying and timer > 0{                                 //If the timer is still going
    timer -= 1}                                                    //Then make it go
else{
    displaying = false}                                          //Otherwise, stop displaying the message.

Code: (Draw Event) [Select]
if displaying{                                                      //If we're displaying
    draw_info(message)}                                     //Draw our stuff.


Auxilary Scripts

This adds things to the messaging queue.
Code: (message_add("message",time)) [Select]
//message_add("message", time)
//Adds a message to the message list, to be shown later.

ds_queue_enqueue(messageList,argument0)                        //Add the new message to the queue
if argument1 = 0 then argument1 = 3                                  //If argument1 is empty, then default to three seconds (change at your leisure)
ds_queue_enqueue(messageTimes,room_speed*argument1)//room_speed is, of course, how many frames per second the room is running at.
                                                                                       //If you multiply that by a number, you get seconds. Very simple math, actually.
//That's...it, actually.


This draws the little window that the info is drawn into.
If you're NOT using GM8, then you're going to need to change a line.
Code: (Made by Ace, of the GMC) [Select]
//draw_window(sprite,x,y,width,height,subimg,alpha)

//OR

//draw_window(sprite,x,y,width,height,0,alpha,clr1,clr2)


//DESCRIPTION:
//Draws an RPG-Maker type window using 9 tiles on a 3x3 grid for border
//and, either a tiled subimage OR a color gradient used as a background,
//centered at an x,y position. If you want the color gradient fill only,
//set the 'subimg' argument to 0 and provide the top and bottom colors.
//If you want to use a stretched subimage to fill the background, select
//a subimage and use -1 where clr1 would be but leave out argument clr2.
//This script is very versatile. It should easily support transparency.

//NOTE:
//The second subimage (unless using GM8) should have the bottom row of
//pixels completely transparent. If you're using GM8, remove the -1 on
//the indicated line below so you can use the full subimage canvas. It
//is commented just before the "Draw Background Color Gradient" line.

//By Ace


var spr, x1, y1, w, h, hcell, vcell, hoff, voff, i, j, ts,alpha;

  
spr=argument0
x1=argument1-ceil(argument3/2)
y1=argument2-ceil(argument4/2)
w=argument3
h=argument4
//tile size
ts=ceil(sprite_get_width(spr)/3);
if argument3<ts then w=ts
if argument4<ts then h=ts
//if the window is too small, draw nothing
if argument3<ts or argument4<ts then exit;
alpha = argument6
  
hcell=ceil(w/ts)-2
vcell=ceil(h/ts)-2
hoff=(w-(hcell*ts))/2
voff=(h-(vcell*ts))/2


//Draw Background Sprite Tiled
if argument5>0 && argument7!=-1
{
    var sprite,subimg,xx,yy,x1,y1,x2,y2;
    sprite = argument0;
    subimg = argument5;
    xx = x1+1;
    yy = y1+1;
    xx1 = x1+1;
    yy1 = y1+1;
    xx2 = x1+w-3;
    yy2 = y1+h-3;
    
    var sw,sh,i,j,jj,left,top,width,height,X,Y;
    sw = sprite_get_width(sprite);
    sh = sprite_get_height(sprite);
    
    i = xx1-((xx1 mod sw) - (xx mod sw)) - sw*((xx1 mod sw)<(xx mod sw));
    j = yy1-((yy1 mod sh) - (yy mod sh)) - sh*((yy1 mod sh)<(yy mod sh));
    jj = j;
    
    for(i=i; i<=xx2; i+=sw) {
        for(j=j; j<=yy2; j+=sh) {
        
            if(i <= xx1) left = xx1-i;
            else left = 0;
            X = i+left;
            
            if(j <= yy1) top = yy1-j;
            else top = 0;
            Y = j+top;
            
            if(xx2 <= i+sw) width = ((sw)-(i+sw-xx2)+1)-left;
            else width = sw-left;
            
            if(yy2 <= j+sh) height = ((sh)-(j+sh-yy2)+1)-top;
            else height = sh-top;
            
            //Remove the -1 on the line below (right after the "top" part) if you're using GM8
            draw_sprite_part(sprite,subimg,left,top,width,height,X,Y);
            }
        j = jj;
    }
}//Draw Background Color Gradient
else
if argument7=-1
then draw_sprite_stretched(argument0,argument5,x1+1,y1+1,w-3,h-3)
else draw_rectangle_color(x1+1,y1+1,x1+w-3,y1+h-3,argument6,argument6,argument7,argument7,0)



//horizontal border
for (i=0;i<hcell;i+=1)
{draw_sprite_part(spr,0,ts,0,ts,ts,x1+(i*ts)+hoff,y1)
draw_sprite_part(spr,0,ts,ts*2,ts,ts-1,x1+(i*ts)+hoff,y1+h-ts)}

//vertical border
for (j=0;j<vcell;j+=1)
{draw_sprite_part(spr,0,0,ts,ts,ts,x1,y1+(j*ts)+voff)
draw_sprite_part(spr,0,ts*2,ts,ts-1,ts,x1+w-ts,y1+(j*ts)+voff)}

//corners
draw_sprite_part(spr,0,0,0,ts,ts,x1,y1) //left top
draw_sprite_part(spr,0,ts*2,0,ts-1,ts,x1+w-ts,y1)//right top
draw_sprite_part(spr,0,0,ts*2,ts,ts-1,x1,y1+h-ts)//left bottom
draw_sprite_part(spr,0,ts*2,ts*2,ts-1,ts-1,x1+w-ts,y1+h-ts) //right bottom


This formats the line.
Code: (lineformat("string",maxchars) - Credit to Ace of the GMC) [Select]
//lineformat(str,maxchrs)

//DESCRIPTION:
//Inserts a line break '#' after a certain number of characters.
//If the break lands on a character other than a space, it will
//be inserted at the last space it came across.


var count, lastspace, i, char;

count=0
lastspace=0
for (i=1; i<=string_length(argument0); i+=1) {
char=string_copy(argument0,i,1)
count+=1
if char=" " { lastspace=i }
else if char="#" { lastspace=0; count=0 }
if count>=argument1 {
if lastspace>0 {
count=0
i=lastspace
lastspace=0
argument0=string_insert("#",string_delete(argument0,i,1),i)
}
}
}
return(argument0)


This draws the info box.
It's like, two lines.
Code: (draw_info("string to draw")) [Select]
var str;

str = lineformat(argument,32) //Format our message, so that it's not HOLY **** huge. Change 32 to something else if you so desire.

draw_window(sprWindow,view_xview[0]+view_wview[0]-(string_width(str)/2)-8,view_yview[0]+view_hview[0]-(string_height(str)/2)-8,string_width(str)+12,string_height(str)+12,1)
draw_text_color(view_xview[0]+view_wview[0]-string_width(str)-8,view_yview[0]+view_hview[0]-string_height(str)-8,str, c_white, c_white, c_white, c_white, 0.75)
[/spoiler]