Vice City: Multiplayer

Server Development => Scripting and Server Management => Snippet Showroom => Topic started by: Diego^ on July 27th, 2015, 12:36 AM

Title: Object System
Post by: Diego^ on July 27th, 2015, 12:36 AM
Object System

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

Code: [Select]
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;
  }
  }
}
Title: Re: Object System
Post by: . on July 27th, 2015, 09:28 AM
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.
Title: Re: Object System
Post by: Thijn on July 27th, 2015, 10:53 AM
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.
Title: Re: Object System
Post by: wilber32 on July 29th, 2015, 03:14 AM
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
Title: Re: Object System
Post by: MatheuS on July 29th, 2015, 03:22 AM
Quote from Thijn on July 27th, 2015, 10:53 AM
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.
it depends on the way you identify your database ::)
Title: Re: Object System
Post by: Stormeus on July 29th, 2015, 04:00 AM
Quote from MatheuS on July 29th, 2015, 03:22 AM
Quote from Thijn on July 27th, 2015, 10:53 AM
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.
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.
Title: Re: Object System
Post by: Anik on December 9th, 2015, 02:35 PM
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.
Title: Re: Object System
Post by: KAKAN on December 9th, 2015, 03:48 PM
Wow! Pr0 bump mate!
Create a new topic and ask.
Title: Re: Object System
Post by: Diego^ on December 12th, 2015, 05:18 PM
Quote from Anik on December 9th, 2015, 02:35 PM
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.
Test on an empty script.
Title: Re: Object System
Post by: W3aPoN^ on December 8th, 2016, 08:17 AM
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
Title: Re: Object System
Post by: KAKAN on December 8th, 2016, 08:37 AM
Quote from =NK=RazaCharan** on December 8th, 2016, 08:17 AM
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
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
Title: Re: Object System
Post by: kennedyarz on December 17th, 2016, 02:17 AM
good code thx @Diego^
Title: Re: Object System
Post by: =RK=MarineForce on May 17th, 2019, 04:55 PM
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....
Title: Re: Object System
Post by: AroliS^ on May 18th, 2019, 01:30 AM
:'v stop bumping and probably you did something wrong so you check it up again
Title: Re: Object System
Post by: =RK=MarineForce on May 18th, 2019, 04:04 AM
are u kidding bro?

i re'added all things .

here:

(https://i.ibb.co/rH4Kx40/Screenshot-1.png)
Title: Re: Object System
Post by: =RK=MarineForce on July 28th, 2019, 08:43 PM
i agian do this thing but same thing fking.