[Tutorial] Want to load your server objects from MySQL?
#1

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?
  • BlueG's MySQL R7 plugin
  • Have your server connected to MySQL
  • Access to phpMyAdmin

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
Reply


Messages In This Thread
Want to load your server objects from MySQL? - by Capua - 31.10.2014, 21:20
Re: Want to load your server objects from MySQL? - by Rudy_ - 01.11.2014, 05:52
Re: Want to load your server objects from MySQL? - by Capua - 01.11.2014, 12:43
Re: Want to load your server objects from MySQL? - by JacobEdwards - 01.11.2014, 20:59
Re: Want to load your server objects from MySQL? - by Vince - 01.11.2014, 22:47
Re: Want to load your server objects from MySQL? - by Glossy42O - 02.11.2014, 04:12
Re: Want to load your server objects from MySQL? - by Rudy_ - 02.11.2014, 05:48
Re: Want to load your server objects from MySQL? - by Ryz - 02.11.2014, 13:16

Forum Jump:


Users browsing this thread: 1 Guest(s)