Object System

Started by Diego^, Jul 26, 2015, 11:36 PM

Previous topic - Next topic

Diego^

Object System

With this simple system, you will be able to create objects in real time on your server and saved it.

function onScriptLoad()
{
db <- ConnectSQL( "DataBase.db" );
print("Object System Started...");
ObjTime <- false;
QuerySQL(db, "CREATE TABLE IF NOT EXISTS Objects ( ID NUMERIC, Model NUMERIC, PX FLOAT, PY FLOAT, PZ FLOAT, AX FLOAT, AY FLOAT, AZ FLOAT )" );
LoadObjects();
}

function onScriptUnload()
{
DisconnectSQL( db );
}

function LoadObjects()
{
try {
if ( CountObjects() != 0 )
{
    local q = QuerySQL( db, "SELECT * FROM Objects WHERE ID LIKE '%'" ), pos;
    while( GetSQLColumnData( q, 0 ) )
    {
        local model = GetSQLColumnData( q, 1 ),
        px = GetSQLColumnData( q, 2 ).tofloat(),
        py = GetSQLColumnData( q, 3 ).tofloat(),
        pz = GetSQLColumnData( q, 4 ).tofloat(),
        ax = GetSQLColumnData( q, 5 ).tofloat(),
                ay = GetSQLColumnData( q, 6 ).tofloat(),
        az = GetSQLColumnData( q, 7 ).tofloat();
           CreateObject( model, 0, Vector( px, py, pz ), 255 ).RotateToEuler( Vector( ax, ay, az ), 0 );
   GetSQLNextRow( q );
     }
        FreeSQLQuery( q );
        print( "Loaded objects: "+ CountObjects() );
 }
}
catch(e) print( "[Error] LoadObjects - " + e );
}

function CountObjects()
{
try
{
        local a = 0, q = QuerySQL( db, "SELECT * FROM Objects" );
        while ( GetSQLColumnData( q, 0 ) )
        {
                a ++;
                GetSQLNextRow( q );
        }
        return a;
FreeSQLQuery( q );
}
catch(e) print( "[Error] CountObjects - " + e );
}

function GetTok(string, separator, n, ...)
{
 local m = vargv.len() > 0 ? vargv[0] : n,
    tokenized = split(string, separator),
    text = "";

 if (n > tokenized.len() || n < 1) return null;
 for (; n <= m; n++)
 {
  text += text == "" ? tokenized[n-1] : separator + tokenized[n-1];
 }
 return text;
}

