05.11.2018, 15:26
Hello everyone i tried to edit the default attachments fs that comes with the server package to make it save the attachments using sqlite i have done this so far:
the pID is random just to check script
this is what i get when i spawn
PHP Code:
//-------------------------------------------------
//
// This is an example of using the EditAttachedObject functions
// to allow the player to customize their character.
//
// h02 2012
//
// SA-MP 0.3e and above
//
//-------------------------------------------------
#include <a_samp>
#define DIALOG_ATTACH_INDEX 13500
#define DIALOG_ATTACH_INDEX_SELECTION DIALOG_ATTACH_INDEX+1
#define DIALOG_ATTACH_EDITREPLACE DIALOG_ATTACH_INDEX+2
#define DIALOG_ATTACH_MODEL_SELECTION DIALOG_ATTACH_INDEX+3
#define DIALOG_ATTACH_BONE_SELECTION DIALOG_ATTACH_INDEX+4
new DB: AttachDB;
new pID;
enum AttachmentEnum
{
attachmodel,
attachname[24]
}
public OnFilterScriptInit()
{
if ((AttachDB = db_open("attachments.db")) == DB: 0)
{
print("Failed to open a connection to \"attachments.db\"");
}
else
{
db_query(AttachDB, "PRAGMA synchronous = OFF");
db_query(AttachDB, "CREATE TABLE IF NOT EXISTS attachments (ownerid INTEGER DEFAULT -1 NOT NULL, index INTEGER DEFAULT 0 NOT NULL, modelid INTEGER DEFAULT 0 NOT NULL, boneid INTEGER DEFAULT 0 NOT NULL, \
fOffsetX FLOAT, \
fOffsetY FLOAT, \
fOffsetZ FLOAT, \
fRotX FLOAT, \
fRotY FLOAT, \
fRotZ FLOAT, \
fScaleX FLOAT, \
fScaleY FLOAT, \
fScaleZ FLOAT)");
}
return 1;
}
public OnFilterScriptExit()
{
return 1;
}
public OnPlayerConnect(playerid)
{
pID = 72;
return 1;
}
public OnPlayerSpawn(playerid)
{
new buf[200], DBResult: Result;
format(buf, sizeof buf, "SELECT * FROM attachments WHERE ownerid = %d", pID);
Result = db_query(AttachDB, buf);
if (db_num_rows(Result))
{
SetPlayerAttachedObject(playerid,db_get_field_assoc_int(Result, "index"),db_get_field_assoc_int(Result, "modelid"),db_get_field_assoc_int(Result, "boneid"),db_get_field_assoc_float(Result, "fOffsetX"), db_get_field_assoc_float(Result, "fOffsetY"), db_get_field_assoc_float(Result, "fOffsetZ"), db_get_field_assoc_float(Result, "fRotX"), db_get_field_assoc_float(Result, "fRotY"), db_get_field_assoc_float(Result, "fRotZ"), db_get_field_assoc_float(Result, "fScaleX"), db_get_field_assoc_float(Result, "fScaleY"), db_get_field_assoc_float(Result, "fScaleZ"));
}
db_free_result(Result);
return 1;
}
public OnPlayerCommandText(playerid, cmdtext[])
{
if(!strcmp(cmdtext, "/attachments", true))
{
new string[128];
for(new x;x<MAX_PLAYER_ATTACHED_OBJECTS;x++)
{
if(IsPlayerAttachedObjectSlotUsed(playerid, x)) format(string, sizeof(string), "%s%d (Used)\n", string, x);
else format(string, sizeof(string), "%s%d\n", string, x);
}
ShowPlayerDialog(playerid, DIALOG_ATTACH_INDEX_SELECTION, DIALOG_STYLE_LIST, \
"{FF0000}Attachment Modification - Index Selection", string, "Select", "Cancel");
return 1;
}
return 0;
}
public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
switch(dialogid)
{
case DIALOG_ATTACH_INDEX_SELECTION:
{
if(response)
{
if(IsPlayerAttachedObjectSlotUsed(playerid, listitem))
{
ShowPlayerDialog(playerid, DIALOG_ATTACH_EDITREPLACE, DIALOG_STYLE_MSGBOX, \
"{FF0000}Attachment Modification", "Do you wish to edit the attachment in that slot, or delete it?", "Edit", "Delete");
}
else
{
new string[4000+1];
for(new x;x<sizeof(AttachmentObjects);x++)
{
format(string, sizeof(string), "%s%s\n", string, AttachmentObjects[x][attachname]);
}
ShowPlayerDialog(playerid, DIALOG_ATTACH_MODEL_SELECTION, DIALOG_STYLE_LIST, \
"{FF0000}Attachment Modification - Model Selection", string, "Select", "Cancel");
}
SetPVarInt(playerid, "AttachmentIndexSel", listitem);
}
return 1;
}
case DIALOG_ATTACH_EDITREPLACE:
{
if(response) EditAttachedObject(playerid, GetPVarInt(playerid, "AttachmentIndexSel"));
else
{
new Query[128];
RemovePlayerAttachedObject(playerid, GetPVarInt(playerid, "AttachmentIndexSel"));
format(Query, sizeof Query, "DELETE FROM attachments WHERE ownerid = %d, index = %d" , pID, GetPVarInt(playerid, "AttachmentIndexSel"));
db_query(AttachDB, Query);
}
DeletePVar(playerid, "AttachmentIndexSel");
return 1;
}
case DIALOG_ATTACH_MODEL_SELECTION:
{
if(response)
{
if(GetPVarInt(playerid, "AttachmentUsed") == 1) EditAttachedObject(playerid, listitem);
else
{
SetPVarInt(playerid, "AttachmentModelSel", AttachmentObjects[listitem][attachmodel]);
new string[256+1];
for(new x;x<sizeof(AttachmentBones);x++)
{
format(string, sizeof(string), "%s%s\n", string, AttachmentBones[x]);
}
ShowPlayerDialog(playerid, DIALOG_ATTACH_BONE_SELECTION, DIALOG_STYLE_LIST, \
"{FF0000}Attachment Modification - Bone Selection", string, "Select", "Cancel");
}
}
else DeletePVar(playerid, "AttachmentIndexSel");
return 1;
}
case DIALOG_ATTACH_BONE_SELECTION:
{
if(response)
{
SetPlayerAttachedObject(playerid, GetPVarInt(playerid, "AttachmentIndexSel"), GetPVarInt(playerid, "AttachmentModelSel"), listitem+1);
EditAttachedObject(playerid, GetPVarInt(playerid, "AttachmentIndexSel"));
SendClientMessage(playerid, 0xFFFFFFFF, "Hint: Use {FFFF00}~k~~PED_SPRINT~{FFFFFF} to look around.");
}
DeletePVar(playerid, "AttachmentIndexSel");
DeletePVar(playerid, "AttachmentModelSel");
return 1;
}
}
return 0;
}
public OnPlayerEditAttachedObject( playerid, response, index, modelid, boneid,
Float:fOffsetX, Float:fOffsetY, Float:fOffsetZ,
Float:fRotX, Float:fRotY, Float:fRotZ,
Float:fScaleX, Float:fScaleY, Float:fScaleZ )
{
new Query[200], DBResult: Result;
new debug_string[256+1];
format(debug_string,256,"SetPlayerAttachedObject(playerid,%d,%d,%d,%f,%f,%f,%f,%f,%f,%f,%f,%f)",
index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ);
print(debug_string);
SendClientMessage(playerid, 0xFFFFFFFF, debug_string);
SetPlayerAttachedObject(playerid,index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ);
SendClientMessage(playerid, 0xFFFFFFFF, "You finished editing an attached object");
format(Query, sizeof Query, "SELECT index FROM attachments WHERE ownerid = %d, index = %d", pID, index);
Result = db_query(AttachDB, Query);
if (db_num_rows(Result))
{
format(Query, sizeof Query, "UPDATE attachments SET fOffsetX = %f, fOffsetY = %f, fOffsetZ = %f, fRotX = %f, fRotY = %f, fRotZ = %f, fScaleX = %f, fScaleY = %f, fScaleX = %Z WHERE ownerid = %d, index = %d", pID, index);
db_query(AttachDB, Query);
}
else
{
format(Query, sizeof Query, "INSERT INTO attachments (ownerid,index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ) VALUES (%d,%d,%d,%d,%f,%f,%f,%f,%f,%f,%f,%f,%f)", pID,index,modelid,boneid,fOffsetX,fOffsetY,fOffsetZ,fRotX,fRotY,fRotZ,fScaleX,fScaleY,fScaleZ);// Insert into users the name and the password. The userid gets increased automatically and the admin is by default 0 value. We don't have to escape password as the hashed output provided by Whirlpool contains only alphabet characters and numbers.
db_query(AttachDB, Query);
}
db_free_result(Result);
return 1;
}
this is what i get when i spawn
Code:
[02:24:14] [db_log_queries]: SELECT * FROM attachments WHERE ownerid = 72 [02:24:14] [Warning] db_query: Query failed: no such table: attachments [02:24:14] [Error] db_num_rows(): invalid result handle 0 [02:24:14] [Error] db_free_result(): invalid result handle 0