Simple Useful Functions Topic!

Game Editor comments and discussion.

Simple Useful Functions Topic!

Postby jimmynewguy » Fri Oct 14, 2011 8:22 pm

Hi everyone, just thought it'd be cool for people to post simple, useful functions here that are "universal" (can be used often) just to see what people have! It's a good thing for beginners or people who want to improve to see.

First one is a toggle function.
Code: Select all
int Toggle(int integer)
{
return !integer;
}

You can use it to toggle and integer or other things. Like this would switch an actor from transparent to visible or vise versa
Code: Select all
transp = Toggle(transp);


Another could be PointFromCircle
Code: Select all
void PointFromCircle(char * circles, int radius)
{
Actor * circle = getclone(circles);
theta = direction(circle->x - view.x, circle->y - view.y, xmouse, ymouse)/60;
x = circle->x+radius*cos(-theta);
y = circle->y+radius*sin(-theta);
}

You can use it to have an actor point at the mouse (or change the mouse to anything else if you want) and stay on a circle paths, not nearly as universal but it could help someone :P
Code: Select all
PointFromCircle("circle", 50);

This would have the actor point towards the mouse around the actor named circle with a radius of 50 pixels. You could use it if you made a soccer like game and the player moved towards the mouse, then the ball would be in the right spot for dribbling!

Now post some more! :)
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Simple Useful Functions Topic!

Postby lcl » Fri Oct 14, 2011 8:34 pm

Well, here's some functions made by me. :)

Integer array reset function
Code: Select all
void resetArray(int * array, int lim)
{
     int i;

    for (i = 0; i < lim; i ++)
    {
        array[i] = 0;
    }
}


Usage:
Code: Select all
resetArray(myArray, 25);

That would reset array named as myArray until array cell number 25.

Get clone2-function (not my own, very commonly used, thought to share since it's very useful :) )
Code: Select all
char ACTOR[50];

Actor * getClone2(char actorName[50], int num)
{
    sprintf(ACTOR, "%s.%i", actorName, num);
    return getclone(ACTOR);
}


Usage:
Code: Select all
int i;
for (i = 0; i < 5; i ++)
{
    getClone2("myActor", i)->r = 0;
}

That code sets all the myActors r's to 0 from cloneindex 0 to 5.

I will post some more soon! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Simple Useful Functions Topic!

Postby EvanBlack » Sat Oct 15, 2011 2:30 am

Edit: Do not use this function, Read below for details.
Code: Select all
void resetIntArray(int * array)
{
     int i;
     int lim = sizeof(array)/sizeof(int);

    for (i = 0; i < lim; i ++)
    {
        array[i] = 0;
    }
}

int myArray[20];

resetIntArray(myArray);


If overloading of functions was allowed you could make just a single reset array with multiple definitions that gets the size of the array type.

I don't have any "universal" functions XD all mine are specific to various needs that I need to repeat multiple times and it all happens in one function... hmm... not sure how to make any universal functions.
Last edited by EvanBlack on Sat Oct 15, 2011 6:52 pm, edited 2 times in total.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Simple Useful Functions Topic!

Postby Hblade » Sat Oct 15, 2011 4:19 am

Check and see if the animation is one of the two, then change that animation:
Code: Select all
void DOB(char *PNAME, char *N1,char *N2,char *N3,char *N4) {
    char *ac=PNAME;
    Actor *A=getclone(PNAME);
    if (A->animindex==getAnimIndex(N1)) {
        ChangeAnimation(ac, N3, FORWARD); }
    if (A->animindex==getAnimIndex(N2)) {
        ChangeAnimation(ac, N4, FORWARD); }
                                                           }


Example:
Code: Select all
DOB("Player", "WalkRight", "WalkLeft", "StopRight", "StopRight");


It will check and see if the animation is "walkright" or "walkleft" (checks 1 and 2), and if it is, it will change it to "StopRight" or "StopLeft"

1 becomes 3 and 2 becomes 4. very useful function ^^
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Simple Useful Functions Topic!

Postby EvanBlack » Sat Oct 15, 2011 8:20 am

Here is an example of a Linked List XD

What this does is stores the clonename of the actor on creation into a linked list. Then it uses the clone name to change variables in the clone. This is very useful for many things, but you would have to create a function for each variable you want to change. Or just use it such as:

Code: Select all
getclone(currentActor->ThisActor)->ActorVariable;


That will access the current actor in the list and change the variable of the actor you need to change. This allows for very dynamic approach to handling clones.

Useful functions to make are:

A Function that creates a new element in the linked list then links it to the node chosen or simply the last node in the list.

A Function that counts the number of elements in a linked list.

A Function that can sort a linked list


Choosing an Array or Linked list can be difficult, you need to make sure you are making the right choice for the job at hand. But things a linked list can do that an array cannot are:

Store more than one variable type per element, this can be useful if you want to handle various data types such as Player name, Player Actor, Health, Position, ect.

Sorting a Linked List and an Array are pretty much the same, except that with an array you get more overhead when moving objects, removing objects, or adding objects. With a linked list you simply change the address of the next node.

Also, when using linked lists, allocating memory means you must free it when you are finished or you will cause a memory leak.
Attachments
Linked List.rar
Lots of Comments
(15.78 KiB) Downloaded 134 times
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Simple Useful Functions Topic!

Postby lcl » Sat Oct 15, 2011 12:29 pm

EvanBlack wrote:I like this function but I may improve upon it.
Code: Select all
void resetIntArray(int * array)
{
     int i;
     int lim = sizeof(array)/sizeof(int);

    for (i = 0; i < lim; i ++)
    {
        array[i] = 0;
    }
}

int myArray[20];

resetIntArray(myArray);


That's nice idea, but you can't get a sizeof() of a variable by pointer.
If you don't believe me, try to use your code.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Simple Useful Functions Topic!

Postby jimmynewguy » Sat Oct 15, 2011 12:38 pm

Glad to see people are posting here :)

