Talk:AI:SQ pitfalls

From OpenTTD
Jump to: navigation, search

I didn't fully understand the section Classes so I made tests and this could help understanding : Eevery array, table or object used to initialise members outside the constructor are constructed once for all objects. The members are not static but their initial value is shared between instances like this example prove :

class X
{
	x = 1;
}

class A
{
	scalar = 1;
	array = [1];
	table = {x=1};
	object = X();

	function PrintContents()
	{
		AILog.Info("scalar = " + this.scalar);
		AILog.Info("array[0] = " + this.array[0]);
		AILog.Info("table.x = " + this.table.x);
		AILog.Info("object.x = " + this.object.x);
	}
}

function Test()
{
	local a1 = A(), a2 = A();
	a1.scalar = 2;
	a1.array[0] = 2;
	a1.table.x = 2;
	a1.object.x = 2;
	AILog.Info("a1:");
	a1.PrintContents();
	AILog.Info("a2:");
	a2.PrintContents();
}

which outputs

a1:
scalar = 2
array[0] = 2
table.x = 2
object.x = 2
a2:
scalar = 1
array[0] = 2
table.x = 2
object.x = 2

There is another problem which the user should be aware of : line feeds are tokens. This means that these codes does not the same thing :

return a && b;

and

return
  a &&
  b;

This may cause bugs when longs expression are to be splited. Valentin Perrelle 21:06, 28 July 2009 (UTC)

Personal tools