SA-MP Forums Archive
Using char on variables. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Using char on variables. (/showthread.php?tid=420163)



Using 'char' array on variables. - Activest - 03.03.2013

Hello.

Would I ask you, how can I use "char" on variables.
For an example, I did:

new Detail[ 5 char ] = {200, 1, 0, 0, 1}
and I use it like that:
GivePlayerMoney(playerid, Detail{0});

I get an error on the line: new Detail...
The error is:
initialization data exceeds declared size


Re: Using char on variables. - Misiur - 03.03.2013

I don't know much myself, but I'll tell what I know (or guess)
char calculates number of cells required to hold this much chars of packed string. Untaged values are 1 cell wide. 5 char equals 2, and you are trying to hold 5 cells in 2 cells - ain't gonna work. If you change 5 char in your current code to 20 char - it will work just fine, becuase it will equal 5 cells.

pawn Код:
new Detail[5 char] = !"Hello";
This will work, because now we have packed hello string inside this char-array. Anyway: char has 8 bits of length, so it can hold values from 0 to 255 (no negative vaules) - in your case the 500 is too big for single byte, and it would fail anyway.

I don't know if there is conversion from untagged value to char, but I you can assign values like this:

pawn Код:
new Detail[ 5 char ];
Detail{0} = 255;
Detail{1} = 1;
Detail{2} = 100;
Detail{3} = 40;
Detail{4} = 50;
GivePlayerMoney(playerid, Detail{0});
Related thingies:
https://sampforum.blast.hk/showthread.php?tid=216730
There is char operator section in pawn_language_guide.pdf
https://sampwiki.blast.hk/wiki/Keywords:Operators#char - ain't too helpful


Re: Using char on variables. - Vince - 03.03.2013

Because 5 chars isn't 5 cells. 5 chars = 2 cells. (5/4 = 1.25 but you can't have a portion of a cell). Actually I'm not sure how to properly initialize this. I assume it involves bit shifting in some way, but I have no idea in what order the variables are stored.


Re: Using char on variables. - ReVo_ - 03.03.2013

-------


Re: Using char on variables. - AndreT - 03.03.2013

AFAIK default values on "char"-arrays don't work properly. Have to initialize them somewhere else!


Re: Using char on variables. - Activest - 03.03.2013

Well, the char will work only with "MAX_PLAYERS char"?
The example that you gave me Misiur doesn't work, and I already read these guides.


Re: Using char on variables. - LarzI - 03.03.2013

Quote:
Originally Posted by Activest
Посмотреть сообщение
Well, the char will work only with "MAX_PLAYERS char"?
The example that you gave me Misiur doesn't work, and I already read these guides.
No. That's simply because you don't initialize values when declaring the array. Read Misiur's post again as he shows you how you would have to do it.


Re: Using char on variables. - Vince - 03.03.2013

Something just came to mind. I tried using this:
pawn Код:
new testchar[5 char] = !{0, 1, 254, 105, 20};
That compiled. No errors or warnings. Unsure if it works, though. Worth a shot.


Re: Using char on variables. - LarzI - 03.03.2013

Quote:
Originally Posted by Vince
Посмотреть сообщение
Something just came to mind. I tried using this:
pawn Код:
new testchar[5 char] = !{0, 1, 254, 105, 20};
That compiled. No errors or warnings. Unsure if it works, though. Worth a shot.
Tested that real quick by using 'testchar' in a simple printf, and I got the error:
Код:
 error 008: must be a constant expression; assumed zero



Re: Using char on variables. - Activest - 03.03.2013

I tested now the code below:
pawn Код:
new testchar[20 char] = {30, 1, 254, 105, 20};
    printf("%i", testchar[0]);
Yes, it's printed the number 30, but when I did print(testchar[0]) it didn't work... maybe cause I should use
the %i for the integers of the variable?

In addition, the thing I need to do is to *4 the number of the cells, right?