Page 6 of 7

Re: Thoughts about data control

PostPosted: Fri Jul 02, 2010 10:17 am
by Fuzzy
Nice functions bee.

Re: Thoughts about data control

PostPosted: Mon Jul 26, 2010 5:23 pm
by Bee-Ant
Function to handle stuff on inventory...it can be items, weapons, armors, etc...
Code: Select all
//Here's the custom functions to operate Item and Weapon made by Bee-Ant
const int MAX_SLOT=99;              //is the max slot
char stuff_name[MAX_SLOT+1][32];
int stuff_atr[MAX_SLOT+1][2];       //[index][0] is the type, while [index][1] is the amount
//The type as follow :
int item=0;
int weapon=1;

void delete_stuff(char*A,int type) //A is the stuff name
{
    int i,index;
    for(i=0;i<MAX_SLOT;i++) //search the stuff index
    {
        if(!strcmp(stuff_name[i],A)&&stuff_atr[i][0]==type)
        {
            index=i;i=MAX_SLOT; //gotcha
        }
    }
    strcpy(stuff_name[index],"");stuff_atr[index][0]=0;stuff_atr[index][1]=0; //delete the stuff
    for(i=index;i<MAX_SLOT;i++) //re-sort the stuff list
    {
        strcpy(stuff_name[i],stuff_name[i+1]);
        stuff_atr[i][0]=stuff_atr[i+1][0];
        stuff_atr[i][1]=stuff_atr[i+1][1];
    }
}
void use_stuff(char*A,int type,int amount) //A is the stuff name
{
    int i,index;
    for(i=0;i<MAX_SLOT;i++) //search the stuff index
    {
        if(!strcmp(stuff_name[i],A)&&stuff_atr[i][0]==type)
        {
            index=i;i=MAX_SLOT; //gotcha
        }
    }
    if(stuff_atr[index][1]>amount)
    {
        stuff_atr[index][1]-=amount; //use if current amount higher than the requested amount
    }
    if(stuff_atr[index][1]==amount)
    {
        delete_stuff(A,type); //delete if the current amount equal with the requested amount
    }
    if(stuff_atr[index][1]<amount)
    {
        //you can add some alert that the requested amount higher than the current amount here
    }
}
void add_stuff(char*A,int type,int amount) //A is the stuff name
{
    int i,index=MAX_SLOT+1;
    for(i=0;i<MAX_SLOT;i++) //search the last/empty stuff index
    {
        if(!strcmp(stuff_name[i],""))
        {
            index=i;i=MAX_SLOT; //gotcha
        }
    }
    if(index<MAX_SLOT+1) //if the slot isn't full yet, add the item
    {
        strcpy(stuff_name[index],A);
        stuff_atr[index][0]=type;
        stuff_atr[index][1]=amount;
    }
    if(index==MAX_SLOT+1) //if the slot is already full
    {
        //you can add some alert here
    }
}

Usage :
Code: Select all
//function("stuff name",stuff type, stuff amount);

add_stuff("antidote",item,1); //1 is the amount you want to add
use_stuff("antidote",item,1); //1 is the amount you want to use
delete_stuff("antidote",item);

add_stuff("sword",weapon,2); //2 is the amount you want to add
use_stuff("sword",weapon,2); //2 is the amount you want to use
delete_stuff("sword",weapon);

System :
- You can have item and weapon get the same name
- You can add more type by adding more type variables
- You can extend your slot by changing the MAX_SLOT value
- All the stuffs stored in the same array to simplify the operation
- All the stuffs have their own type to avoid missoperation
- To neutralize typo such as "anTidotE", use the letter case function here :
viewtopic.php?f=5&t=6854&start=60#p59790
So in the usage, you can type something like this :
Code: Select all
add_stuff(ToLower("AnTiDote"),item,1);

It will be converted to "antidote", so no worries on typo :D

Hope helps :D

Re: Thoughts about data control

PostPosted: Mon Aug 23, 2010 1:33 am
by zxcvbnm
Hello sirs , nice to meet all of you. I happen to read the post here and while many are very advanced beyond my coding ability, I find this articule intoxicating. My data control is not as advanced but effective. I will pose a simple example I have in mind.

