House Save/Load System
#9

How can I go about adding a variable to my house info checking if a house is created or not? Not really too sure how I would go about doing that. Could you show an example?

I've fixed everything else thankyou very much for you time! I will +rep you as soon as it lets me again x)

EDIT: I seem to also have another problem. I /createhouse and it does it fine and saves it fine to the correct file corresponding to the ID I input and even when I /buyhouse it saves fine. But when shutting down the server and restarting it just puts that house ID coordinates to 0.0 0.0 0.0 in the game but in the file it's still set how it was saved. I think I have a problem when loading. Here's my updated code for save/load/create and also /buy

SAVE

pawn Код:
forward SaveHouses();
public SaveHouses()
{
    new string3[128];
    for(new idx; idx < sizeof(HouseInfo); idx++)
    {
        format(string3, sizeof(string3), "South-WestRP/Houses/%d.ini", idx);
        new File: hFile = fopen(string3, io_write);
        if (hFile)
        {
            new var[32];
            format(var, 32, "hName=%s\n", HouseInfo[idx][hName]);fwrite(hFile, var);
            format(var, 32, "hOwner=%s\n", HouseInfo[idx][hOwner]);fwrite(hFile, var);
            format(var, 32, "hEnterX=%f\n", HouseInfo[idx][hEnterX]);fwrite(hFile, var);
            format(var, 32, "hEnterY=%f\n", HouseInfo[idx][hEnterY]);fwrite(hFile, var);
            format(var, 32, "hEnterZ=%f\n", HouseInfo[idx][hEnterZ]);fwrite(hFile, var);
            format(var, 32, "hExitX=%f\n", HouseInfo[idx][hExitX]);fwrite(hFile, var);
            format(var, 32, "hExitY=%f\n", HouseInfo[idx][hExitY]);fwrite(hFile, var);
            format(var, 32, "hExitZ=%f\n", HouseInfo[idx][hExitZ]);fwrite(hFile, var);
            format(var, 32, "EnterInterior=%d\n", HouseInfo[idx][EnterInterior]);fwrite(hFile, var);
            format(var, 32, "EnterWorld=%d\n", HouseInfo[idx][EnterWorld]);fwrite(hFile, var);
            format(var, 32, "hPrice=%d\n", HouseInfo[idx][hPrice]);fwrite(hFile, var);
            format(var, 32, "hLocked=%d\n", HouseInfo[idx][hLocked]);fwrite(hFile, var);
            format(var, 32, "hProducts=%d\n", HouseInfo[idx][hProducts]);fwrite(hFile, var);
            format(var, 32, "hNumber=%d\n", HouseInfo[idx][hNumber]);fwrite(hFile, var);
            format(var, 32, "hType=%d\n", HouseInfo[idx][hType]);fwrite(hFile, var);
            format(var, 32, "hOwned=%d\n", HouseInfo[idx][hOwned]);fwrite(hFile, var);
            fclose(hFile);
        }
    }
    return 1;
}
LOAD

