Game Development Community

T2D MIT and Fields

by Jason Gossiaux · in Torque 2D Beginner · 09/11/2015 (12:05 am) · 1 replies

Hello everyone. I've recently returned to using Torque, and after converting my old TBG projects over to T2D MIT I've been off and running with new development for the past few weeks. Tonight I hit a bit of an odd bump and it may be related to some of the differences between the two engines (Datablocks and persistent vs dynamic fields, for example).

From the examples and comments in the code, it appears that in Torquescript I should be able to define and reference dynamic fields without the need for .SetFieldValue() and .GetFieldValue(). And this seems to work most of the time. But today I encountered an example of this not working.

I am creating a power up sprite and immediately giving it a dynamic field %newsprite.roomID = 5; Just as an example. I can breakpoint in Torsion and see my object ID of 1510 and see that roomID = 5. I can do a 1510.dump and see the dynamic field is setup properly.

Now a little while later I want to interact with this sprite. I have its ID in a $SELECTED variable. Again, I can breakpoint and see the variable ID is correctly set. In script I can $SELECTED.dump() and see the dynamic field. However, $SELECTED.roomID is invalid. I cannot get or set it by .-referencing it.

I can however use .getFieldValue and .setFieldValue to get it.

%field = $SELECTION.getFieldValue("roomID");
%field2 = $SELECTION.roomID;

%field does not equal $field2 in this example

In Visual Studio I am able to breakpoint on the console function getFieldValue, but I'm not able to unravel what is taking place in script to de-reference .roomID. I'm not sure if this is a bug or what, but it would seem unintended. .get/setFieldValue were commonly used in TBG. I found a few forum posts saying dynamic fields were unreliable so don't use .fieldname to access them. But I saw no use of these functions in the examples provided with T2D MIT and the .h file comments indicate they should work fine as follows in simObject_ScriptBinding.h:

You would normally access dynamic fields directly `%%object.field` or
indirectly `%%object.getFieldValue(%%field)`.

Anyone have ideas? Thanks!


#1
09/11/2015 (3:29 pm)
Thank you to everyone in IRC for the help. Wanted to post my findings here.

%obj.getFieldValue("fieldname") uses a different set of C++ function calls than %obj.fieldname. In my case the ID was being returned by a world pick - and I forgot that has the potential to return a list of objects separated by spaces.

But in Torsion and apparently in the console, %obj is identifying with the first ID in the list for the purposes of watching and dump() and .getFieldValue() etc.

In the Opcode section that assembles the %obj.fieldname operation, it has an additional check that specifically nulls the object ID if the string is a collection of IDs.

Different behavior depending on how you go about doing it. What confused me was how Torsion and the Console properly resolved %obj to be just 1 ID, but %obj.fieldname resolved to null. Now I know, and so do you :)