Loading objects from a .txt file
#1

So I have this stock code to load objects from a text file.

Код:
#include <streamer>

#define        MAP_OBJECTS_LOCATION		"Maps/%s/objects.ini"


stock LoadMapDynamicObjects(filename[]) // Load from a .txt file in scriptfile directory (Bugged, don't know why)
{
    new File:file, line[256], modelid, Float:SpawnX, Float:SpawnY, Float:SpawnZ, Float:SpawnRotX, Float:SpawnRotY, Float:SpawnRotZ, objects_loaded;
    new fileloc[100];
    format(fileloc, sizeof(fileloc), MAP_OBJECTS_LOCATION, filename);

    file = fopen(fileloc, io_read);
    if(!file) return printf("FATAL ERROR: Failed to load object %s (File doesnt exist in scriptfiles directory)!", filename);
    while(fread(file, line))
    {
        if(sscanf(line, "dffffff", modelid, SpawnX, SpawnY, SpawnZ, SpawnRotX, SpawnRotY, SpawnRotZ))
        {
        	CreateDynamicObject(modelid, SpawnX, SpawnY, SpawnZ, SpawnRotX, SpawnRotY, SpawnRotZ);
        	printf("created | modelid: %f | spawnx: %f | spawny: %f | spawnz: %f | spawnrotx: %f | spawnroty: %f | spawnrotz: %f", modelid, SpawnX, SpawnY, SpawnZ, SpawnRotX, SpawnRotY, SpawnRotZ); // Debug	
      		objects_loaded++;
        }
    }
    fclose(file);
    printf("[MAP: %s]  %d objects loaded!", filename, objects_loaded);
    return 1;
}
And as an example when I call the function to load a specific map:

Код:
LoadMapDynamicObjects(mapname);
Example of file: (objects.ini) contains:
Код:
CreateDynamicObject(5166,703.5460205,-3315.6921387,13.1085014,0.0000000,0.0000000,0.0000000); //
CreateDynamicObject(5160,703.5654297,-3315.7128906,13.1171703,0.0000000,0.0000000,0.0000000); //
CreateDynamicObject(5167,596.1196899,-3307.6933594,15.1931553,0.0000000,0.0000000,0.0000000); //
CreateDynamicObject(5156,647.8857422,-3307.6230469,18.8172741,0.0000000,0.0000000,0.0000000); //
CreateDynamicObject(5157,756.7019653,-3307.5939941,24.8475151,0.0000000,0.0000000,0.0000000); //
CreateDynamicObject(5155,579.5029907,-3307.4985352,29.1487885,0.0000000,0.0000000,0.0000000); //
CreateDynamicObject(5158,555.4444580,-3307.7600098,19.8190804,0.0000000,0.0000000,89.9615479); //
CreateDynamicObject(5154,692.7363281,-3308.1171875,23.5348148,0.0000000,0.0000000,0.0000000); //
CreateDynamicObject(5154,639.6289062,-3307.2509766,23.4234257,0.0000000,0.0000000,0.0000000); //
CreateDynamicObject(2934,732.1699219,-3307.3906250,20.3406200,0.0000000,0.0000000,0.0000000); //
It doesn't load any objects, and from the stock functions above you can see I created a code to show if the function actually read the file properly
Код:
This one:

printf("created | modelid: %f | spawnx: %f | spawny: %f | spawnz: %f | spawnrotx: %f | spawnroty: %f | spawnrotz: %f", modelid, SpawnX, SpawnY, SpawnZ, SpawnRotX, SpawnRotY, SpawnRotZ); // Debug
It all returns to 0, don't know why:

server_log.txt
Код:
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
[15:27:12] created | modelid: 0.000000 | spawnx: 0.000000 | spawny: 0.000000 | spawnz: 0.000000 | spawnrotx: 0.000000 | spawnroty: 0.000000 | spawnrotz: 0.000000
What did I do wrong?
Reply
#2

Bump.

Any thought on this?
Reply
#3

SSCANF, usually separates values using space " ", although, in this case you need a ","
PHP код:
sscanf(line"p<,>dffffff"modelidSpawnXSpawnYSpawnZSpawnRotXSpawnRotYSpawnRotZ
Reply
#4

You need to ignore the name of the function and after the last parameter. I had written a function to move maps that conflicted and I used:
pawn Код:
new modelid, Float: p[6];
...
sscanf(line, "'CreateDynamicObject('P<),>ia<f>[6]", modelid, p);
printf("created | modelid: %i | spawnx: %f | spawny: %f | spawnz: %f | spawnrotx: %f | spawnroty: %f | spawnrotz: %f", modelid, p[0], p[1], p[2], p[3], p[4], p[5]);
and modelid is an integer so %i or %d specifier in format, you had %f
Reply
#5

The problem is that your use of sscanf is incorrect. Since the lines in the file begin with CreateDynamicObject(...) sscanf fails to parse the line as that part of the string isn't an integer (d) and therefore doesn't conform with the specifiers (dffffff).

I've came across this issue before. The solution is using sscanf's multi-delimiter feature as I've demonstrated in my code below (which works as intended in my map loading script):

pawn Код:
if(sscanf(line, "P<(),>{s[32]}iffffff", modelid, x, y, z, rx, ry, rz))
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
You need to ignore the name of the function and after the last parameter. I had written a function to move maps that conflicted and I used:
pawn Код:
new modelid, Float: p[6];
...
sscanf(line, "'CreateDynamicObject('P<),>ia<f>[6]", modelid, p);
printf("created | modelid: %i | spawnx: %f | spawny: %f | spawnz: %f | spawnrotx: %f | spawnroty: %f | spawnrotz: %f", modelid, p[0], p[1], p[2], p[3], p[4], p[5]);
and modelid is an integer so %i or %d specifier in format, you had %f
Thanks for your answer! It is now loading the files properly.

And for the others, thank you for helping me too!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)