pawn Код:
forward LoadHouses();
public LoadHouses()
{
    new string2[128];
    for(new idx; idx < sizeof(HouseInfo); idx++)
    {
        format(string2, sizeof(string2), "South-WestRP/Houses/%d.ini", idx);
        new File:example = fopen(string2, io_read);
        if(example)
        {
            new key[ 256 ] , val[ 256 ];
            new Data[ 256 ];
            while ( fread( example , Data , sizeof( Data ) ) )
            {
                key = ini_GetKey( Data );
                if( strcmp( key , "hName" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hName] = strval( val ); }
                if( strcmp( key , "hOwner" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hOwner] = strval( val ); }
                if( strcmp( key , "hEnterX" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hEnterX] = floatstr( val ); }
                if( strcmp( key , "hEnterY" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hEnterY] = floatstr( val ); }
                if( strcmp( key , "hEnterZ" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hEnterZ] = floatstr( val ); }
                if( strcmp( key , "hExitX" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hExitX] = floatstr( val ); }
                if( strcmp( key , "hExitY" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hExitY] = floatstr( val ); }
                if( strcmp( key , "hExitZ" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hExitZ] = floatstr( val ); }
                if( strcmp( key , "EnterInterior" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][EnterInterior] = strval( val ); }
                if( strcmp( key , "EnterWorld" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][EnterWorld] = strval( val ); }
                if( strcmp( key , "hPrice" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hPrice] = strval( val ); }
                if( strcmp( key , "hLocked" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hLocked] = strval( val ); }
                if( strcmp( key , "hProducts" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hProducts] = strval( val ); }
                if( strcmp( key , "hNumber" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hNumber] = strval( val ); }
                if( strcmp( key , "hType" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hType] = strval( val ); }
                if( strcmp( key , "hOwned" , true ) == 0 ) { val = ini_GetValue( Data ); HouseInfo[idx][hOwned] = strval( val ); }
                new string[128];
                new housetype[128];
                if(HouseInfo[idx][hOwned] == 0)
                {
                    if(HouseInfo[idx][hType] != 0)
                    {
                        if(HouseInfo[idx][hType] == 1) { housetype = "Un-Furnished"; }
                        else if(HouseInfo[idx][hType] == 2) { housetype = "Furnished"; }
                    }
                    else { housetype = "Empty House"; }
                    format(string, sizeof(string), "%s\nHouse for Sale\nAvailable Price is $%d", housetype,HouseInfo[idx][hPrice]);
                    housetext[idx] = Create3DTextLabel(string,COLOR_ORANGERED,HouseInfo[idx][hEnterX],HouseInfo[idx][hEnterY],HouseInfo[idx][hEnterZ],40.0,0, 1);
                    housepickup[idx] = CreateDynamicPickup(1273, 23,  HouseInfo[idx][hEnterX],HouseInfo[idx][hEnterY],HouseInfo[idx][hEnterZ],-1,-1,-1,40.0);
                }
                else
                {
                    if(HouseInfo[idx][hType] != 0)
                    {
                        if(HouseInfo[idx][hType] == 1) { housetype = "Un-Furnished"; }
                        else if(HouseInfo[idx][hType] == 2) { housetype = "Furnished"; }
                    }
                    else { housetype = "Empty House"; }
                    format(string, sizeof(string), "%s\nOwner: %s\n%s House", HouseInfo[idx][hName],HouseInfo[idx][hOwner],housetype);
                    housetext2[idx] = Create3DTextLabel(string,COLOR_LAWNGREEN,HouseInfo[idx][hEnterX],HouseInfo[idx][hEnterY],HouseInfo[idx][hEnterZ],40.0,0, 1);
                    housepickup2[idx] = CreateDynamicPickup(1272, 23,  HouseInfo[idx][hEnterX],HouseInfo[idx][hEnterY],HouseInfo[idx][hEnterZ],-1,-1,-1,40.0);
                }
                fclose(example);
            }
        }
    }
    return 1;
}
CREATE

