Jump to content
  • Advertisement
Sign in to follow this  
Madguy

Scripted events

This topic is 3671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When developing games in the past, I've typically used a scripting language built specifically for game development. Now, I'm attempting to branch out and create something from scratch using C++ and DirectX. In the scripting language, let's say I wanted an enemy to pop out and do some wacky movements when the player reached a certain point on the screen. My code would look something like this: if (player has reached position) start { enemyMovement(); } Anything within the brackets of the start command would run concurrently with the rest of the action on the screen-- the game would not pause and wait for enemyMovement() to complete before allowing everything else to continue. Right now, I'm trying to figure out the easiest way to achieve this sort of thing in C++. Any guidance would be greatly appreciated. Also if there's a common name for what I'm describing, I'd love to know what it is.

Share this post


Link to post
Share on other sites
Advertisement
You COULD use a threading library like POSTIX Threads to achieve the exact same effect*, but most C/C++ programmers go the single-threaded route and do something like this pseudo code:

main loop {
get_input();
do_one_frame_of_logic();
do_one_frame_of_sound();
draw_one_frame();
play_nice_with_multitasking(); //Sleep
}


* It depends on the language, but the script is either directly spawning another thread for every "start" or is doing "fake" multi-threading by executing pieces of code alternately. True multi-threading would achieve better performance on multi-threaed machines but slightly sacrifice it on single-threaded ones.

Share this post


Link to post
Share on other sites
Maybe I'm a little dense, but could you explain this line?

play_nice_with_multitasking(); //Sleep

Anyways, what I'm currently trying to do is recreate one of my games I did in the scripting language from scratch in C++, so if single-threaded is the preferred method then it looks like I've got my work cut out for me! :D

Share this post


Link to post
Share on other sites
Instead of just calling a function, you need to think of changing the state of the enemy. So your function may just set the state of the enemy, or you could set it directly if you're not picky about access protection. If the enemy is an object:


if(player has reached position){
enemy->goNuts();//this returns quickly, only sets state
}
or

if(player has reached position){
enemy->state = MOVING_CRAZY;
}


The next time you update the enemy, it will start executing whatever pre-defined sequence you have planned for it, cycle by cycle. Hope that all makes sense. I don't know how you're used to handling game objects, and your assumptions may differ from mine.

Share this post


Link to post
Share on other sites
You could use a behaviours system where each entity has a list of behaviours attached to it.

When you want to make an entity move attach a MoveBehaviour object. Alternatively, attach a MoveCrazyLikeBehaviour for a short period of time then either disable the behaviour or remove it.

// Behaviour base class
class Behaviour
{
private:
bool _Enabled;

protected:
virtual void DoFrameAction (Entity* ent, double time) = 0;

public:
void DoFrame (Entity* ent, double time) { if (_Enabled) DoFrameAction (ent, time); }
void Enable (void) { _Enabled = true; }
void Disable (void) { _Enabled = false; }
};

// Entity base class
class Entity
{
private:
List<Behaviour*> _Behaviours;

public:
void AttachBehaviour (Behaviour* beh) { _Behaviours.Push(beh); }
void DoBehaviours (double time) {
for List<Behaviour*>::Iterator i = _Behaviours.Begin(); i != _Behaviours.End(); i++) {
(*i)->DoFrame (this, time);
}
}
};

// Use a behaviour
int main ()
{
Entity* e = new Monster();
MoveBehaviour* mb = new MoveBehaviour();
mb->SetSpeed(20);
mb->SetDir (0, 1, 0);

e->AttachBehaviour(mb);
e->DoBehaviours();

mb->SetSpeed(-20);
e->DoBehaviours();

delete e;
delete mb;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!