Page 1 of 1

Newbie Question: Arrays

Posted: Mon Feb 06, 2006 6:14 am
by DevGIB
declaring arrays on items in cfg files... just wondering how its done... cos ive been having some trouble and cant figure it out

cprop onID a{1,2,3}

thats what i figured it would be but it doesnt seem to work

thanks in advanced

Posted: Mon Feb 06, 2006 6:30 am
by Pierce
If you declare it directly in a file it looks that way:

cprop onID a3:i1i2i3

Posted: Tue Feb 07, 2006 6:16 am
by DevGIB
what about strings in arrays?

Posted: Tue Feb 07, 2006 7:11 am
by Pierce
That's nearly the same except the string length:

cprop onID a3:i1S4:testi3

Posted: Tue Feb 07, 2006 8:32 am
by DevGIB
ok awesome thanks heaps man

Posted: Tue Feb 07, 2006 8:41 am
by DevGIB
hrmm i tested it and it doesnt seem to respond correctly... it consideres the number and the : to be part of the string... is that the correct syntax?

eg
s4:none
returns
4:none ingame

Posted: Tue Feb 07, 2006 9:25 am
by Austin
To make it easier, try building your variable by hand, then output it with Pack() in basic.em and youll get the string for it. ;)


Example:

Code: Select all

use os;
use basic;

program SomeProgram()
     var my_var := array{1, "help", struct{"A":=1}, dictionary{"B"=>1024}};
     Print("-> "+Pack(my_var));
endprogram

Posted: Tue Feb 07, 2006 1:28 pm
by Yukiko
Relating to this topic, does case matter in the array declaration in the file?

Is this:
cprop onID a3:i1i2i3

the same as this:
cprop onID A3:I1I2I3

?

Posted: Tue Feb 07, 2006 7:22 pm
by DevGIB
thanks austin but i have no problems using arrays in scripts... its the cfg file that i have he problems with... ill explain what im trying to do to make it alittle easier...

i want to make it so that certain things can be writen in the cfg file to trigger effects on id e.g. for gm weaps rather than having the normal id sysmessage it will say something different kinda thing... i also wanna store the name of the weap in the array in the cfg but for some reason s26:abcdefghijklmnopqrstuvwxtyz
would come through as 25:abcdefghijklmnopqrstuvwxyz and if i put any other parts of the array behind it it will add them to that string aswell...

Posted: Tue Feb 07, 2006 11:38 pm
by Jasaka
But Austin didn't said 'you must do it by script', this script what he posted is easier way to do from this { 1, 2, 3 } to this a3:i1:i2:i3.

Posted: Wed Feb 08, 2006 4:38 am
by Pierce
I am not sure if i understood you right. But i try to post a solution for a config file. I personally would prefer datafiles for such actions :wink:

If the cfg file called aaa.cfg looks that way:

Code: Select all

Test 1
{
    onID a3:i1S4:testi3
}
You will get your array with the following code:

Code: Select all

use uo;
use cfgfile;
use basic;

program test(who)

var t := ReadConfigFile("aaa");
var e := FindConfigElem(t, 1);
var s := GetConfigString(e, "onID");
var tt := unpack(s);

endprogram

Posted: Thu Feb 09, 2006 6:18 am
by Firedancer
Jasaka wrote:But Austin didn't said 'you must do it by script', this script what he posted is easier way to do from this { 1, 2, 3 } to this a3:i1:i2:i3.
true... you can type these strings manually into the config file too, but your syntax is wrong. It's: a3:i1i2i3


I have never tried if it works with capital letters though... if you want to know, why, just try it, sure simple enough to do. Anyway though, the syntax works as follows:

Arrays: a<number of array entries>:<entry1><entry2>...<entryx>
Integers: i<value>
Strings: s<length of string>:<stringtext>

so an array { 1, hello, {2,3}} stored on an item's cprop would require this line in the itemdesc.cfg

cprop mycpropname a3:i1s5:helloa2:i2i3


ps: I'm not 100% sure on strings and I got no idea how structs are stored... but above are plenty samples how you can figure it out.

Posted: Thu Feb 09, 2006 6:56 am
by Austin
If you take a data type, and pass it to the Pack() function, it will return how it looks when you type it into your config files manually.

Works on anything. Just try the code sample I gave. Faith!

Posted: Thu Feb 09, 2006 7:23 am
by DevGIB
ok well il will try the pack code you offered on my script it was just that .prop edit wasnt returning the cprop right it was turning everything in the array after s (string) into the string... ill give it a shot and let you know, thanks for your help