I can re-post my check function. It checks if there is an actor at x,y but runs much faster than CollisionFree
Code: Select all
int Check(int posX, int posY)
{
return(getactor(posX, posY)->cloneindex == -1);
}

Returns 1 if the "coast is clear"
Working on a probably too ambitious project! Wild-west-adventure-RPG-shooter-thing.
User avatar
jimmynewguy
 
Posts: 1137
Joined: Sat Mar 31, 2007 6:27 pm
Score: 89 Give a positive score

Re: Simple Useful Functions Topic!

Postby Hblade » Sat Oct 15, 2011 4:08 pm

A Simple recolor function (untested just thought I'd post it xD Might have an error or two but just small ones)
Code: Select all
void Color(, char *AName, int R, int G, int B, double ALPHA) {
    Actor *A=getclone(AName);
    A->r=R;
    A->g=G;
    A->b=B;
    A->transp=ALPHA;
}


Usage:
Code: Select all
Color("Player", 255, 255, 255, .50);


Edit: For some reason this doesn't work with "Event Actor" D: So if your using clones or bullets this wont work xD unless you simply put "Bullet".
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Simple Useful Functions Topic!

Postby Game A Gogo » Sat Oct 15, 2011 4:29 pm

Hblade wrote:A Simple recolor function (untested just thought I'd post it xD Might have an error or two but just small ones)
Code: Select all
void Color(, char *AName, int R, int G, int B, double ALPHA) {
    Actor *A=getclone(AName);
    A->r=R;
    A->g=G;
    A->b=B;
    A->transp=ALPHA;
}


Usage:
Code: Select all
Color("Player", 255, 255, 255, .50);


Edit: For some reason this doesn't work with "Event Actor" D: So if your using clones or bullets this wont work xD unless you simply put "Bullet".


Code: Select all
void Color(char *AName, int R, int G, int B, double ALPHA)
{
    if(strcmp(AName,"Event Actor")
    {
        r=R;
        g=G;
        b=B;
        transp=ALPHA;
    }
    else if(strcmp(AName,"Collide Actor")
    {
        collide.r=R;
        collide.g=G;
        collide.b=B;
        collide.transp=ALPHA;
    }
    else if(strcmp(AName,"Parent Actor")
    {
        parent.r=R;
        parent.g=G;
        parent.b=B;
        parent.transp=ALPHA;
    }
    else
    {
        Actor *A=getclone(AName);
        A->r=R;
        A->g=G;
        A->b=B;
        A->transp=ALPHA;
    }
}


Now it works with Event Actor, Collide Actor and Parent Actor as well as a specific one ;)
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Simple Useful Functions Topic!

Postby EvanBlack » Sat Oct 15, 2011 5:00 pm

lcl wrote:That's nice idea, but you can't get a sizeof() of a variable by pointer.
If you don't believe me, try to use your code.

Thanks :3

While you CAN sizeof(array) you CANNOT sizeof(*array). Strange.
Last edited by EvanBlack on Sat Oct 15, 2011 6:55 pm, edited 1 time in total.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Simple Useful Functions Topic!

Postby Game A Gogo » Sat Oct 15, 2011 5:45 pm

EvanBlack wrote:
lcl wrote:That's nice idea, but you can't get a sizeof() of a variable by pointer.
If you don't believe me, try to use your code.



Honestly.. I did believe you so I thought I wrote the code wrong because I was tired last night. So I rewrote it and it was wrong, so I checked the original code and it worked...

I have sizeof()'ed arrays before so I knew it would work, and it does work on pointers because an array is a pointer but it points to a large block of memory which is then separated into smaller blocks of memory of the data type.

Here is the ged


Try having an array of 50. then it won't work. it works for little arrays, but after a certain size sizeof doesn't return it's actual value

exemple:
resetarrayfunction.zip
(194.57 KiB) Downloaded 137 times
Attachments
untitled.PNG
Last entry is the 50th cell in the array
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Simple Useful Functions Topic!

Postby EvanBlack » Sat Oct 15, 2011 5:48 pm

interesting... why is that?
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Simple Useful Functions Topic!

Postby Game A Gogo » Sat Oct 15, 2011 5:58 pm

lcl wrote:The problem is that sizeof doesn't know where the pointer is pointing.
That's what I read about it. :P

Fuzzy wrote:Turns out GE has a build in memset function that it takes from c/c++ but we still cant tell the sizeof() a structure pointed at. You'll need to record its size when you create the array.


other than that, I don't know more myself!
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Simple Useful Functions Topic!

Postby EvanBlack » Sat Oct 15, 2011 6:12 pm

I see, I did some tests the array too, even trying to pass the address of the array directly to the function. It seems that upon creation of the array it defines it as a new type.

* ARY[n]int

n = the number of elements.

So on compilation of the script it already has definitive size...

So then yes the only way to use the function would be to pass the size of the array with it. But if you are dynamically allocating the array you would need to store an extra integer variable that gives the size of the array. Or use a terminator for the end of all your arrays then when the array reaches the terminator end the loop.

But there doesn't seem to be a logical terminator variable for an int array. As a char array "\0" would be the terminator.
(\__/) ( Soon... The world)
(O.o )< will be mine!____)
(> < )
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bunny Overlord 2012!
EvanBlack
 
Posts: 202
Joined: Fri Sep 30, 2011 10:17 am
Score: 19 Give a positive score

Re: Simple Useful Functions Topic!

Postby Game A Gogo » Sat Oct 15, 2011 6:31 pm

it's too bad in C and C++ NULL equals to 0 S:
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Next

Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest