What is the equivalent of the Update method
by John Bura · in Torque X 2D · 06/23/2010 (7:07 am) · 15 replies
So I have been programming with the standard starter kits that come from Microsoft. I use the
protected override void Update(GameTime gameTime)
a lot. What is the equivalent in the Torque engine. Basically Im asking how to access the game loop.
protected override void Update(GameTime gameTime)
a lot. What is the equivalent in the Torque engine. Basically Im asking how to access the game loop.
#2
One thing I am not clear on is that I don't know what a ShedualedEventDelegate MethodToCall is.
06/23/2010 (10:51 am)
Im guess this line can help me make a game loop.if (!_instance._restartPending)
{
Game.Instance.Engine.GameTimeSchedule.Schedule(3000, _instance.ScheduledRestart);
_instance._restartPending = true;
}One thing I am not clear on is that I don't know what a ShedualedEventDelegate MethodToCall is.
#3
Alot of what you would normally do in the update method for straight XNA (game logic, input, updating positions ) has been abstracted out to parts of the Torque X engine.
you could get access to update via an override from a derrived class. (IE: TorqueGame )
put this in you game.cs file and you will have access to the game loop.
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
Torque X basicly is an engine sitting on top of the xna frameworks (draw, update calls)
you will not have access to update or draw through components as these are something internal to the torque engine.
06/23/2010 (11:04 am)
first of all what are you trying to do in the update method? Alot of what you would normally do in the update method for straight XNA (game logic, input, updating positions ) has been abstracted out to parts of the Torque X engine.
you could get access to update via an override from a derrived class. (IE: TorqueGame )
put this in you game.cs file and you will have access to the game loop.
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
Torque X basicly is an engine sitting on top of the xna frameworks (draw, update calls)
you will not have access to update or draw through components as these are something internal to the torque engine.
#4
@ 10 seconds do this.
Im also trying to play sound cues at certain times
@ 10 seconds play this sound when X enemy is destroyed
@ 20 seconds play this sound when X enemy is destroyed
I had the logic set up in a regular XNA game. I am just lost in Torque.
06/23/2010 (11:07 am)
Basically Im trying to set up at timeline@ 10 seconds do this.
Im also trying to play sound cues at certain times
@ 10 seconds play this sound when X enemy is destroyed
@ 20 seconds play this sound when X enemy is destroyed
I had the logic set up in a regular XNA game. I am just lost in Torque.
#5
you can make a componenet that checks the time on destruction and will do just that. This component would be attached to the enemy.
06/23/2010 (1:28 pm)
OK so if I get you, Enemy X will upon his demise, play sound A, but after 10 secs of gameplay he will play soundB etc?you can make a componenet that checks the time on destruction and will do just that. This component would be attached to the enemy.
#6
@10 secs into the game you want to do something (this can be done very easily with a TorqueEvent)
and then
@10 seconds into the game you want enemys to play sound 1 when they die.
@20 seconds into the game you want enemys to play sound 2 when they die.
is that correct or am i misunderstanding your timeline?
06/23/2010 (5:19 pm)
Ok from what I'm reading in that time line your trying to setup.@10 secs into the game you want to do something (this can be done very easily with a TorqueEvent)
and then
@10 seconds into the game you want enemys to play sound 1 when they die.
@20 seconds into the game you want enemys to play sound 2 when they die.
is that correct or am i misunderstanding your timeline?
#7
What I need to do is
@ 10 seconds(or any given time
Spawn certain enemies
Print text
Play certain sound effects at a certain time
Can anybody point me to a Resource or tutorial with this TorqueEvent.
06/23/2010 (8:13 pm)
That's exactly it guys :)What I need to do is
@ 10 seconds(or any given time
Spawn certain enemies
Print text
Play certain sound effects at a certain time
Can anybody point me to a Resource or tutorial with this TorqueEvent.
#8
First you will want to declare and event like so
TorqueEvent<args>
args can be any type and it is used to pass data with the event in the example i'm showing i am using an int to represent the score.
this needs to be created for every class that is going to listen for the event.
Then you setup the class to listen for the event like so.
for my GUI I put these in the _OnWake and _OnSleep methods so that when the gui is inactive/sleeping it doesn't respond to the event (even though the event may still be firing)
then you setup the class to handle the event when it gets triggerd by creating a method that takes in the event and event Data.
Now to actually trigger the event or post it you would only use one of these methods depending on how you want the event handled.
I think this gives the basic knowledge of how to setup and use an TorqueEvent.
06/24/2010 (8:02 am)
here is a quick overview of how to use TorqueEvent (at least how I use it)First you will want to declare and event like so
TorqueEvent<args>
args can be any type and it is used to pass data with the event in the example i'm showing i am using an int to represent the score.
this needs to be created for every class that is going to listen for the event.
private TorqueEvent<int> GUIScoreEvent = new TorqueEvent<int>("GUIScoreUpdate");Then you setup the class to listen for the event like so.
for my GUI I put these in the _OnWake and _OnSleep methods so that when the gui is inactive/sleeping it doesn't respond to the event (even though the event may still be firing)
//this will setup the class to recieve the event and call the UpdateScore method when it gets the event. TorqueEventManager.Instance.MgrListenEvents<int>(GUIScoreEvent, UpdateScore, null); //this will silence the event for this class //this does not stop the event from getting posted or triggered. //it only stops this class from responding to the event. TorqueEventManager.Instance.MgrSilenceEvents<int>(GUIScoreEvent, null, null);
then you setup the class to handle the event when it gets triggerd by creating a method that takes in the event and event Data.
//this will get called by the engine when the event is triggered or posted.
public void UpdateScore(string EventName, int score)
{
//the score variable is the <int> part from the event.
_playerScore1.Text = String.Format("Player Score: {0}", score.ToString());
}Now to actually trigger the event or post it you would only use one of these methods depending on how you want the event handled.
//this triggers the event and all listeners get notified immediately. TorqueEventManager.Instance.MgrTriggerEvent<int>(GUIScoreEvent, 10); // or this posts the event to the event queue (means it will get processed at some point.) TorqueEventManager.Instance.MgrPostEvent<int>(GUIScoreEvent, 10);
I think this gives the basic knowledge of how to setup and use an TorqueEvent.
#9
a Little helper function to convert seconds to milliseconds used for scheduling.
this will call SomeMethod roughly 10 seconds after this line of code is executed it may get called a littler later but never earlier.
so for your case you would want to do multiple lines for each "Event" that you want to happen in your game (I see this getting very messy with lots of "Events") I can't stress enough that the method gets call X milliseconds after the schedule method
the method declariation for the scheduler.
there isn't any thing in the engine that sets up a timeline like you want but I think with scheduling and TorqueEvents you should be able to come up with something that accomplish's the goal you have.
06/24/2010 (8:29 am)
now for scheduling.a Little helper function to convert seconds to milliseconds used for scheduling.
public int GetMillisecondsFromSeconds(int seconds)
{
return seconds * 1000;
}this will call SomeMethod roughly 10 seconds after this line of code is executed it may get called a littler later but never earlier.
so for your case you would want to do multiple lines for each "Event" that you want to happen in your game (I see this getting very messy with lots of "Events") I can't stress enough that the method gets call X milliseconds after the schedule method
Engine.RealTimeSchedule.Schedule(GetMillisecondsFromSeconds(10), SomeMethod);
the method declariation for the scheduler.
// this method gets called when the scheduled time for the method is reached.
public void SomeMethod(object sender, ScheduledEventArguments args)
{
// do whatever you want to in here.
// sender would be the class that scheduled the event if I remember correctly.
// ScheduledEventArguments would be an object that you sent as data in the Schedule method call.
}there isn't any thing in the engine that sets up a timeline like you want but I think with scheduling and TorqueEvents you should be able to come up with something that accomplish's the goal you have.
#10
I guess the best place to put this code would be in a torque component. And attach it to a blank scene object so that it is constantly running?
Also what using statement hold the Engine.Realtime function?
06/24/2010 (10:14 am)
Thank you so much for putting this here it solves a lot of my problems.I guess the best place to put this code would be in a torque component. And attach it to a blank scene object so that it is constantly running?
Also what using statement hold the Engine.Realtime function?
#11
that will allow you to use the engine.realtime method.
as for putting it into a component so that it is always running.
the scheduler is always running.
the only thing I would use a component for would be to setup the timeline.
basicly you would want to know when your scene has fully loaded and given control to the player and then you can call a method that sets up your schedule.
but I'm sure you have something in mind for how your component will work.
Feel free to drop me a line at blackpanther(at)panthers(hyphen)den(dot)com sorry for the ()'s but I hate bots that scan forums for e-mails.
06/24/2010 (11:33 am)
using GarageGames.Torque.GameUtil;that will allow you to use the engine.realtime method.
as for putting it into a component so that it is always running.
the scheduler is always running.
the only thing I would use a component for would be to setup the timeline.
basicly you would want to know when your scene has fully loaded and given control to the player and then you can call a method that sets up your schedule.
public void setupSchedule()
{
// schedule event 1,2,3,4
Engine.RealTimeSchedule.Schedule(event1time,Event1Method);
Engine.RealTimeSchedule.Schedule(event2time,Event2Method);
Engine.RealTimeSchedule.Schedule(event3time,Event3Method);
Engine.RealTimeSchedule.Schedule(event4time,Event4Method);
// etc..
}but I'm sure you have something in mind for how your component will work.
Feel free to drop me a line at blackpanther(at)panthers(hyphen)den(dot)com sorry for the ()'s but I hate bots that scan forums for e-mails.
#12
I'm just having problems with this
I'm not exactly sure what to put as the object and the ScheduledEventArguments. Plus it says the the namespace name ScheduledEventArguments could not be found.
06/24/2010 (12:00 pm)
Thank you for all of your help.I'm just having problems with this
public void SomeMethod(object sender, ScheduledEventArguments args)
{
}I'm not exactly sure what to put as the object and the ScheduledEventArguments. Plus it says the the namespace name ScheduledEventArguments could not be found.
#13
as for the namespace thing put
using GarageGames.Torque.Sim;
in the using's block at the top of the file.
Sorry I use an addon that tells me the namespaces of things I'm missing so i tend to forget that they need to be added.
so heres how it works in the background (kinda high level i'm assuming you have some programming experience.)
the Engine.RealTimeSchedule.Schedule(10000, SomeMethod); call registers an "Event" with the scheduler system.
then when the time is up it does a
SomeMethod(this,args); call which in turn calls your method. (in this case since you didn't pass any args to the scheduler to pass along to your method args will be null)
if you wanted to pass args you could do something like the following improperly formatted code.
so what this does is it passes an argument object to the scheduler (MyArgs) which in turn passes it to the SomeMethod call and you can then do processing on it to check states or whatever you want really.
06/24/2010 (12:38 pm)
John that is the method declaration you don't change "object sender, ScheduledEventArguments args" at all those are what gets passed into that method from the scheduler.as for the namespace thing put
using GarageGames.Torque.Sim;
in the using's block at the top of the file.
Sorry I use an addon that tells me the namespaces of things I'm missing so i tend to forget that they need to be added.
so heres how it works in the background (kinda high level i'm assuming you have some programming experience.)
the Engine.RealTimeSchedule.Schedule(10000, SomeMethod); call registers an "Event" with the scheduler system.
then when the time is up it does a
SomeMethod(this,args); call which in turn calls your method. (in this case since you didn't pass any args to the scheduler to pass along to your method args will be null)
if you wanted to pass args you could do something like the following improperly formatted code.
struct MyArgs
{
public bool isMenuShowing;
};
public void SetupTimeline()
{
MyArgs args = new MyArgs();
MyArgs.isMenuShowing = false;
Engine.RealTimeSchedule.Schedule(10000, SomeMethod, args);
}
public void SomeMethod(object sender, ScheduledEventArguments args)
{
MyArgs recievedArgs = args as MyArgs;
if (recievedArgs.isMenuShowing == false)
{
//show a menu or something here
}
}so what this does is it passes an argument object to the scheduler (MyArgs) which in turn passes it to the SomeMethod call and you can then do processing on it to check states or whatever you want really.
#14
I for some reason can get the same effect as
This line of code works. Does anybody know what is wrong with the other one?
So far I have a basic thing that works Ill try more in the morning :)
06/24/2010 (8:32 pm)
Actually I have never been able to use the Engine.InstanceI for some reason can get the same effect as
Game.Instance.Engine.GameTimeSchedule.Schedule(3000, SomeMethod);
This line of code works. Does anybody know what is wrong with the other one?
So far I have a basic thing that works Ill try more in the morning :)
#15
06/24/2010 (10:55 pm)
ignore Engine.Instance it was a mistype. I was doing most of that from memory.
Torque 3D Owner Henry Shilling
Smokin Skull