SA-MP Forums Archive
[Tutorial] Want to load your server objects from MySQL? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] Want to load your server objects from MySQL? (/showthread.php?tid=544230)



Want to load your server objects from MySQL? - Capua - 31.10.2014

Introduction

This tutorial covers up how to load object from MySQL (fast solution) instead of from scriptfiles or your gamemode.
Note: If you haven't set up your MySQL and Apache server yet, take a U-turn and find your way to MySQL tutorials. This tutorial is not aimed at persons with poor knowledge of MySQL.


What is required?

Let's get started!

We'll add this to the top of our gamemode script:
pawn Код:
enum oInfo
{
    ScriptID,
    ModelID,
    Float:X,
    Float:Y,
    Float:Z,
    Float:rX,
    Float:rY,
    Float:rZ
};

new object[2000][oInfo];

I assume you have "mySQL_init();" inside OnGameModeInit, so under mySQL_init line add the following:
pawn Код:
print("**Now loading CreateDynamicObject SQL**");
mysql_function_query(1, "SELECT * FROM `objects`", true, "LoadObjects", "i", sizeof(object));

Then the biggest part:

pawn Код:
forward LoadObjects(limit);
public LoadObjects(limit)
{
    new count, rows, fields, data[50];
    cache_get_data(rows, fields);
    for(new i; i < rows; i++)
    {
        if(count > limit)
        {
            printf("Number of objects exceeded limit!");
            break;
        }
        for(new h; h < sizeof(object); h++)
        {
            if(object[h][ScriptID] == 0)
            {
                cache_get_field_content(i, "ScriptID", data); object[h][ScriptID] = strval(data);
                cache_get_field_content(i, "ModelID", data); object[h][ModelID] = strval(data);
                cache_get_field_content(i, "X", data); object[h][X] = floatstr(data);
                cache_get_field_content(i, "Y", data); object[h][Y] = floatstr(data);
                cache_get_field_content(i, "Z", data); object[h][Z] = floatstr(data);
                cache_get_field_content(i, "rX", data); object[h][rX] = floatstr(data);
                cache_get_field_content(i, "rY", data); object[h][rY] = floatstr(data);
                cache_get_field_content(i, "rZ", data); object[h][rZ] = floatstr(data);
                CreateDynamicObject(object[h][ModelID], object[h][X], object[h][Y], object[h][Z], object[h][rX], object[h][rY], object[h][rZ]);
                count++;
                break;
            }
        }
    }
    printf("Current objects on the server: %d",CountDynamicObjects());
    return 1;
}

Halfway through! Coding in your gamemode part is finished and you can compile.

Next step is to create a SQL table. It can be done with notepad etc etc, but I use Pawno.
So here's the code:

PHP код:
-- phpMyAdmin SQL Dump
-- version 4.2.7.1
-- http://www.phpmyadmin.net
--
-- 
Host127.0.0.1
-- Generation TimeOct 312014 at 08:49 PM
-- Server version5.6.20
-- PHP Version5.5.15
SET SQL_MODE 
"NO_AUTO_VALUE_ON_ZERO";
SET time_zone "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- 
Database: `changeme`
--
-- --------------------------------------------------------
--
-- 
Table structure for table `objects`
--
CREATE TABLE IF NOT EXISTS `objects` (
`
ScriptIDint(10NOT NULL,
  `
ModelIDint(10NOT NULL,
  `
Xfloat NOT NULL,
  `
Yfloat NOT NULL,
  `
Zfloat NOT NULL,
  `
rXfloat NOT NULL,
  `
rYfloat NOT NULL,
  `
rZfloat NOT NULL,
  `
Commentvarchar(30NOT NULL
ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=38 ;
INSERT INTO `objects` (`ScriptID`, `ModelID`, `X`, `Y`, `Z`, `rX`, `rY`, `rZ`, `Comment`) VALUES
(985777.942, -1330.1814.251200, -1'Chicago Outfit'),
(
19410771.941, -1339.7614.25270090'Chicago Outfit'); 
Change "changeme" to whatever your database name is.
I've used 2 objects as example on how to place them in the code.


Now save the file as "objects.sql" and import it from phpMyAdmin into your already existing database. A new table named "objects" will appear in your database, inside "objects" there will be those 2 example objects, unless you changed them.

Congratulations! You have fully set up a fully functioning table for dynamic objects!



A tip:

If you have much objects you'd like to add, it would be time consuming to add them one by one. So copy & paste your object code, for example: CreateDynamicObject(14407, 1121.00830, -1194.85254, 27.83070, 0.00000, 0.00000, 90.26003); - then simply (if you use Pawno) press CTRL + H. First we need to replace CreateDynamicObject with nothing, so leave "Replace with:" empty. That was fast. Now for the part on the right, you could fully remove the "Comment" value to save time, or just suffer a bit and start editing with Replace function. I would do it like this: Replace ); with ), 'changeme'), - REMEMBER, the last line must have a semicolon in the end, every other object line has just a comma ( , ) in the ending.



Credits goes to [HiC]TheKiller for his in-game Object Maker


Re: Want to load your server objects from MySQL? - Rudy_ - 01.11.2014

What have you explained here? It's more like Copy this and paste that.


Re: Want to load your server objects from MySQL? - Capua - 01.11.2014

Quote:
Originally Posted by Rudy_
Посмотреть сообщение
What have you explained here? It's more like Copy this and paste that.
There is absolutely nothing to explain. This tutorial isn't aimed at newbies so I assume you know what those functions does. If you don't, then go read tutorials. This tutorial shows how to load server objects from MySQL. It's not a tutorial to explain functions.

Btw I'm adding credits soon.


Re: Want to load your server objects from MySQL? - JacobEdwards - 01.11.2014

It looks to me that most of this "tutorial" is just copied and pasted. Little to nothing explained.


Re: Want to load your server objects from MySQL? - Vince - 01.11.2014

http://forum.sa-mp.com/showthread.ph...ghlight=dating


Re: Want to load your server objects from MySQL? - Glossy42O - 02.11.2014

Not explained at all.


Re: Want to load your server objects from MySQL? - Rudy_ - 02.11.2014

Quote:
Originally Posted by Capua
Посмотреть сообщение
There is absolutely nothing to explain. This tutorial isn't aimed at newbies so I assume you know what those functions does. If you don't, then go read tutorials. This tutorial shows how to load server objects from MySQL. It's not a tutorial to explain functions.

Btw I'm adding credits soon.
Tutorials are For newbies, Why would one read a Tutorial if he Know how it is done?


Re: Want to load your server objects from MySQL? - Ryz - 02.11.2014

lol i have upto 3500 objects in my gamemode! very hard to write in mysql. i mean in SQL table.