Placement of Large Quantities of Scene Objects
by Thomas Pereira · in Torque Game Builder · 01/16/2007 (10:39 pm) · 9 replies
Alright, I've got a pretty simple problem which hopefully has a pretty simple solution. Basically, I've got a radar script. It goes through all the objects in the level, which currently amount to ~60, and places a t2dParticleEffect on the position calculated. This, however, causes a brief pause in the operation of the engine every second (the timer is set for 1 second). Sure, I could make the timer 10 seconds or whatever, but a brief second of lag every 10 seconds is not much better. I'm mainly wondering if anyone has a better idea to handle this problem, or some other form of insight. Heres my code:
Thanks for the help.
-Tom
function Radar::onTimer(%this) {
%objects=mainScreen.getSceneObjectList();
%pPos = $PlayerShip.getPosition();
//echo(mainScreen.getSceneObjectCount());
for (%i=0;%i<mainScreen.getSceneObjectCount()-1;%i++) {
%object=getWord(%objects,%i);
%oPos = %object.getPosition();
%position = t2dVectorAdd(%this.getPosition(),t2dVectorMult(t2dVectorSub(%opos,%ppos),".01 .01"));
//echo(%object.getClassName());
if (stricmp(%object.class,"Projectile")==false) {
fxJetBlast(%this,"RadarBlip_Projectile_Enemy.eff",0.25,1,%position);
}else{
if (!stricmp(%object.getClassName(),"t2dParticleEffect")==false) {
fxJetBlast(%this,"RadarBlip.eff",0.25,1,%position);
}
}
}
}function fxJetBlast(%this,%filename,%life,%layer,%position) {
%JetBlast = new t2dParticleEffect()
{
scenegraph = %this.scenegraph;
};
%JetBlast.loadEffect("~/data/particles/"@%filename);
%JetBlast.setEffectLifeMode("KILL", %life);
%JetBlast.setPosition(%position);
%JetBlast.setLayer(%layer);
%JetBlast.playEffect();
}Thanks for the help.
-Tom
About the author
#2
I mean, all I'm really saying is I find it hard to believe that the engine can't handle instant placement of at least 100 objects, so I figured I must be doing something wrong.
01/17/2007 (12:34 am)
I forgot to state that I know when the lag occurs. I want to know if there is a better way of handling this issue. One of my original thoughts was to make a single particle effect and create more emitters, but that will prevent some flexibility and I doubt if it will even work. I mean, all I'm really saying is I find it hard to believe that the engine can't handle instant placement of at least 100 objects, so I figured I must be doing something wrong.
#3
As I said, you should create those objects first and place it when you need ( use setVisible()/setPosition() ). There is no other way to do it. Even if you only create a new emitters in exist effect.
01/17/2007 (6:12 am)
The engine can handle 'instant placement' of at least 100 object for sure. But the engine can't create 100 objects very fast and keep fps high. There is f lot of work for one object creation. As I said, you should create those objects first and place it when you need ( use setVisible()/setPosition() ). There is no other way to do it. Even if you only create a new emitters in exist effect.
#4
For example, when you create an object that should be represented in your radar, you could create the particle effect and store it's id in a dynamic variable within the father object itself.
When you have to update the radar, you could cycle through all the objects and retrieve the id for the corresponding radar's "dot" and update it's position.
If it's still too slow, you could update only a few of the object for each radar update.
Bye,
Jacopo
01/17/2007 (6:47 am)
I agree with Igor. Avoiding the creation of the td2ParticleEffects should help.For example, when you create an object that should be represented in your radar, you could create the particle effect and store it's id in a dynamic variable within the father object itself.
When you have to update the radar, you could cycle through all the objects and retrieve the id for the corresponding radar's "dot" and update it's position.
If it's still too slow, you could update only a few of the object for each radar update.
Bye,
Jacopo
#5
You're telling me storing ~100 objects for use later is better than creating and deleting them? I'd expect that to bog down the memory a bit, but I guess it can't be that bad. Eh, alright, thanks for the help.
01/17/2007 (10:15 am)
X_XYou're telling me storing ~100 objects for use later is better than creating and deleting them? I'd expect that to bog down the memory a bit, but I guess it can't be that bad. Eh, alright, thanks for the help.
#6
If you think about it, it's not so strange.
When you move around your game's objects, you don't destroy them and recreate them in a new position, right?
Bye,
Jacopo
01/17/2007 (10:47 am)
Actually, what we are saying is that creating 100 objects and keeping them alive for the time you need them is better than creating and destroying 50 objects every second... :-)If you think about it, it's not so strange.
When you move around your game's objects, you don't destroy them and recreate them in a new position, right?
Bye,
Jacopo
#7
01/17/2007 (1:05 pm)
Hmm, didn't think about it like that. This discussion sparked a good solution to my problem. Thanks for the help gentlemen.
#8
1. Your loop index has to make a method call and a subtraction for each iteration when you say
%i < mainScreen.getSceneObjectCount() - 1
This involves the method call to get the object count, a temporary variable to hold the returned value, and the subtraction of one (potentially stored in another temporary variable). This is all calculated every time the loop iterates. Cache the value locally.
2. %this.getPosition() never changes during execution of this code yet you are spending time doing the method call every iteration of the loop. Cache this value locally as well.
3. This is getting really nitpicky, but you also have four or five string literals. I'm not familiar with the inner workings of torquescript but it is possible the interpreter is instantiating a new instance of each literal each iteration of the loop. Cache those values locally as well.
Again, you are operating on a small enough number of things that it probably makes no real world difference, but keep it in mind.
01/17/2007 (4:06 pm)
Given the number of objects you are dealing with, the following are not too serious. But if you were talking about n objects (instead of 60) then you would be worried about these things as n grows very large.1. Your loop index has to make a method call and a subtraction for each iteration when you say
%i < mainScreen.getSceneObjectCount() - 1
This involves the method call to get the object count, a temporary variable to hold the returned value, and the subtraction of one (potentially stored in another temporary variable). This is all calculated every time the loop iterates. Cache the value locally.
2. %this.getPosition() never changes during execution of this code yet you are spending time doing the method call every iteration of the loop. Cache this value locally as well.
3. This is getting really nitpicky, but you also have four or five string literals. I'm not familiar with the inner workings of torquescript but it is possible the interpreter is instantiating a new instance of each literal each iteration of the loop. Cache those values locally as well.
Again, you are operating on a small enough number of things that it probably makes no real world difference, but keep it in mind.
#9
Anyway, thanks again gents. I got it working very fast and without any perceivable slowdown.
01/17/2007 (7:32 pm)
Ah, thanks for pointing that stuff out. I've already optimized the script, but I missed %this.getPosition(). Anyway, thanks again gents. I got it working very fast and without any perceivable slowdown.
Torque Owner Igor Kuryatnikov
Create pool of effects once and change EffectLifeMode from 'kill'. Using of an exist objects
can speed up you game