Random map
#1

Hey!

I currently have maps folder in my scriptfiles folder which has many maps with different names.. now i'm just wondering, how do you get all of these maps in script and then select random one.. thanks for help in advance !
Reply
#2

Add an array with all map names and use random.
Reply
#3

Since files aren't a database and every time you add/ remove the map from your scripfiles folder, you'll need to modify the script. Assuming that you use an array or a function with all the maps locations.

Else what you can do is make a separate file which holds all the map file names and then add/ remove when you touch the map files.

But what I recommend the most is using SQL or an actual database! This really makes your work better and come a lot handy different ways.

And don't be misguided with thinking that you need to use MySQL, since you can also use SQLite. And both are fast enough and can do what you'll need in a small sa-mp server.
Reply
#4

Ye I know how to use random, but I dont know how to create array with all map names.. havent worked much with file related stuff
Reply
#5

For ex:

map1.inc
map2.inc
map3.inc

Array:

pawn Код:
static maps[][] = {"map1.inc", "map2.inc", "map3.inc"};
CMD:

pawn Код:
cmd:map(playerid, params[])
{
    if(sscanf(params, "d", params[0])) return SendClientMessage(playerid, -1, "/map [mapid]");
    for(new i; i < MAX_OBJECTS; i++) DestroyObject(i);
    new string[14];
    format(string, sizeof(string), "../%s", maps[params[0]]);
    #include string
    return 1;
}
Or without array:

pawn Код:
cmd:map(playerid, params[])
{
    if(sscanf(params, "d", params[0])) return SendClientMessage(playerid, -1, "/map [mapid]");
    for(new i; i < MAX_OBJECTS; i++) DestroyObject(i);
    new string[14];
    format(string, sizeof(string), "../map%d.inc", params[0]);
    #include string
    return 1;
}
Reply
#6

Quote:
Originally Posted by OKStyle
Посмотреть сообщение
For ex:

map1.inc
map2.inc
map3.inc

Array:

pawn Код:
static maps[][] = {"map1.inc", "map2.inc", "map3.inc"};
CMD:

pawn Код:
cmd:map(playerid, params[])
{
    if(sscanf(params, "d", params[0])) return SendClientMessage(playerid, -1, "/map [mapid]");
    for(new i; i < MAX_OBJECTS; i++) DestroyObject(i);
    new string[14];
    format(string, sizeof(string), "../%s", maps[params[0]]);
    #include string
    return 1;
}
Or without array:

pawn Код:
cmd:map(playerid, params[])
{
    if(sscanf(params, "d", params[0])) return SendClientMessage(playerid, -1, "/map [mapid]");
    for(new i; i < MAX_OBJECTS; i++) DestroyObject(i);
    new string[14];
    format(string, sizeof(string), "../map%d.inc", params[0]);
    #include string
    return 1;
}
You do know that string is a variable, and including happens at compile time?

What you attempt here is to include the file "string", not the content of the array since that isn't known at compile time, and even if it was known it wouldn't work because you try to include additional code after the compiling is done (at runtime).
The filename for the #include directive must be a constant.

If you tried to compile it, yes it does compile, but only because "string" is a valid include name from the SAMP Server package (string.inc). No errors because it's already included.

Good idea but don't post code when drunk!

You could however make a function which switches through or checks for a given value and executes the code from the includes:

PHP код:
LoadMap(id)
{
    switch(
id)
    {
        case 
0:
        {
            
#include "map1.inc"
        
}
        case 
1:
        {
            
#include "map2.inc"
        
}
    }

But I'm pretty sure OP is talking about map (.map or whatever) files in the scriptfiles folder. That's better anyway, since hardcoded maps need a GMX for any changes you want to make. With files (or a database) you can also change the map names, add/remove names etc without touching the script.
Reply
#7

Quote:
Originally Posted by NaS
Посмотреть сообщение
You do know that string is a variable, and including happens at compile time?
Yes, I know it. My code for example "HOW IT WORKS", not for use "as-is".

In fact, I would add functions for loading objects from each map at all: LoadMap1(), LoadMap2() etc.

And he can make map/xml/file parser via cmd. But it will be harder.
Reply
#8

One method requires numbers in sequential order and the other to re-compile the script. You can use a third method: a file manager plugin: https://forum.sa-mp.com/showpost.php...90&postcount=4
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)