Game Development Community

Scripting Clarification: getFieldValue(field) -- vs -- if(field=#)

by Steve Acaster · in Torque 3D Professional · 05/16/2010 (8:54 am) · 3 replies

Looking for an idiot proof answer to a scripting question.

Is there any difference/benefit between retrieving the field's value before using the value as a variable?

eg:

%theField = %this.getFieldValue(myField);// <--this bit

if(%theField <= 3)
%this.doStuff1();

if(%theField >4 && %theField <6)
%this.doStuff2();

if(%theField >= 6)
%this.doStuff3();

versus

if(%this.myField <= 3)
%this.doStuff1();

if(%this.myField >4 && %this.myField <6)
%this.doStuff2();

if(%this.myField >= 6)
%this.doStuff3();

Both will return the same result.

Is there any benefit either way? Apart from the first being able to pass the variable (%theField) to another function?

#1
05/16/2010 (5:31 pm)
the first one has the drawback of having a hardcoded identifier while the second one can be passed etc however you want as it can be achieved through dynamic evaluation
#2
05/17/2010 (5:44 am)
I think accessing a field is cheaper than calling a function (in script). Use getFieldValue() only if the field name is dynamic.
#3
05/17/2010 (7:51 am)
Okidokee - thanks, chaps. Just something that I wondered about.

Also is there any gain from using an alias can't think of proper word rather than repeating %this.field?

eg:
%it = %this.myField;

if(%it <= 3)
%this.doStuff1();

if(%it >4 && %it <6)
%this.doStuff2();

if(%it >= 6)
%this.doStuff3();

Apart from saving on keystrokes, that is? ;)