Invalid variable values
#1

Hello,

I am having a conflict with variables being assigned (or that is the only thing I can figure). As the script below shows, I'm creating a variable and assigning the value of "1" to it. Inside the OnFilterScriptInit function, I call a custom function, LoadMap, which gets passed that variable. All the function does it print the variable, so it would be expected to print the value "1". However, it prints a value of "109" for me. Here is the script:
pawn Код:
#include    <a_samp>


new
    g_CurMap = 1;


public OnFilterScriptInit()
{
    LoadMap(g_CurMap);
    return 1;
}

stock LoadMap(mapID)
{
    printf("mapID: %i", mapID);
}
I'm assuming there is a problem with memory of the script, however am not sure. I've been screwing around for the past hour or so trying to isolate the problem, and came across something interesting. It seems calling the printf() function before calling LoadMap, it will pass the correct value of 1. Here is the script, which prints the expected result:
pawn Код:
#include    <a_samp>


new
    g_CurMap = 1;


public OnFilterScriptInit()
{
    printf("g_CurMap: %i", g_CurMap);
    LoadMap(g_CurMap);
    return 1;
}

stock LoadMap(mapID)
{
    printf("mapID: %i", mapID);
}
Can anyone give me a reason as to why this is happening? I tried using different functions before the LoadMap function as well, but that did not fix the problem either. I suppose only functions that use the variable prior to passing it to LoadMap would fix this issue but have not tested it. My brother had a similar problem quite some time ago (memory of variables getting incorrectly assigned due to function call (assuming that is the problem), which was caused by strcat) and he never got a solution to it.

Thanks.
Reply
#2

Odd. Are you running another script? Using #emit perhaps? Might also be stack overflow from another script.
Reply
#3

I've run it on two separate gamemodes, one being blank, as well as running the script above as a gamemode itself. The same result.
Reply
#4

This is kinda odd, but I'm sure there's an explanation to it. Hmm, run this code and see what happens, I created the LoadMap function before using it:

pawn Код:
#include <a_samp>

new
    g_CurMap = 1;

stock LoadMap(mapID)
{
    printf("mapID: %i", mapID);
}

public OnFilterScriptInit()
{
    LoadMap(g_CurMap);
    return 1;
}
Reply
#5

Yeah, that works as well, I forgot to mention it.

I'm not looking for a fix, as I had one with calling printf() in OnFilterScriptInit, I'm looking for an explanation as to why it's happening.
Reply
#6

Bump.

Still interested in trying to figure out how this works.
Reply
#7

The variable gets never created, also the address of the variable stays 0
This will result in calling the first cell in the data segment which is in that cause the 'm' from the printf
Reply
#8

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
The variable gets never created, also the address of the variable stays 0
This will result in calling the first cell in the data segment which is in that cause the 'm' from the printf
Would you mind explaining to me what you mean by the variable never gets created? I declare the g_CurMap variable.
Reply
#9

Quote:
Originally Posted by Bakr
Посмотреть сообщение
Would you mind explaining to me what you mean by the variable never gets created? I declare the g_CurMap variable.
I just took a look in the assembly which you could create if you execute pawncc.exe with the parameter -a

Their was the data block missing for this variable and the address was set to 0
Therefor it would print the first data which is the 'm' from "mapID: %i"
If you used %s it would likely print "mapID: mapID: %s"
Reply
#10

Quote:
Originally Posted by ******
Посмотреть сообщение
Is that the EXACT code you run that gives this problem?
Sorry for the big delay, I just now got back and saw the reply.

Yes, that was the exact code. Ran both from a filterscript and gamemode. I noticed the code beforehand in a much larger script, but with a lot of debugging and code removal, I was able to get it down to the lines shown in the original post.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)