pawn Код:
if(strcmp(cmd, "/createhouse", true) == 0)
        {
            if (PlayerInfo[playerid][pAdmin] >= 4)
            {
                tmp = strtok(cmdtext, idx);
                if(!strlen(tmp))
                {
                    SendClientMessage(playerid, COLOR_SYSTEM, "[CMDINFO] - /createhouse [id] [type] [price] [name]");
                    SendClientMessage(playerid, COLOR_SYSTEM, "Type: 1 Un-Furnished");
                    SendClientMessage(playerid, COLOR_SYSTEM, "Type: 2 Furnished");
                    return 1;
                }
                new id = strval(tmp);
                tmp = strtok(cmdtext, idx);
                if(!strlen(tmp))
                {
                    SendClientMessage(playerid, COLOR_SYSTEM, "[CMDINFO] - /createhouse [id] [type] [price] [name]");
                    SendClientMessage(playerid, COLOR_SYSTEM, "Type: 1 Un-Furnished");
                    SendClientMessage(playerid, COLOR_SYSTEM, "Type: 2 Furnished");
                    return 1;
                }
                new Float:x,Float:y,Float:z;
                GetPlayerPos(playerid, x, y, z);
                HouseInfo[id][hEnterX] = x;
                HouseInfo[id][hEnterY] = y;
                HouseInfo[id][hEnterZ] = z;
                new id2;
                id2 = strval(tmp);
                tmp = strtok(cmdtext, idx);
                if(!strlen(tmp))
                {
                    SendClientMessage(playerid, COLOR_SYSTEM, "[CMDINFO] - /createhouse [id] [type] [price] [name]");
                    SendClientMessage(playerid, COLOR_SYSTEM, "Type: 1 Un-Furnished");
                    SendClientMessage(playerid, COLOR_SYSTEM, "Type: 2 Furnished");
                    return 1;
                }
                new id3;
                id3 = strval(tmp);
                tmp = strtok(cmdtext, idx);
                if(!strlen(tmp))
                {
                    SendClientMessage(playerid, COLOR_SYSTEM, "[CMDINFO] - /createhouse [id] [type] [price] [name]");
                    SendClientMessage(playerid, COLOR_SYSTEM, "Type: 1 Un-Furnished");
                    SendClientMessage(playerid, COLOR_SYSTEM, "Type: 2 Furnished");
                    return 1;
                }
                new length = strlen(cmdtext);
                while ((idx < length) && (cmdtext[idx] <= ' '))
                {
                    idx++;
                }
                new offset = idx;
                new result[64];
                while ((idx < length) && ((idx - offset) < (sizeof(result) - 1)))
                {
                    result[idx - offset] = cmdtext[idx];
                    idx++;
                }
                result[idx - offset] = EOS;
                HouseInfo[id][hType] = id2;
                strmid(HouseInfo[id][hName], (result), 0, strlen((result)), 128);
                HouseInfo[id][hPrice] = id3;
                if(HouseInfo[id][hType] == 1)
                {
                    HouseInfo[id][hExitX] = 116.51876068115;
                    HouseInfo[id][hExitY] = 1347.8156738281;
                    HouseInfo[id][hExitZ] = 1027.0693359375;
                    HouseInfo[id][EnterWorld] = id;
                    HouseInfo[id][EnterInterior] = 9;
                }
                if(HouseInfo[id][hType] == 2)
                {
                    HouseInfo[id][hExitX] = 223.48486328125;
                    HouseInfo[id][hExitY] = 1287.5721435547;
                    HouseInfo[id][hExitZ] = 1082.140625;
                    HouseInfo[id][EnterWorld] = id;
                    HouseInfo[id][EnterInterior] = 1;
                }
                format(string, sizeof(string), "[INFO:] House ID %d, Is a Type %d House and Cost %d", id, id2, id3);
                SendClientMessage(playerid, COLOR_SYSTEM, string);
                SaveHouses();
                new housetype[128];
                if(HouseInfo[id][hOwned] == 0)
                {
                    if(HouseInfo[idx][hType] != 0)
                    {
                        if(HouseInfo[idx][hType] == 1) { housetype = "Un-Furnished"; }
                        else if(HouseInfo[idx][hType] == 2) { housetype = "Furnished"; }
                    }
                    else { housetype = "Empty House"; }
                    format(string, sizeof(string), "%s\nHouse for Sale\nAvailable Price is $%d", housetype,HouseInfo[id][hPrice]);
                    Move3DTextLabel(housetext[id], string, COLOR_ORANGERED, x, y, z);
                    housepickup[idx] = CreateDynamicPickup(1273, 23,  HouseInfo[idx][hEnterX],HouseInfo[idx][hEnterY],HouseInfo[idx][hEnterZ],-1,-1,-1,40.0);
                }
                else
                {
                    if(HouseInfo[idx][hType] != 0)
                    {
                        if(HouseInfo[idx][hType] == 1) { housetype = "Un-Furnished"; }
                        else if(HouseInfo[idx][hType] == 2) { housetype = "Furnished"; }
                    }
                    else { housetype = "Empty House"; }
                    format(string, sizeof(string), "%s\nOwner: %s\n%s House", HouseInfo[id][hName],HouseInfo[id][hOwner],housetype);
                    Move3DTextLabel(housetext2[id], string, COLOR_LAWNGREEN, x, y, z);
                    housepickup2[idx] = CreateDynamicPickup(1272, 23,  HouseInfo[idx][hEnterX],HouseInfo[idx][hEnterY],HouseInfo[idx][hEnterZ],-1,-1,-1,40.0);
                }
                return 1;
            }
        }
