Junior Spellweaver
Addition to this guide:
http://forum.ragezone.com/f864/development-save-tent-1019961/
Copy prepared DBServerSaveObject and rename to DBServerSaveVariable.
Now we need to remove some parts of this code.
Step 1. First time comment 3 lines after
Step 2. We define a variable for variables.
Replace
per
Step 3. Rename column for database. Find "model" and rename to "value".
Replace
(person->GetType()->GetName())
by
(value)
Comment next line (person->JSONSaveTent...)
Step 4. Rename function for this type of request. In line BuildServerRequest find save_obj and rename to save_value.
The result should look something like:
Hint: To load variable from database you must use
Dont say "it doesn't work", all works fine. Using this you can save and load any variables: count of killed zombies, state of some door and even script codes. The last can be compiled on server side using method
call compile {code}
Sorry for my English.
http://forum.ragezone.com/f864/development-save-tent-1019961/
Copy prepared DBServerSaveObject and rename to DBServerSaveVariable.
PHP:
GameValue DBServerSaveVariable(const GameState *state, GameValuePar oper1)
{
if (!GetNetworkManager().IsServerOwner())
{
state->AccessError("dbServerSaveCharacter");
return false;
}
if (oper1.GetType() == GameArray)
{
DoAssert(oper1.GetType() == GameArray);
const GameArrayType &array = oper1;
if (!CheckSize(state, array, 2)) return false;
if (!CheckType(state, array[0], GameString)) return false;
if (!CheckType(state, array[1], GameObject)) return false;
RString uid = array[0];
InventoryItem *person = dyn_cast<InventoryItem>(GetObject(array[1]));
if (!person) return false;
JSONDocument document;
document.SetObject();
JSONAllocator& allocator = document.GetAllocator();
// keep some members outside to be consistent with Load
document.AddMember("model", cc_cast(person->GetType()->GetName()), allocator);
//document.AddMember("alive", person->Brain() && person->Brain()->LSIsAlive() ? 1 : 0, allocator);
person->JSONSaveTent(document, allocator);
JSONBuffer buffer;
JSONWriter writer(buffer);
document.Accept(writer);
RString request = buffer.GetString();
RString url, attrib;
BuildServerRequest(ESERVLET_CHARACTER,url,"save_obj");
BuildObjectUID(attrib,uid);
return GetNetworkManager().DBSave(url.Data(),attrib,request);
}
else
{
return false;
}
}
Now we need to remove some parts of this code.
Step 1. First time comment 3 lines after
PHP:
const GameArrayType &array = oper1;
Step 2. We define a variable for variables.
Replace
Code:
[COLOR=#0000BB]InventoryItem [/COLOR][COLOR=#007700]*[/COLOR][COLOR=#0000BB]person [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]dyn_cast[/COLOR][COLOR=#007700]<[/COLOR][COLOR=#0000BB]InventoryItem[/COLOR][COLOR=#007700]>([/COLOR][COLOR=#0000BB]GetObject[/COLOR][COLOR=#007700](array[[/COLOR][COLOR=#0000BB]1[/COLOR][COLOR=#007700]]));
if (![/COLOR][COLOR=#0000BB]person[/COLOR][COLOR=#007700]) return [/COLOR][COLOR=#0000BB]false[/COLOR][COLOR=#007700];[/COLOR]
PHP:
RString value = array[1];
Step 3. Rename column for database. Find "model" and rename to "value".
Replace
(person->GetType()->GetName())
by
(value)
Comment next line (person->JSONSaveTent...)
Step 4. Rename function for this type of request. In line BuildServerRequest find save_obj and rename to save_value.
The result should look something like:
PHP:
GameValue DBServerSaveObject(const GameState *state, GameValuePar oper1)
{
if (!GetNetworkManager().IsServerOwner())
{
state->AccessError("dbServerSaveCharacter");
return false;
}
if (oper1.GetType() == GameArray)
{
DoAssert(oper1.GetType() == GameArray);
const GameArrayType &array = oper1;
if (!CheckSize(state, array, 2)) return false;
if (!CheckType(state, array[0], GameString)) return false;
if (!CheckType(state, array[1], GameObject)) return false;
RString uid = array[0];
RString value = array[1];
//InventoryItem *person = dyn_cast<InventoryItem>(GetObject(array[1]));
//if (!person) return false;
JSONDocument document;
document.SetObject();
JSONAllocator& allocator = document.GetAllocator();
// keep some members outside to be consistent with Load
document.AddMember("value", cc_cast(value), allocator);
//document.AddMember("alive", person->Brain() && person->Brain()->LSIsAlive() ? 1 : 0, allocator);
//person->JSONSaveTent(document, allocator);
JSONBuffer buffer;
JSONWriter writer(buffer);
document.Accept(writer);
RString request = buffer.GetString();
RString url, attrib;
BuildServerRequest(ESERVLET_CHARACTER,url,"save_value");
BuildObjectUID(attrib,uid);
return GetNetworkManager().DBSave(url.Data(),attrib,request);
}
else
{
return false;
}
}
Hint: To load variable from database you must use
PHP:
RString retValue = value.GetString();
return retValue;
Dont say "it doesn't work", all works fine. Using this you can save and load any variables: count of killed zombies, state of some door and even script codes. The last can be compiled on server side using method
call compile {code}
Sorry for my English.