Diego^

  • Jr. Member
  • LBR Member.
  • Posts: 89
Object System
« on July 27th, 2015, 12:36 AM »Last edited on March 12th, 2017, 10:46 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;
  }
  }
}
Sorry for my bad English.

.

  • VC:MP Beta Tester
  • .
  • Posts: 1,804
Re: Object System
« Reply #1, 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.
.

Thijn

  • VC:MP Beta Tester
  • Posts: 1,689
Re: Object System
« Reply #2, 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.

wilber32

  • Jr. Member
  • Posts: 56
Re: Object System
« Reply #3, 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

MatheuS

  • Wiki Contributor
  • LBR | Co-Leader & Brasil Real RPG | Developer
  • Posts: 449
Re: Object System
« Reply #4, 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 ::)
Code: [Select]
if( !sucess ) tryAgain();

Stormeus

  • VC:MP Developer
  • oh god how did i get here im not good w computer
  • Posts: 1,128
Re: Object System
« Reply #5, 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.

Anik

  • Sr. Member
  • Posts: 355
Re: Object System
« Reply #6, on December 9th, 2015, 02:35 PM »Last edited on December 9th, 2015, 02:37 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.

KAKAN

  • Wiki Contributor
  • Posts: 3,346
Re: Object System
« Reply #7, on December 9th, 2015, 03:48 PM »
Wow! Pr0 bump mate!
Create a new topic and ask.
You can contact me using Discord, at Developers.CPP( check off-board to find the invite link )
Github: https://github.com/theKAKAN

Diego^

  • Jr. Member
  • LBR Member.
  • Posts: 89
Re: Object System
« Reply #8, 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.
Sorry for my bad English.

W3aPoN^

  • Full Member
  • Posts: 105
Re: Object System
« Reply #9, 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

KAKAN

  • Wiki Contributor
  • Posts: 3,346
Re: Object System
« Reply #10, 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
You can contact me using Discord, at Developers.CPP( check off-board to find the invite link )
Github: https://github.com/theKAKAN

kennedyarz

  • Sr. Member
  • Making American Great Again
  • Posts: 334

=RK=MarineForce

  • Sr. Member
  • WAR IS HELL!
  • Posts: 448
Re: Object System
« Reply #12,  »
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....
Try to UnderStand ME!

AroliS^

  • Newbie
  • Learning of Mike Tyson K.O
  • Posts: 48
Re: Object System
« Reply #13,  »
:'v stop bumping and probably you did something wrong so you check it up again

=RK=MarineForce

  • Sr. Member
  • WAR IS HELL!
  • Posts: 448
Re: Object System
« Reply #14,  »
are u kidding bro?

i re'added all things .

here:


Try to UnderStand ME!