BUY

pawn Код:
if(strcmp(cmd, "/buyhouse", true) == 0)
        {
            new house = IsPlayerNearHouse(playerid);
            if(house == -1) return SendClientMessage(playerid, COLOR_CORAL, "You are not at the location");
            if(HouseInfo[house][hOwned] == 1) return SendClientMessage(playerid, COLOR_CORAL, "This House is already owned");
            if(PlayerInfo[playerid][pHouse1] < 999 && PlayerInfo[playerid][pHouse2] < 999)
            {
                SendClientMessage(playerid, COLOR_SYSTEM, "You already own 2 houses.");
                return 1;
            }
            if(GetPlayerCash(playerid) >= HouseInfo[house][hPrice])
            {
                DestroyDynamicPickup(housepickup[house]);
                Delete3DTextLabel(housetext[house]);
                new housetype[128];
                if(HouseInfo[house][hType] != 0)
                {
                    if(HouseInfo[house][hType] == 1) { housetype = "Un-Furnished"; }
                    else if(HouseInfo[house][hType] == 2) { housetype = "Furnished"; }
                }
                else { housetype = "Empty House"; }
                format(string, sizeof(string), "%s\nOwner: %s\n%s House", HouseInfo[house][hName],GetPlayerNameEx(playerid),housetype);
                housetext2[house] = Create3DTextLabel(string,COLOR_LAWNGREEN,HouseInfo[house][hEnterX], HouseInfo[house][hEnterY], HouseInfo[house][hEnterZ],40.0,0);
                housepickup2[house] = CreateDynamicPickup(1272, 23,  HouseInfo[idx][hEnterX],HouseInfo[idx][hEnterY],HouseInfo[idx][hEnterZ],-1,-1,-1,40.0);
                GivePlayerCash(playerid, -HouseInfo[house][hPrice]);
                if(PlayerInfo[playerid][pHouse1] == 999)
                {
                    HouseInfo[house][hOwned] = 1;
                    PlayerInfo[playerid][pHouse1] = house;
                    HouseInfo[house][hProducts] = 500;
                    HouseInfo[house][hOwner] = GetPlayerNameEx(playerid);
                    strmid(HouseInfo[house][hOwner], GetPlayerNameEx(playerid), 0, strlen(GetPlayerNameEx(playerid)), 255);
                    SaveHouses();
                    InfoBoxForPlayer(playerid, "You bought a house, you can view your house commands using /househelp");
                    return 1;
                }
                if(PlayerInfo[playerid][pHouse2] == 999)
                {
                    HouseInfo[house][hOwned] = 1;
                    PlayerInfo[playerid][pHouse2] = house;
                    HouseInfo[house][hProducts] = 500;
                    HouseInfo[house][hOwner] = GetPlayerNameEx(playerid);
                    strmid(HouseInfo[house][hOwner], GetPlayerNameEx(playerid), 0, strlen(GetPlayerNameEx(playerid)), 255);
                    SaveHouses();
                    InfoBoxForPlayer(playerid, "You bought a house, you can view your house commands using /househelp");
                    return 1;
                }
            }
        }
Reply


Messages In This Thread
House Save/Load System - by Phil_Cutcliffe - 14.02.2014, 15:21
Re: House Save/Load System - by CuervO - 14.02.2014, 15:26
Re: House Save/Load System - by Phil_Cutcliffe - 14.02.2014, 15:30
Re: House Save/Load System - by CuervO - 14.02.2014, 15:36
Re: House Save/Load System - by Phil_Cutcliffe - 14.02.2014, 15:41
Re: House Save/Load System - by CuervO - 14.02.2014, 15:51
Re: House Save/Load System - by Phil_Cutcliffe - 14.02.2014, 15:55
Re: House Save/Load System - by CuervO - 14.02.2014, 16:00
Re: House Save/Load System - by Phil_Cutcliffe - 14.02.2014, 16:35
Re: House Save/Load System - by CuervO - 14.02.2014, 17:04

Forum Jump:


Users browsing this thread: 4 Guest(s)