function onPlayerCommand( player, cmd, text )
{
      if ( cmd == "obj" )
{
     if ( !text ) MessagePlayer( "[Sintaxe] - /" + cmd + " <ID>", player );
else if ( ObjTime == true ) MessagePlayer( "[Error] - An object is created, use /objdel or /objsave.", player );
else
{
   OBJ <- CreateObject( text.tointeger(), player.World, Vector( player.Pos.x, player.Pos.y, player.Pos.z + 0.5 ), 255 );
           MessagePlayer( "Created Object...", player );
           ObjTime = true;
   }
}

else if ( cmd == "objmov" )
{
     if ( !text ) MessagePlayer( "[Syntax] - /" + cmd + " <x/y/z> <+/-> <value>", player );
   else {
local pos = GetTok( text, " ", 1);
if ( !pos ) MessagePlayer( "[Syntax] - /" + cmd + " <x/y/z> <+/-> <value>", player );
else if (pos != "x" && pos != "y" && pos != "z") MessagePlayer("[Error] - x/y/z.",player);
else
{
local sign = GetTok( text, " ", 2);
   if (!sign) MessagePlayer("[Error] - Use sign +/-.",player);
   else if (sign != "+" && sign != "-") MessagePlayer("[Error] - Use sign +/-.",player);
   else
   {
   local value = GetTok( text, " ", 3);
   if (!value) MessagePlayer( "[Syntax] - /" + cmd + " <x/y/z> <+/-> <value>", player );
   else
   {
   if (pos == "x")
   {
   if (sign == "-")
{
OBJ.MoveTo( Vector( OBJ.Pos.x - value.tofloat(), OBJ.Pos.y, OBJ.Pos.z ), 2800 );
    MessagePlayer( "Object Moved...", player );
}
   if (sign == "+")
   {
   OBJ.MoveTo( Vector( OBJ.Pos.x + value.tofloat(), OBJ.Pos.y, OBJ.Pos.z ), 2800 );
    MessagePlayer( "Object Moved...", player );
           }
           }
            if (pos == "y")
   {
   if (sign == "-")
{
OBJ.MoveTo( Vector( OBJ.Pos.x, OBJ.Pos.y - value.tofloat(), OBJ.Pos.z ), 2800 );
    MessagePlayer( "Object Moved...", player );
}
   if (sign == "+")
   {
   OBJ.MoveTo( Vector( OBJ.Pos.x, OBJ.Pos.y + value.tofloat(), OBJ.Pos.z ), 2800 );
    MessagePlayer( "Object Moved...", player );
           }
           }
           if (pos == "z")
   {
   if (sign == "-")
{
OBJ.MoveTo( Vector( OBJ.Pos.x, OBJ.Pos.y, OBJ.Pos.z - value.tofloat() ), 2800 );
    MessagePlayer( "Object Moved...", player );
}
   if (sign == "+")
   {
   OBJ.MoveTo( Vector( OBJ.Pos.x, OBJ.Pos.y, OBJ.Pos.z + value.tofloat() ), 2800 );
    MessagePlayer( "Object Moved...", player );
           }
           }
   }
   }
  }
}
}

else if ( cmd == "objrot" )
{
     if ( !text ) MessagePlayer( "[Syntax] - /" + cmd + " <x/y/z> <+/-> <value>", player );
   else {
local pos = GetTok( text, " ", 1);
if ( !pos ) MessagePlayer( "[Syntax] - /" + cmd + " <x/y/z> <+/-> <value>", player );
else if (pos != "x" && pos != "y" && pos != "z") MessagePlayer("[Error] - x/y/z.",player);
else
{
local sign = GetTok( text, " ", 2);
   if (!sign) MessagePlayer("[Error] - Use sign +/-.",player);
   else if (sign != "+" && sign != "-") MessagePlayer("[Error] - Use sign +/-.",player);
   else
   {
   local value = GetTok( text, " ", 3);
   if (!value) MessagePlayer( "[Syntax] - /" + cmd + " <x/y/z> <+/-> <value>", player );
   else
   {
   if (pos == "x")
   {
   if (sign == "-")
{
OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x - value.tofloat(), OBJ.RotationEuler.y, OBJ.RotationEuler.z ), 2800 );
    MessagePlayer( "Rotated Object...", player );
}
   if (sign == "+")
   {
   OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x + value.tofloat(), OBJ.RotationEuler.y, OBJ.RotationEuler.z ), 2800 );
    MessagePlayer( "Rotated Object...", player );
           }
           }
            if (pos == "y")
   {
   if (sign == "-")
{
OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x, OBJ.RotationEuler.y - value.tofloat(), OBJ.RotationEuler.z ), 2800 );
    MessagePlayer( "Rotated Object...", player );
}
   if (sign == "+")
   {
   OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x, OBJ.RotationEuler.y + value.tofloat(), OBJ.RotationEuler.z ), 2800 );
    MessagePlayer( "Rotated Object...", player );
           }
           }
           if (pos == "z")
   {
   if (sign == "-")
{
OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x, OBJ.RotationEuler.y, OBJ.RotationEuler.z - value.tofloat() ), 2800 );
    MessagePlayer( "Rotated Object...", player );
}
   if (sign == "+")
   {
   OBJ.RotateToEuler( Vector( OBJ.RotationEuler.x, OBJ.RotationEuler.y, OBJ.RotationEuler.z + value.tofloat() ), 2800 );
    MessagePlayer( "Rotated Object...", player );
           }
           }
   }
   }
  }
}
}

else if ( cmd == "objsave" )
{
if ( ObjTime == false ) MessagePlayer( "[Error] - There created objects.", player );
else {
       local id = CountObjects()+1;
                QuerySQL( db, "INSERT INTO Objects ( ID, Model, PX, PY, PZ, AX, AY, AZ ) VALUES ( '" + id + "', '" + OBJ.Model + "', '" + OBJ.Pos.x + "', '" + OBJ.Pos.y + "', '" + OBJ.Pos.z + "', '" + OBJ.RotationEuler.x + "', '" + OBJ.RotationEuler.y + "', '" + OBJ.RotationEuler.z + "'  )" );
        ObjTime = false;
        MessagePlayer( "Object Saved...", player );
}
}

else if ( cmd == "objdel" )
  {
   if ( ObjTime == false ) MessagePlayer( "[Error] - There created objects.", player );
  else {
  OBJ.Delete();
  MessagePlayer( "Object Deleted...", player );
  ObjTime = false;
  }
  }
}
BRL's Developer.

