Game Development Community

changing a behaviorField value in script to save time

by rennie moffat · in Torque Game Builder · 03/23/2010 (2:37 pm) · 14 replies

I have a behavior tied to hundreds of sceneObjects. More specifically, hundreds of objects, use the same behavior. My problem is, I want to change a behaviorField float. I thought it I put in the value surrounded by "", so float, "75", all the values for that field on all the objects would change if I reload the game. Not so. Is there a way to make sure they all change without having to start from the beginning or doing each manually?


About the author

My thanks to Garage Games and the Garage Games Community combined with owned determination I got one game up, Temple Racer and I am looking to build more interesting, fun games for the mass market of the iOS app store.


#1
03/24/2010 (3:24 am)
If you manually set a behavior field in the first place (which is not equal to its default value), then (to my knowledge) you can't change them all to another value at once. Without going into details, it's because of the way the behaviors are created after being saved into the scene object.

I typically just bring the file into a VI editor and use a regular expression to fix this problem. If you don't have regular expression skills, you'll just have to change each one manually.

As an aside, if your objects could be created in another manner (say, array based), then it would just be a one-line fix.
#2
03/24/2010 (4:37 am)
hi thanks William.

A VI editor, i googled it but it's too early to read about, will tho. and arrays, i have no skills on currently. I imagine that will come over the next year or two.


Thanks.
#3
03/24/2010 (2:02 pm)
Regular expressions are hard to learn, but once you get a grasp, problems like this can be fixed in 2 minutes instead of hours.

One or two years is too long to learn arrays. In any computer class, this is taught early on because it has enormous power. Combined with for-loops, you can create entire worlds with just a few dozen lines of code. Looking at other code you posted this week, you are probably writing 4 times (or more) the code as you need to be writing.
#4
03/24/2010 (6:50 pm)
hmm thanks.


so would "regular expressions" be code, that uses "for" statements?


if not and if so, could you give me a small example where I might have used this to ease the amount of coding i am doing?




:)
#5
03/24/2010 (8:25 pm)
Regular Expression Tutorials: http://www.regular-expressions.info/

Regular expressions just help you manipulate text so that you can more quickly find patterns. In your case, you could find all uses of the behavior in the level file and fix them all at once.

Here's a rough, non-compilable example of building a grid-based world using arrays and for-loops.
for( %x = 0; %x < 10; %x++ )
{
  for( %y = 0; %y < 10; %y++ )
  {
    $WORLD::Grid[%x,%y] =
        new t2dStaticSprite()
        {
          scenegraph = YourSceneGraph;
          imagemap = YourTileImageMap;
          size = "1 1";
          position = %x SPC %y;
          layer = 30;
        };
    %someBehavior = YourBehavior.createInstance();
    %someBehavior.speed = 75;
    $WORLD::Grid[%x,%y].addBehavior( %someBehavior );
  }
}

Oh, you want to grow the size based upon the level? You only need to change 2 lines of code.

Oh, the variable on the behavior should be based upon position. One line of code.

You want to rotate the grid by 30 degrees? Add a "rotation = 30;" and change the position to "(%x * mCos(30) - %y * mSin(30)) SPC (%x * mSin(30) + %y * mCos(30))"; just 2 lines of code! Maybe 25 degrees would be better... a trivial change.

You can now easily access the squares surrounding the player and make them change color, size, make them spin, etc.

As with all code, what is best depends upon your project. There aren't formulas that can be followed. You have to design your game and then find the right tools (which include the right code) which best help to bring your ideas to life.
#6
03/24/2010 (9:14 pm)
This is great William,
traditionally I have been, and do, build things very "structurally" but I can see how this coding I can manipulate time space a little better. Excellent, the plans are coming together. I do have some questions tho to better understand what you have told me. If you have a minute, please read over my questions to better help me understand these concepts.








