Torque Model View pattern
by Pedro Vicente · in iTorque 2D · 08/11/2011 (7:01 pm) · 0 replies
Hello everyone
I have a code design issue that so far I have not found a solution that I find acceptable to implement in Torque script, so just wondering if any of the Torque gurus have any suggestion.
Model View Controller is a pattern used in software engineering that separates the application logic and data from the user interface (input and presentation).
Model View Controller
This resource can be considered an example:
The Game of Life Tutorial
In it 2 separate entities are defined:
Model - a C++ class named CaModel (descendent from ScriptObject)
View - a t2dSceneGraph instance instantiated in script.
Here is the way the model is called and shown within the view.
The DrawGrid method just renders tiles in a tile layer. The grid itself is a C++ 2D array defined in the model. All works great, since I have only one "level", that is, only one t2dSceneGraph instance.
Now, for working with 2 or more levels, this approach will not work. This is because %this.model is a member of this t2dSceneGraph.
Here's a recent resource that uses 2 levels:
Torque Minimal Template -- Part 5. Levels and sound
Possible solutions:
Define this somewhere
Global variables are a bad code practice. More than this, this "model" has several methods, if later I do another "model" there will be conflicts.
Make the C++ model class a descendant of t2dSceneWindow. Every Torque app has one t2dSceneWindow instance, so it would be a matter of instantiating that class instead. However all the model data and methods have nothing to do with t2dSceneWindow.
The ideal solution would be to define a local Torque object like this
And then pass this object "somehow" to the several t2dSceneGraph instances. t2dSceneGraph inherits from SimSet, that has an "add" method, that can add a SimObject object. But this translates to adding a t2dSceneObject* in t2dSceneGraph, so the object is not actually added.
Suggestions?
I have a code design issue that so far I have not found a solution that I find acceptable to implement in Torque script, so just wondering if any of the Torque gurus have any suggestion.
The Model View Controller pattern
Model View Controller is a pattern used in software engineering that separates the application logic and data from the user interface (input and presentation).
Model View Controller
This resource can be considered an example:
The Game of Life Tutorial
In it 2 separate entities are defined:
Model - a C++ class named CaModel (descendent from ScriptObject)
View - a t2dSceneGraph instance instantiated in script.
Here is the way the model is called and shown within the view.
function CaGame::OnAdd(%this)
{
%this.model = new CaModel();
%this.model.set_value( 2, 2, 1);
%this.DrawGrid();
}The DrawGrid method just renders tiles in a tile layer. The grid itself is a C++ 2D array defined in the model. All works great, since I have only one "level", that is, only one t2dSceneGraph instance.
Now, for working with 2 or more levels, this approach will not work. This is because %this.model is a member of this t2dSceneGraph.
Here's a recent resource that uses 2 levels:
Torque Minimal Template -- Part 5. Levels and sound
Possible solutions:
Global Torque object
Define this somewhere
$model = new CaModel();
Global variables are a bad code practice. More than this, this "model" has several methods, if later I do another "model" there will be conflicts.
subclass t2dSceneWindow
Make the C++ model class a descendant of t2dSceneWindow. Every Torque app has one t2dSceneWindow instance, so it would be a matter of instantiating that class instead. However all the model data and methods have nothing to do with t2dSceneWindow.
The ideal solution would be to define a local Torque object like this
%model = new CaModel();
And then pass this object "somehow" to the several t2dSceneGraph instances. t2dSceneGraph inherits from SimSet, that has an "add" method, that can add a SimObject object. But this translates to adding a t2dSceneObject* in t2dSceneGraph, so the object is not actually added.
Suggestions?