Page 1 of 1

Coding Tutorial - Make voids!

PostPosted: Sat Jul 10, 2010 12:54 am
by Hblade
Coding Tutorial
          by Hblade

Alright guys, as you may know, when you get high up into making games, you'll start to see your self using the same code over and over again. You can make this a lot shorter on yourself by using voids. Voids pretty much, to put it simply (the only way I know it as xD) is, its a custom function that you made. It can do actions that regular coding can but all in 1 line. For example, say you have TONS of code, like maybe 15 lines. You do the same 15 lines for each actor. Instead, simply do this
Code: Select all
void MyFunction1(int variable1, char txt1, double d1, float d1)
{
    functions
}

int, char, double, float, they are all variable control that can be controlled or read within the brackets. An example of a void function:
Code: Select all
void changeSpeed(double xvel, double yvel)
{
    xvelocity = xvel;
    yvelocity = yvel;
}


Its a short example but that code will allow you to change the xvelocity and yvelocity of the actor using that code, simply by calling this function changeSpeed(2, 8);. This means Xvelocity is 2, and yvelocity is 8.

If you want to make a void function control an actor, simply do it like this:
Code: Select all
void changeSpeed(char *aName, double xvel, double yvel)
{
    Actor * actor = getclone(aName);
    actor->xvelocity = xvel;
    actor->yvelocity = yvel;
}

As you see, I didn't use actor.xvelocity, I used actor->yvelocity. This is because we used the getclone function to get the aName, or actor name. For some reason you need to do -> xD Idk why :o.


Anyways, you can put anything within a void, so use them and shorten your code! :D

Enjoy :3 I hope this helped :D

More examples

PostPosted: Sat Jul 10, 2010 12:54 am
by Hblade
Heres some more examples of what you can do.

Set custom X and Y
Code: Select all
void setPos(char *aName, int X, int Y)
{
    Actor * actor = getclone(aName);
    actor->x = X;
    actor->y = Y;
}

How to use,
setPos("Event Actor", 32, 189);

Get the length of an actors text
Code: Select all
void getTextLength(char *aName)
{
    Actor * actor = getclone(aName);
    int i;
    i = strlen(actor->text);
    textNumber = i;
}

This will return the length of a text actor tot he textNumber of the actor using this command. You can replace textNumber with a local variable you created to store it in that, then use it for farther coding.

How to use,
getTextLength("Message");
Where Message is an actor that stores the game dialog.

Re: Coding Tutorial - Make voids!

PostPosted: Sat Jul 10, 2010 1:14 am
by Hblade
If you have a game where you have multiple players, or a player selection you might not wanna type all kidns of draw actor with the same code on different actors, and not use the inharet events from because you want them to have different abilities, use a void like this to control the view movement
Code: Select all
void playerView()
{
    if (xscreen>=340)
    {
        view.x+=4;
    }
    if (xscreen>=380)
    {
        view.x+=2;
    }
    if (xscreen<=280)
    {
        view.x-=4;
    }
    if (xscreen<=240)
    {
        view.x-=2;
    }
}


For any player actor in action, simply put this code in draw actor instead of all of that :D
playerView();

Re: Coding Tutorial - Make voids!

PostPosted: Sat Jul 10, 2010 10:02 pm
by lcl
Cool! I have been playing around trying understand the global code for making voids, and this is very useful! :D

Re: Coding Tutorial - Make voids!

PostPosted: Sat Jul 10, 2010 10:23 pm
by Hblade
Glad you like it :D

Re: Coding Tutorial - Make voids!

PostPosted: Mon Apr 25, 2011 10:10 pm
by schnellboot
this is C

Re: Coding Tutorial - Make voids!

PostPosted: Mon Apr 25, 2011 10:11 pm
by Game A Gogo
it IS c scripting!
Except you get slightly bit more functions that are GE related, and some C functions won't work

Re: Coding Tutorial - Make voids!

PostPosted: Mon Apr 25, 2011 10:18 pm
by AnarchCassius
Awww, for cryin out loud.... HBlade, you're incredibly smart but your lack of formal training is really showing :) I'm not trying to be mean, for you to have figured all that out by yourself is impressive but you obviously did figure it out by yourself since you got the jargon and some details wrong. It's like watching someone from 1,000 years from now try and figure out our code :)

Voids pretty much, to put it simply (the only way I know it as xD) is, its a custom function that you made.

Actually the things you are discussing here are exactly custom functions and that is the technical way to refer to them. Voids are not what you think they are.

Void is what the function is returning, it's not (just) a declaration of the beginning of a function, it's what gets sent back. In the case of "void", nothing. But you can make functions that return data as well.

Such as:

Code: Select all
int ItemCount(int item)
{
int i=NULL;
int count=0;
for(i=0; i<InventorySize; i+=2) //for each inv slot
{
    if (Inventory[i] == item && Inventory[i+1] > 0) //if slot empty or contains this item
    {
        count += Inventory[i+1];
    }
}
return count;
}


This little guy runs through an array and when it finds a given item, adds the item count to a running tally, when done it returns the total number of that item type in the inventory.

Sorry the example is so complex but none of my simple functions need to return anything. Speaking of which you want to end functions with a "return", then GE knows it's time to stop the function and send the data back. Things may work fine without it, but it's good practice. In fact you can end the function anywhere you like by using return, making certain conditions prevent others from being reached and so forth.

Oh and the use of "->" is because it's a pointer and not one of GE's objects. Honestly I'm not even entirely sure why it works like that, maybe to remind you it IS a pointer. But that's how pointer's are.

Re: Coding Tutorial - Make voids!

PostPosted: Wed May 11, 2011 5:46 pm
by Hblade
I will be going over this in my future tutorials :D