Position of Object in the Grid.
Positions are set up by %x and %y. What I am wondering, since %x and %y are part of the "for statement", they are run up intermintally, 1, 2, 3, 4, 5, until 9 is met. What I am wondering is what if my positions need to be "432.004 671.333"? How do I assure that? What I want to know is is, How does the count as it moves up be anything other than 1, 2, 3 and so on. What is the advantage?










$WORLD
What would this be in how you code William? I mean that like this... in a game, if you had a $WORLD variable, what would it include? anything I guess? A world class, could you explain you're thinking on this and how it might pertain to a regular expression, like the grid you have set up here?










%someBehavior = YourBehavior.createInstance();
why would you include this? what is the createInstance()? Can you give an example of relevant nature for this example? I think I get why you would have a new variable = YourBehavior. Are you calling a behavior here like getBehavior? Or is this just a class behaviorName?

Then, createInstance();
what is the purpose of this? what is createInstance(), an event? Say onExplode?




And thanks,
Next level, here I come!
#7
03/24/2010 (9:33 pm)
Sorry to jump in here like this but I happen to see this today as well and thought it quite funny.

http://xkcd.com/208/
#8
03/24/2010 (10:52 pm)
I love XKCD.

Like a year ago when I provided a lot of help to you, I think I may have jumped the gun here. Based upon your questions, I can see that you have a long ways to go before you'd even understand my responses.

I recommend saving this post and coming back to it when you've learned more about variables, arrays, behaviors, and design.
#9
03/25/2010 (5:58 am)
Bummer. But if you feel that way, so be it master.



:))))



#10
03/30/2010 (10:03 am)
@William, or anyone who understands array's well,
I understand you think I currently can not handle an array but it would help if you could answer this question...

if I wanted to make a grid in TorqueScript of a 17 x 8 point structure... how would I do it?
#11
03/30/2010 (11:35 am)
I have a prime example that you still can't handle arrays: your question has already been answered in this post.

Another way this is handled is by using a t2dTileMap.

It all depends upon your design.
#12
03/30/2010 (12:19 pm)
I understand that I can not yet handle arrays, I admit that in this post.

I was just hoping you could give me a clear example in my mind of how, in Torque, I would make a grid of 17 x 8 or 9 x 9 whatever.

I know you posted your example, and I understand it up until the staticsprite insertion "new" From here it is puzzling for me because, as in your example...


it seems you have done a line, 0 x 10. My problem is, that I do not understand how the computer will place these, I imagine 10 new staticSprites. Will they be placed at positions 0 1, 0 2, 0 3, up to 0 10? That would make sense, my question was more so.. and I guess I can associate 0 3 or any other variable to be 145.333 78.124. You see, my thinking was that my spacing, in my game, the positions are going to be anything but ints?


Does that shed any light on how you or someone might be able to help me? Thanks.

#13
03/31/2010 (12:36 am)
Asking if you can assign "145.333 78.124" to a sprite's position is the wrong question. It doesn't matter. I let the Magic of Coding(tm) handle the positioning, as that is beneath me.

Look back at Reply #5. You can see that I can create a rotated grid. I don't manually list the positions. I don't care what the numbers even look like.

I feel like my example *is* clear on how to create a grid. Personally, I would find it hard to make it clearer.

Since you are pressing the topic, I feel the need to be blunt. Arrays are a 3rd week topic in programming classes. You've been at this for 9 months. I still think you need to step back from making your game and learn the basics. Take a class! In 3 months, you'll have 10 times the knowledge! Feel free to ignore my advice and experience; you can only hurt yourself.
#14
03/31/2010 (3:16 am)
I hope it is not that harsh. Maybe I should just study up as you say. Currently everything I build is very physical, structural if you will. A good example of what I mean is that I have built a clock for my game However, this clock is nothing beyond one object bouncing back and forth between two others. Point is is that it is physical. I have not yet learned ways of making the computer accomplish things in more abstract coded ways.

I will look into, thanks.