SA-MP Gamemodes with PHP

Quote:
Originally Posted by GWMPT
Посмотреть сообщение
I'm not sure if the version uploaded has support to table "like" dialogs.
This plugin needs a update for sure, I'll try to do a update in the upcoming days.


It's sad that nobody else picks on this project and continue developing it, like I did when lapayo released the original version.
I wish I could, but I can't, I'm working on couple of other projects,
Hope it gets updated <3
Reply

Quote:
Originally Posted by amirm3hdi
Посмотреть сообщение
Here's what getWeaponClasses() returns: (I included only 1 item)
PHP код:
function getWeaponClasses() {
    return [
        [
            
name => "Assault",
            
weapons => [[WEAPON_M4150], [WEAPON_UZI400], [WEAPON_GRENADE2]]
        ]
    ];

Well I can't find any examples for dialog style 5...
But I think this is the right way to add rows, if not, can u provide an example?
This is indeed the right way of adding rows yeah, but not for style 5 i think, as GWMPT said, it's most likely not supported yet. What if you try style 2, will this work?

Also, your code looks fine, but try to use double/single quotes instead of nothing when accessing an array. Why? Because this is what happens when you don't use quotes and have like for example 'name' defined as something else: http://stackoverflow.com/a/13622685
Reply

Quote:
Originally Posted by BlackBank3
Посмотреть сообщение
This is indeed the right way of adding rows yeah, but not for style 5 i think, as GWMPT said, it's most likely not supported yet. What if you try style 2, will this work?

Also, your code looks fine, but try to use double/single quotes instead of nothing when accessing an array. Why? Because this is what happens when you don't use quotes and have like for example 'name' defined as something else: http://stackoverflow.com/a/13622685
Thanks for reply,
I ended up using the ShowPlayerDialog and works perfectly fine,
About the variables, yea I got that fixed when I saw the results .
Reply

So you can use samp natives in it normaly too? Also does it support streamer?
Reply

Quote:
Originally Posted by TwinkiDaBoss
Посмотреть сообщение
So you can use samp natives in it normaly too? Also does it support streamer?
Since the very first version, done by lapayo, the plugin had issues handling plugins callbacks and functions.
So it isn't supported for now.
Reply

Quote:
Originally Posted by GWMPT
Посмотреть сообщение
Since the very first version, done by lapayo, the plugin had issues handling plugins callbacks and functions.
So it isn't supported for now.
So plugins are not supported? How about a filterscript to handle them?
Reply

Quote:
Originally Posted by amirm3hdi
Посмотреть сообщение
So plugins are not supported? How about a filterscript to handle them?
Filterscripts are supported, but PHP has no "direct contact"(By this, i mean, Cannot execute functions or receive callbacks) with filterscripts or plugins for now.
Reply

Quote:
Originally Posted by GWMPT
Посмотреть сообщение
Filterscripts are supported, but PHP has no "direct contact"(By this, i mean, Cannot execute functions or receive callbacks) with filterscripts or plugins for now.
So I can only use a FS to handle plugins and stuff, but no way of contacting the gamemode itself
Reply

Hmm interesting, so we could have objects being loaded from a Filterscipt that has the plugin and such (using pawn). That actually makes it perfect

I wonder tho, how would you actually do some of the SQLi inside the SAMPPHP? I see some informations in the OP but its quite outdated and I dont see any SQLi examples.

For example, core PHP would be something like

Example code, wrote it on forums, not sure if its gonna work but I hope you get my point
PHP код:
function Connect() {
    
$host "localhost";
    
$user "root";
    
$pass "";
    
$db "myDB";
    
    
$conn mysqli_connect($host$user$pass$db);
    
    return 
$conn;
}
//meanwhile somewhere else...
    
$conn Connect();
    
    
$result $conn->query("SELECT * FROM users WHERE noob='Noob'");
    
    if(
$result->num_rows >= 1) {
        while(
$row $result->fetch_assoc()) {
            echo 
"Bla bla bla";
        }
    } 
Reply

Quote:
Originally Posted by TwinkiDaBoss
Посмотреть сообщение
Hmm interesting, so we could have objects being loaded from a Filterscipt that has the plugin and such (using pawn). That actually makes it perfect

I wonder tho, how would you actually do some of the SQLi inside the SAMPPHP? I see some informations in the OP but its quite outdated and I dont see any SQLi examples.

For example, core PHP would be something like

Example code, wrote it on forums, not sure if its gonna work but I hope you get my point
PHP код:
function Connect() {
    
$host "localhost";
    
$user "root";
    
$pass "";
    
$db "myDB";
    
    
$conn mysqli_connect($host$user$pass$db);
    
    return 
$conn;
}
//meanwhile somewhere else...
    
$conn Connect();
    
    
$result $conn->query("SELECT * FROM users WHERE noob='Noob'");
    
    if(
$result->num_rows >= 1) {
        while(
$row $result->fetch_assoc()) {
            echo 
"Bla bla bla";
        }
    } 
Actually I wanna use mysql functions right now, gonna test it out see what I can do,
Imma use the normal PHP mysql/mysqli functions, so no PAWNO,
I'll share the results.
Reply

Quote:
Originally Posted by amirm3hdi
Посмотреть сообщение
Actually I wanna use mysql functions right now, gonna test it out see what I can do,
Imma use the normal PHP mysql/mysqli functions, so no PAWNO,
I'll share the results.
You are making me hyped. Cant wait to see the results, thanks mate! I've searched a bit thru Git and seems that it does support both mysql and mysqli so shouldnt be a problem for it to handle it.

Also gotta look into this
https://github.com/aleksandarzivanovic/samphp

He wrote that he made some stuff that allow dynamic features (dynamic objects, 3d texts etc.)


Quote:
Originally Posted by amirm3hdi
Посмотреть сообщение
So I can only use a FS to handle plugins and stuff, but no way of contacting the gamemode itself
Looks like we will have to use SQL as contact in between PHP and Pawn haha.
Send the Request from Pawn to SQL, get the result from the SQL inside the PHP, magic hahaha
Reply

Quote:
Originally Posted by TwinkiDaBoss
Посмотреть сообщение
You are making me hyped. Cant wait to see the results, thanks mate! I've searched a bit thru Git and seems that it does support both mysql and mysqli so shouldnt be a problem for it to handle it.

Also gotta look into this
https://github.com/aleksandarzivanovic/samphp

He wrote that he made some stuff that allow dynamic features (dynamic objects, 3d texts etc.)




Looks like we will have to use SQL as contact in between PHP and Pawn haha.
Send the Request from Pawn to SQL, get the result from the SQL inside the PHP, magic hahaha
I did this:
PHP код:
Event::on("GameModeInit", function() {
    
     
$mysql mysqli_connect("127.0.0.1""samp""0000""samp");
});
Event::on("GameModeExit", function() {
    
     
mysqli_close($mysql);
}); 
I got
Код:
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in ...
I know what's the problem, but currently have no solution for it...
I can't use variable outside of events or functions...
If we can use global vars it'll work...

Alt way: Make a connection each time doing something which is bad
No way of having global variables, if you know a way, tell me.
Reply

Quote:
Originally Posted by amirm3hdi
Посмотреть сообщение
I did this:
PHP код:
Event::on("GameModeInit", function() {
    
     
$mysql mysqli_connect("127.0.0.1""samp""0000""samp");
});
Event::on("GameModeExit", function() {
    
     
mysqli_close($mysql);
}); 
I got
Код:
Warning: mysqli_close() expects parameter 1 to be mysqli, null given in ...

I know what's the problem, but currently have no solution for it...
I can't use variable outside of events or functions...
If we can use global vars it'll work...

Alt way: Make a connection each time doing something which is bad
No way of having global variables, if you know a way, tell me.
Hmm I see the problem, I understand what you mean. Well hmm.

What about returning it thru a function? (Are they even added in? Like public functions)

pawn Код:
function Connect()
    $connection = .... ; //mysql bla bla bla bla
    return $connection;
}