.

Quick suggestion. Don't encapsulate code that doesn't throw exceptions in a try/catch block. Like you do in the load functions for example. The SQLite plugin doesn't throw anything.
.

Thijn

Another quick suggestion: SELECT * FROM Objects WHERE ID LIKE "%" is a lot slower, then no where. If you dont specify a where condition, everything will match.

wilber32

Nice (LBR)Diego Ty friend but there is a problem in cmd is what
QuerySQL( db, "INSERT INTO Objects ( ID, Model, PX, PY, PZ, AX, AY, AZ ) VALUES ( '" + id + "', '" + OBJ.Model + "', '" + OBJ.Pos.x + "', '" + OBJ.Pos.y + "', '" + OBJ.Pos.z + "', '" + OBJ.RotationEuler.x + "', '" + OBJ.RotationEuler.y + "', '" + OBJ.RotationEuler.z + "'  )" );  in QuerySQL( db no it does not work in my scripts Just I put this QuerySQL( sqliteDB, "INSERT INTO Objcts and fixes my scripts

MatheuS

Quote from: Thijn on Jul 27, 2015, 09:53 AMAnother quick suggestion: SELECT * FROM Objects WHERE ID LIKE "%" is a lot slower, then no where. If you dont specify a where condition, everything will match.

it depends on the way you identify your database ::)
if( !sucess ) tryAgain();
Thanks to the VCMP community. It was the happiest period of my life.

Stormeus

Quote from: MatheuS on Jul 29, 2015, 02:22 AM
Quote from: Thijn on Jul 27, 2015, 09:53 AMAnother quick suggestion: SELECT * FROM Objects WHERE ID LIKE "%" is a lot slower, then no where. If you dont specify a where condition, everything will match.

it depends on the way you identify your database ::)

It really doesn't, SELECT * FROM table will always match every row if you don't specify a condition. Also, I hope you didn't make your ID column a text type.

Anik

#6
I am facing One Problem.
[SCRIPT]  [Error] CountObjects - the index 'db' does not exist
[SCRIPT]  [Error] LoadObjects - the index 'db' does not exist
But I have DB. MoreOver, when I used /obj cmd it created an object. and when I used /objsave it saved the data in  db. But its not loading when I restarts the server.
I created another database and named id "ob" and used the Database "ob" but still the same error.

KAKAN

Wow! Pr0 bump mate!
Create a new topic and ask.
oh no

Diego^

Quote from: Anik on Dec 09, 2015, 02:35 PMI am facing One Problem.
[SCRIPT]  [Error] CountObjects - the index 'db' does not exist
[SCRIPT]  [Error] LoadObjects - the index 'db' does not exist
But I have DB. MoreOver, when I used /obj cmd it created an object. and when I used /objsave it saved the data in  db. But its not loading when I restarts the server.
I created another database and named id "ob" and used the Database "ob" but still the same error.

Test on an empty script.
BRL's Developer.

W3aPoN^

Hm Thanks Nice But I Want This Like When Player Use This /addobj /delobj e.t.c Then Errors Come You Must Buy a World Usage /buyworld Then Player Can Use These Commands Can You Make It? and Yes /buyworld Must Start a Blank World No Vehicles? i Think You Can Thanks in Advance If You Make Then Please Post It Thanks :D

KAKAN

Quote from: =NK=RazaCharan** on Dec 08, 2016, 08:17 AMHm Thanks Nice But I Want This Like When Player Use This /addobj /delobj e.t.c Then Errors Come You Must Buy a World Usage /buyworld Then Player Can Use These Commands Can You Make It? and Yes /buyworld Must Start a Blank World No Vehicles? i Think You Can Thanks in Advance If You Make Then Please Post It Thanks :D
He just showed a way to load and create objects, rest is yours work. This topic name matches its post, if you want such a script, ask for it in here: http://forum.vc-mp.org/?board=37.0
oh no

kennedyarz


=RK=MarineForce

WTF, my system was working ..

but its showing eerror now in objsave cmd

Count objects parameter 1 is invalid type null except userdata.

its is also showing in your suban system..

Kelvin Account system:

in ur scripts i never get an error like this.

but in this new server i m getting....

AroliS^

:'v stop bumping and probably you did something wrong so you check it up again
Lemme love ya

=RK=MarineForce

are u kidding bro?

i re'added all things .

here: