Datablocks, Global Variables, and clean code
by Vern Jensen · in Torque Game Builder · 06/18/2006 (2:37 pm) · 4 replies
One of the main concept you learn when programming is to avoid repetition. For instance, don't hard-code constants; define them in some place, and use the definition in your code.
However, I'm having difficulty discovering how to do this in Torque. In the Shooter tutorial, it tells us to add variables to our enemy ship. Then to make more enemy ships, you copy and paste this ship. So the variables we added in the tutorial -- maxX, maxY, maxSpeed, and minSpeed get copied. However, what if I decide later that I want to change the min/max speeds for all enemies? Or the maxX and maxY values? I either have to manually change ALL ships, or delete them all, change one copy, and copy and paste again. This clearly isn't "good programming style."
What I want is one place to declare the values associated with my ship, and then they can easily be changed should I ever need to do so for all ships. What's the best way to do this?
Are datablocks the way to go? If so, I have some questions:
1) Are the values in a datablock static (unchangeable)?
2) Click on a sprite. Then click Edit --> Scripting. Here there is a "Config Datablock" pop-up menu that would appear to let me select a datablock that this sprite inherits values from. Is this true? If so, how do I get the datablock I've written to appear in this menu?
Or, if there is a documentation resource that answers these questions, I'd love to be pointed to it.
While I'm on the topic of clean code, the programmer in me wants to "declare" all my global variables in a single file. Sure, you can create a new global variable at any point in any file, but then it's easy to lose track of exactly which globals you've got, and where you've put them. Is there some standard script file where one might initialize global variables, or is it just "every person do as he pleases"?
TGE seems to make it very easy to get a game up and running quickly. I just want to make sure I adopt good practices that let me make changes in the future, should I need to.
-Vern
However, I'm having difficulty discovering how to do this in Torque. In the Shooter tutorial, it tells us to add variables to our enemy ship. Then to make more enemy ships, you copy and paste this ship. So the variables we added in the tutorial -- maxX, maxY, maxSpeed, and minSpeed get copied. However, what if I decide later that I want to change the min/max speeds for all enemies? Or the maxX and maxY values? I either have to manually change ALL ships, or delete them all, change one copy, and copy and paste again. This clearly isn't "good programming style."
What I want is one place to declare the values associated with my ship, and then they can easily be changed should I ever need to do so for all ships. What's the best way to do this?
Are datablocks the way to go? If so, I have some questions:
1) Are the values in a datablock static (unchangeable)?
2) Click on a sprite. Then click Edit --> Scripting. Here there is a "Config Datablock" pop-up menu that would appear to let me select a datablock that this sprite inherits values from. Is this true? If so, how do I get the datablock I've written to appear in this menu?
Or, if there is a documentation resource that answers these questions, I'd love to be pointed to it.
While I'm on the topic of clean code, the programmer in me wants to "declare" all my global variables in a single file. Sure, you can create a new global variable at any point in any file, but then it's easy to lose track of exactly which globals you've got, and where you've put them. Is there some standard script file where one might initialize global variables, or is it just "every person do as he pleases"?
TGE seems to make it very easy to get a game up and running quickly. I just want to make sure I adopt good practices that let me make changes in the future, should I need to.
-Vern
#2
In the feature tutorials folder - config datablocks.pdf. It will answer your second question.
For your first question, values that are defined in a datablock can be changed. Most fields in a datablock also have a corresponding function which can alter the pre-defined value at any point you see fit.
06/18/2006 (3:15 pm)
Have you looked at the documentation on your hard drive that is included in the SDK? ;)In the feature tutorials folder - config datablocks.pdf. It will answer your second question.
For your first question, values that are defined in a datablock can be changed. Most fields in a datablock also have a corresponding function which can alter the pre-defined value at any point you see fit.
#3
06/18/2006 (3:30 pm)
You can't change the values in the datablock itself at runtime, but when you use a "config" datablock the values in your created object are copied from the datablock, not referenced in it. So you can later change the values in the created object. I am currently using config datablocks this way in the game I'm currently working on.
#4
>In the feature tutorials folder - config datablocks.pdf. It will answer your second question.
Yes, I've read quite a bit of it, but I completely missed the "Feature Tutorials" folder. Thanks!
>You can't change the values in the datablock itself at runtime, but when you use a "config" datablock
>the values in your created object are copied from the datablock, not referenced in it. So you can later
>change the values in the created object. I am currently using config datablocks this way in the game
>I'm currently working on.
Ahhh, works just the way I hoped it would. Thanks for the explanation!
Now I'm off to read the Datablocks tutorial...
-Vern
06/19/2006 (12:15 pm)
>Have you looked at the documentation on your hard drive that is included in the SDK? ;)>In the feature tutorials folder - config datablocks.pdf. It will answer your second question.
Yes, I've read quite a bit of it, but I completely missed the "Feature Tutorials" folder. Thanks!
>You can't change the values in the datablock itself at runtime, but when you use a "config" datablock
>the values in your created object are copied from the datablock, not referenced in it. So you can later
>change the values in the created object. I am currently using config datablocks this way in the game
>I'm currently working on.
Ahhh, works just the way I hoped it would. Thanks for the explanation!
Now I'm off to read the Datablocks tutorial...
-Vern
Torque Owner Philip Mansfield
Default Studio Name
I guess the other place you can put it is in main.cs, but really anywhere you feel comfortable with would be good.