Game Development Community

Where does generic game code go?

by Bruno Campolo · in Torque Game Builder · 01/15/2008 (6:59 pm) · 12 replies

Hi, I've been using tgb for a couple months part time and have scripted alot of code for my player object and my enemy objects, but I don't know what the preferred way to script generic game logic is:

In other words, where does code like the logic for a game timer countdown go, or other logic that is not specific to any objects such as player, enemy, etc, go? I know I could put it in any number of places and it would worke, but does it belong in a specific place or to a specific class?

When I code a function for my player it looks something like:

function playerClass:foo(%this)
{
}

but what class does the generic stuff belong to?

I hope my question is clear. I am an experienced programmer and have worked on games for a few years, so I really just need to know how experienced tgb scripters would do something like this.

- Bruno

About the author

Creator of Bantam City Games, a one-man independent game development studio. To learn more, check out 'A Game Developer's Saga', a game development blog at: http://www.bantamcity.com/blog


#1
01/15/2008 (7:15 pm)
It doesn't have to belong to a class. We keep a couple of files with things for game-specific formulas, et cetera. Actually, that one is called 'math.cs'.
#2
01/15/2008 (10:30 pm)
I would put this in a seperate script file, not in one that is part of a default project. This way you can easily move it over to a new project.
#3
01/16/2008 (7:09 am)
Thanks, but say I have some code that needs to have an onUpdate function that is not object specific, but is for the overall game. If I put something into a separate file, how do I make sure that it gets called every tick? Is there an overall game onUpdate function and equivalent callback functions?
#4
01/16/2008 (7:20 am)
Why not just use an onUpdateScene then?
#5
01/16/2008 (7:05 pm)
Hmmm... I hadn't noticed that one before. It seems like it might work, but the documentation I just read says that it gets called once per frame which is machine dependent instead of once per tick which would be machine independent.

Is this what others are using?
#6
01/16/2008 (7:39 pm)
Sorry, I should have said 'onUpdate'.
#7
01/17/2008 (4:50 am)
Jason, it looks like onUpdate needs to be declared on a particular object/class, but the logic that I would like to implement is not specific to any of my individual game objects but applies to the overall game... like a timer countdown, etc. Which object/class would I declare the onUpdate function?
#8
01/17/2008 (6:37 am)
Oy veh. It does need to be called on an object. You're really approaching this in a dogmatic way. Just make a class, let it update what you need and be done with it. It's possible to make managing classes that do administrative tasks.

Just make a scriptObject to handle what you need. The solution is really quite simple, but it seems that you refuse to see the obvious answer. The view you've taken on the what defines a class is entirely too narrow.
#9
01/17/2008 (7:53 am)
Hi Jason, thanks for your explanation. I am fine with creating an administrative class that does what I want, but before I went all gung ho with that approach I wanted to find out if there was a more preferred mechanism already in place in TGB to handle that situation. I am fairly new to TGB so I figured that the more experienced users here would have this kind of thing down to a science.

I'm not as thick headed as I seem, but I do like doing things the "right way" and just didn't know what most TGB users do.

Thanks again,
Bruno
#10
01/17/2008 (1:57 pm)
Honestly, I would seriously evaluate whether or not you need to run anything every tick as it will have serious consequences. Have you considered any sort of delaying mechanism to do it less than 30 times per second?
#11
01/18/2008 (4:16 am)
Well, by default, onUpdate will happen 31.25 times per second, right?
#12
01/18/2008 (5:54 am)
As an average, yes.