Page 1 of 1

"ParentBlock" code with demo

PostPosted: Wed Nov 05, 2014 4:00 am
by Hblade
This script allows you to create a block which many actors can become "parrented" to.

Instructions:
updateParentBlock(id, x, y, width, height, z);
This code sets/updates the initial variables of the ParentBlock ID.

snapToParentBlock(id, offsetx, offsety, offsetz);
This sets the actor's X and Y equal to the blocks X and Y, plus the offsets you specified. It also alters the Z depth, which can also be offset.

drawParentBlock(id);
This is used in a canvas, make sure to stretch the canvas to the views size.

Copy the code to your global code, and name it something fitting.

Code: Select all
//Creating the struct
typedef struct {
    int x, y, width, height;
    double z;
               } parentBlock;

parentBlock ParentBlock[9999];

//Struct Creation

void updateParentBlock(int id, int X, int Y, int Width, int Height, double Z) //Set the initial X and Y, or update it.
{
    ParentBlock[id].x=X;
    ParentBlock[id].y=Y;
    ParentBlock[id].width=Width;
    ParentBlock[id].height=Height;
    ParentBlock[id].z=Z;
}


void snapToParentBlock(int id, int offsetX, int offsetY, double offsetZ) //Snaps an actor to the given block, technically "parrenting" it. Also effects Z depth.
{
    x=ParentBlock[id].x+offsetX;
    y=ParentBlock[id].y+offsetY;
    ChangeZDepth("Event Actor", ParentBlock[id].z+offsetZ);
}

void drawParentBlock(int id) // Use this in a canvas to debug/draw the window, stretch canvas to views size.
{
    moveto(-view.x+ParentBlock[id].x, -view.y+ParentBlock[id].y); //Honestly don't know why it has to be negative.
    lineto(-view.x+ParentBlock[id].x, -view.y+ParentBlock[id].y+ParentBlock[id].height);
    lineto(-view.x+ParentBlock[id].x+ParentBlock[id].width, -view.y+ParentBlock[id].y+ParentBlock[id].height);
    lineto(-view.x+ParentBlock[id].x+ParentBlock[id].width, -view.y+ParentBlock[id].y);
    lineto(-view.x+ParentBlock[id].x, -view.y+ParentBlock[id].y);
}


Skydereign will probably notice some more optimized way of doing this, but that's part of the fun! :D Enjoy guys.

Reason for file size: I apparently used a font that's about 20MB lol O:...

In the demo, you can use left and right to control the box :D.

Re: "ParentBlock" code with demo

PostPosted: Fri Nov 07, 2014 12:51 am
by lcl
Pretty nice demo. :)

Re: "ParentBlock" code with demo

PostPosted: Fri Nov 07, 2014 1:28 am
by Zivouhr
I have to check out the demo, but this looks interesting. What are some of the advantages in a video game of this Parenting code? Thanks.

Re: "ParentBlock" code with demo

PostPosted: Fri Nov 07, 2014 3:58 pm
by Hblade
At the moment, one thing I can think of would be to create a boss made of multiple parts.

Re: "ParentBlock" code with demo

PostPosted: Sat Nov 08, 2014 7:20 am
by Zivouhr
That sounds interesting, thanks HBlade.