In a space invaders type game I use 2 filled regions that I place above and below the view. Then on the collision command any actor , usaully bullets, I put destroy actor collide actor.
Now what this does is free memory by destroying any actor that has exited the screen and is no longer useful.
That is a simple example of how to save memory space by deleting charactors not in use. Thank you for your time in reading this and I hope to add many more posts of wisdom to your data control articule.

Re: Thoughts about data control

PostPosted: Mon Aug 23, 2010 3:29 am
by DST
Use Out of Vision>Destroy Actor for destroying offscreen actors. It's in the actor events list.

Re: Thoughts about data control

PostPosted: Mon Aug 23, 2010 3:43 am
by DilloDude
Or you can check the actor's y-coordinate.

Re: Thoughts about data control

PostPosted: Mon Aug 23, 2010 12:25 pm
by zxcvbnm
Thank you kind sirs for showing me a more effective way of data control. Now onward to more coding matters. Lets say I have made a mario type game with the variable being "mjump" . As usual many people will give the value of mjump=1; in the physical reponse setion. Let say the programmer wanted a platform where the player
would do a double or triple jump. Normal on pure instint the programmer would make another variable and set the value at 2 or 3 depending how many times the player would be alotted to press the button.
Now my method would not to declare another variable. There is no need because you can just change the value of " mjump" to 2 or 3 for the specific platform you want the player to be able to double or triple jump. This saves you precious memory which could be used in another part of your game. This concludes today's lesson and I thank you all for taking out the time to read this.

Re: Thoughts about data control

PostPosted: Tue Aug 24, 2010 1:18 am
by Bee-Ant
zxcvbnm wrote:There is no need because you can just change the value of " mjump" to 2 or 3 for the specific platform you want the player to be able to double or triple jump. This saves you precious memory which could be used in another part of your game.

Make the work more effective yes, save precious memory not really...
Integer only sized 2 bytes.

Re: Thoughts about data control

PostPosted: Sun Aug 29, 2010 1:05 pm
by Fuzzy
Bee says that sun java 6 with Ubuntu prevents posting on GE. I dont know how hes controlling his data, but it works for me. :P

Re: Thoughts about data control

PostPosted: Mon Aug 30, 2010 3:13 am
by Bee-Ant
Fuzzy wrote:Bee says that sun java 6 with Ubuntu prevents posting on GE. I dont know how hes controlling his data, but it works for me. :P

Darn you...at least I could control anything faster than you :P
I didn't say it prevents me from posting, it just made my firefox a little messed up...
Anyway, it was on 9.10
Haven't tried it on 10.04 :mrgreen:

Re: Thoughts about data control

PostPosted: Thu Feb 10, 2011 1:37 am
by NightOfHorror
as embarassing as this what makes up strings

Re: Thoughts about data control

PostPosted: Sat Feb 12, 2011 11:04 am
by needaname
a string is an array of char so characters

Re: Thoughts about data control

PostPosted: Sun Feb 13, 2011 2:48 pm
by Bee-Ant
needaname wrote:a string is an array of char so characters

Actually string is an array of integer. The difference is just it's stored in the characters ASCII form, and you can't do math operation on it.
So,
Code: Select all
char str[32]="needaname";

and
Code: Select all
int str[32]={110,101,101,100,97,110,97,109,101};

are ALMOST the same.
110 is the ASCII code for "n", 101 for "e", etc...
To convert the char to int, you can do this:
Code: Select all
intvar=(int)str[index];

And to convert int to char, you can do this:
Code: Select all
sprintf(str,"%s",&intvar);

:D

Re: Thoughts about data control

PostPosted: Mon Feb 14, 2011 10:34 am
by needaname
well doesnt ge use chars as the basis of the string? neway they are pretty much the same thing so since char is the special case of an int saying they are chars is being more specific right? you can do math with chars if you wanted but that woudnt make much sense. i think the opposite applies here as well.

Re: Thoughts about data control

PostPosted: Mon Feb 14, 2011 11:00 am
by Game A Gogo
It's only logical doing math with an unsigned char. especially if it's a byte taken from a file.
but some things with char often gives you random error you can avoid by having it an int...

Re: Thoughts about data control

PostPosted: Tue Feb 15, 2011 2:42 am
by NightOfHorror
What is an array?