Aller au contenu

A timer?


3 réponses à ce sujet

#1
Guest_dewkl_*

Guest_dewkl_*
  • Guests
Is there a way to create a timer? To have a script or something run after (e.g) 30 seconds (of some action)? 

#2
DavidSims

DavidSims
  • BioWare Employees
  • 196 messages
You can delay an event with a given time. The time however is not exact, particularly if sent to an object far away from the player and when there is a lot of activity happening such as battle. The proccessing of the delayed event is an AI update, and the game gets around to those when it has time. Using the area as the target object helps minimize this.

The code looks something like this:

//I like to declare my event types as constants. Keeps it easy to read. Using the custom event type
//prevents conflicts with other events. Idealy, this should be done in an include file.
const int EVENT_TYPE_MY_DELAYED_EVENT = EVENT_TYPE_CUSTOM_EVENT_1;

...
float fDelay = 5.0;//delay in seconds
event evNew = Event(EVENT_TYPE_MY_DELAYED_EVENT);
object oTarget = GetArea(GetHero());//Could be any target object.
DelayEvent(fDelay, oTarget, evNew);

If you want to pass more detail along with the event, you can set any number of paramaters on the event:

float fDelay = 5.0;//delay in seconds
event evNew = Event(EVENT_TYPE_MY_DELAYED_EVENT);
evNew = SetEventInteger(evNew, 0, 100);//Adding value 100 to index 0
object oTarget = GetArea(GetHero());//Could be any target object.
DelayEvent(fDelay, oTarget, evNew);

Within the taget objects script, you'll need to catch that event and do something with it:

const int EVENT_TYPE_MY_DELAYED_EVENT = EVENT_TYPE_CUSTOM_EVENT_1;

void main()
{
event ev = GetCurrentEvent();
int nEventType = GetEventType(ev);
int nEventHandled = FALSE;

switch(nEventType)
{
case EVENT_TYPE_MY_DELAYED_EVENT:
{
int nValue = GetEventInteger(ev,0);//nValue will be 100, the number we passed in.
//do stuff
break;
}

}
if (!nEventHandled)
{
HandleEvent(ev, RESOURCE_SCRIPT_AREA_CORE);
}
}

If you want a heartbeat, you can delay the same event back to OBJECT_SELF within that script.

Modifié par DavidSims, 23 avril 2010 - 09:40 .


#3
DavidSims

DavidSims
  • BioWare Employees
  • 196 messages
Outside of a script, use const datatype variablename = value;



in this case, I had:

const int EVENT_TYPE_MY_DELAYED_EVENT = EVENT_TYPE_CUSTOM_EVENT_1;



My best guess is you don't have any include files above that line, so the custom event 1 constant isn't defined. Try including utility_h at the top and try compiling again.

#4
DavidSims

DavidSims
  • BioWare Employees
  • 196 messages
It's in events_h, which is included by utility_h.



Could you post the code? My guess is you're doing something else wrong.