Event::OnSomething() {
    $conn = Connect();
}
Also any way to use call_user_func maybe?

Quote:

call_user_func('Connect');

Reply

Quote:
Originally Posted by TwinkiDaBoss
Посмотреть сообщение
Hmm I see the problem, I understand what you mean. Well hmm.

What about returning it thru a function? (Are they even added in? Like public functions)

pawn Код:
function Connect()
    $connection = .... ; //mysql bla bla bla bla
    return $connection;
}

Event::OnSomething() {
    $conn = Connect();
}
Also any way to use call_user_func maybe?
I already did that, using a function to get and set connection variable.. still null.
Imma try to dig more now
Reply

I used a function to set and get a global variable.. no luck
Reply

We could use 1 NPC as our global handler? All global variables hooked to a NPC therefore we can always manipulate them


If we can use global variable on NPC's then we could eventually connect 1 NPC at the start and then use his connection to manipulate the stuff.

This is just an example to show you what I mean, the code will not work
pawn Код:
Event::on('PlayerConnect', function($player) { //of course, all $player should be replaced with NPC
    $found = 0;
    for($i = 0; i < MAX_NPC; $i++) {
        if($NPC->IsConnected($NPC)) {
            $found += 1;
        }
    }
    if($found == 0) $NPC->connect = mysqli_connect(....); //if he is the first NPC connected, we will use him as our offical global variable handler
}


Event::on(SomethingElse, function($something) {
    mysqli_close($NPC->connect);
}
Since if we connect the NPC when the server connects, he will be always online, so we can use his variables as the global ones. Or something like that.


Altho the problem is then using this inside callbacks that dont have any sort of Players inside of them such as GameModeInit. But yeah, what do you think?
Reply

Quote:
Originally Posted by TwinkiDaBoss
Посмотреть сообщение
We could use 1 NPC as our global handler? All global variables hooked to a NPC therefore we can always manipulate them


If we can use global variable on NPC's then we could eventually connect 1 NPC at the start and then use his connection to manipulate the stuff.

This is just an example to show you what I mean, the code will not work
pawn Код:
Event::on('PlayerConnect', function($player) { //of course, all $player should be replaced with NPC
    $found = 0;
    for($i = 0; i < MAX_NPC; $i++) {
        if($NPC->IsConnected($NPC)) {
            $found += 1;
        }
    }
    if($found == 0) $NPC->connect = mysqli_connect(....); //if he is the first NPC connected, we will use him as our offical global variable handler
}


Event::on(SomethingElse, function($something) {
    mysqli_close($NPC->connect);
}
Since if we connect the NPC when the server connects, he will be always online, so we can use his variables as the global ones. Or something like that.


Altho the problem is then using this inside callbacks that dont have any sort of Players inside of them such as GameModeInit. But yeah, what do you think?
I get what you mean, but as a programmer, this is wrong and we should not even discuss using variables in a wrong way, if it doesn't work, we should not use the framework/plugin anymore...
Reply

Quote:
Originally Posted by BlackBank3
Посмотреть сообщение
What if you change use ($mysql) to use (&$mysql)?

The object example:
PHP код:
<?php
$testObject 
= new stdClass;
$testObject->name 'test';
$testObject->value true;
$testObject->function = function() {
    return 
'function works!';
};
Event::on("GameModeInit", function() use ($testObject) {  
    
print_r($testObject);
    echo 
$testObject->function(); // Call function and returns 'function works!'.
});
Hmm interesting way to do it, but why are you making a new object? Cant we just push the stuff without making it? Ie without having new stdClass?


Note: Altho hmm seems that I even get a problem with that
Код:
Fatal error: Call to undefined method stdClass::function()
Sample code
PHP код:
<?php
    
include "core/bootstrap.php";
    
    
$testObject = new stdClass
    
$testObject->pass ''
    
$testObject->function = function() { 
        echo 
$this->pass;
    }; 
    
Event::on("GameModeInit", function() use ($testObject) { 
        
$testObject->function();
    });
?>

@amirm how did you get the mysqli to work? I tried with mysqli_connect but seems that I get undefined symbol for it

PHP код:
<?php
    
include "core/bootstrap.php";
    
    
$mysql null
    
    
Event::on("GameModeInit", function() use (&$mysql) { 
        
$mysql mysqli_connect("localhost""root""""roleplay"); 
        
        if(
$mysql == true) {
            echo 
"Hello world";
        }
    });
?>
Reply

Quote:

@amirm how did you get the mysqli to work? I tried with mysqli_connect but seems that I get undefined symbol for it

PHP код:
<?php
    
include "core/bootstrap.php";
    
    
$mysql null
    
    
Event::on("GameModeInit", function() use (&$mysql) { 
        
$mysql mysqli_connect("localhost""root""""roleplay"); 
        
        if(
$mysql == true) {
            echo 
"Hello world";
        }
    });
?>
Here's how I did it:

PHP код:
$mysql;
Event::on("GameModeInit", function () use (&$mysql) {
    
$mysql mysqli_connect("127.0.0.1""samp""0000""samp");
});
Event::on("GameModeExit", function () use (&$mysql) {
    
mysqli_close($mysql);
}); 
I also queried it and it worked.
If you still can't use it, maybe your PHP version is outdated.
Reply

Quote:
Originally Posted by TwinkiDaBoss
Посмотреть сообщение
Hmm interesting way to do it, but why are you making a new object? Cant we just push the stuff without making it? Ie without having new stdClass?


Note: Altho hmm seems that I even get a problem with that
Код:
Fatal error: Call to undefined method stdClass::function()
Sample code
PHP код:
<?php
    
include "core/bootstrap.php";
    
    
$testObject = new stdClass
    
$testObject->pass ''
    
$testObject->function = function() { 
        echo 
$this->pass;
    }; 
    
Event::on("GameModeInit", function() use ($testObject) { 
        
$testObject->function();
    });
?>
My fault, you can't use functions inside a stdClass object. But it was an example of what is possible to insert inside a function parameter. You can for example insert a whole User class, and call the function of it inside that function/closure function.
Reply


Forum Jump:


Users browsing this thread: 9 Guest(s)