BUG: Accessing functions/properties of an attribute.

Started by monkey0506, Sat 11/08/2007 03:22:34

Previous topic - Next topic

monkey0506

There appears to be a bug when attempting to access a function or property of an attribute (of which every property of every AGS managed type is). The problem was discovered by joelphilippage and Gilbot here, I have simply diagnosed why it is occurring.

QuoteBased on my tests, it would appear the reason for this bug has to do with the way AGS handles its managed types. Every property of every managed AGS type is imported as an attribute. The way an attribute works, every time the property is accessed a function is called to either get or set the value of the property. So if we type this:

Code: ags
Game.GlobalStrings[0] = "Hello";
Game.GlobalStrings[0] = Game.GlobalStrings[0].Append(" World!");


What is actually happening (internally) is this:

Code: ags
Game.seti_GlobalStrings(0, "Hello");
Game.seti_GlobalStrings(0, Game.geti_GlobalStrings(0).Append(" World!"));


The return type of the Game.geti_GlobalStrings function is a String, however we obviously can't directly call the Append function on the returned String. This is where the bug occurs, and it probably isn't one that could easily be fixed any time soon, so the safest bet is to do the following instead:

Code: ags
Game.GlobalStrings[0] = "Hello";
Game.GlobalStrings[0] = String.Format("%s World!", Game.GlobalStrings[0]);


Which will be handled internally as:

Code: ags
Game.seti_GlobalStrings(0, "Hello");
Game.seti_GlobalStrings(0, String.Format("%s World!", Game.geti_GlobalStrings(0)));


Which is why it works.

The problem directly deals with the way attributes work which means that things like the Game.GlobalStrings are only useful for storing/retrieving text values, but attempting to call any of the normal String functions/properties, such as to retrieve the String's length, append text, etc. will cause a compile-time crash.

A workaround for the Append/AppendChar functions would be to use the Format function instead, however if you want to get the Length, access the individual Chars, or any of the other functions/properties you would first need to copy the global String into a local copy like this:

Code: ags
String gs10 = Game.GlobalStrings[10];
Display("global string 10 length: %d", gs10.Length);


As mentioned in the above quote, this would probably be difficult to fix as the way attributes are handled would have to be completely revised, but I think it is important that the bug be recognized.

SSH

Well, it would be great if Strings were first-class objects and you could do

String fred="Hello".Append(" there!");

In fact, just replace the script language with python ;)
12

Pumaman

This is a known limitation of the scripting language, it has been mentioned before. Currently you can't access a non-static property on a static property -- so because Game.GlobalStrings is static it won't work.

It's something that I could fix, but so far it hasn't been a big enough problem to be worth the effort.

monkey0506

Actually...now it seems that my way of thinking it through last night wouldn't actually be producing a compile-time error anyway... ::)

SMF spam blocked by CleanTalk