Scripting & Events

Started by
3 comments, last by Mybowlcut 14 years, 9 months ago
I'm about to focus on getting events into my game. I've decided to start with a simple problem: getting NPCs to talk to the player via dialogue boxes. I have my primitive dialogue system up and running:
class Dialogue_Manager
: public Renderable
, public Event_Updateable
, public ticpp::File_RW
{
public:
    Dialogue_Manager();
    Dialogue_Manager(const boost_path& file_name);

    bool Is_Dialogue() const;
    void Push_Dialogue(const std::string& dialogue);
    bool Pop_Dialogue();

    void Scroll_Dialogue_Up();
    void Scroll_Dialogue_Down();

    virtual void Render(SDL_Renderer& renderer);

    virtual void Read(const boost_path& file_name);
    virtual void Write(const boost_path& file_name) const;
private:
    // ...
};



But I am trying to figure out how I would define an event. Would I even define an event structure in C++? Should it all be scripted? I'm very confused as to what belongs in the host language and what should be scripted (currently I'm planning to use GameMonkey as the scripting language if it makes any difference). I want my events system to be very flexible. Some things off the top of my head that I'm thinking events should be able to do are:
  • alter what the player possesses
  • alter their skills and attributes
  • change tiles in the level
  • start/stop animations/music/sounds
  • interact with the quest system to signal the completion of quests
  • and of course, create dialogues
Can someone please point me in the right direction? Should I just jump in there and try it? Or should I follow a tried and true process that will save me all the trouble of trying to create some crazy architecture? Is there a book that explains this? An online article? I'm really in need of some guidance with this. Cheers.

Advertisement
You could define a class like this:
class Event{    public:    virtual int type() const=0;};

and then all events inherit Event:
enum EVENT_TYPE {PLAYER_MOVE /*, other events... */};class PlayerMoveEvent: public Event{    public:    PlayerMoveEvent(float x,float y):x(x),y(y){}    int type() const { return PLAYER_MOVE;}    float x,y;};

then you need a function like
void postEvent(Object* receiver, Event* e) {...}

to post the event and delete it once it has been posted:
postEvent(Object ,new PlayerMoveEvent(10,10));

where object might itself post the event to other objects for example its children.
The receiver then uses type() to identify the Event:
if(e->type()==PLAYER_MOVE){    PlayerMoveEvent* player_move_event = static_cast<PlayerMoveEvent*>(e);    // Do something useful with player_move_event    // ...}


I wouldn't use events for most things you have listed. Events are generated when something actually has happen that might (or might not) interest other objects. PlayerMoveEvent can be used by another character to follow the player. If you want something to happen call a function:
createDialog(text,YES_NO_BUTTONS);

It's probably a good Idea to implement basic things like Event class in c++ and then make it available to the scripting engine (the other way doesn't work well in my experience), unless you are going to write the whole engine in script and think that you don't need events in c++ at all.
Hey thanks for the advice, Kambiz. It does make more sense for events to occur like that now that I think of it.

How would this all work with scripting though? I don't know what would be scripted and what wouldn't. I also don't understand which part has control.. e.g. the game engine detects input and the player controller moves the player.. is that still C++? Then the player talks to an NPC by hitting the space bar and a dialogue pops up... what is script and what is C++? Does the script control the behaviour of the game and the engine just provide the functionality behind the behaviour?

i would think that everything would be c++ up to the point where you want a varied response based on what you do.

for example, all NPC's will probably have different dialog. so controlling your chararacter, walking up to the NPC, pressing space to initiate dialog will be c++, at that point your c++ code will load a dialog script and interpret it and perform the actions in that script.

another example. you want your character to catch on fire and explode when he steps on a certain tile. the varied response here is what to do each time you step on a tile, do you catch on fire and explode, does your arms fall off, does a monster spawn, or in the large majority of cases.....nothing happens. so each tile could store a point to the script that gets executed when you step on that tile. so controlling the character, walking on the tile is controlled in c++. c++ will detect if the tile has a script, if so, then c++ will load the script, interpret it, and execute the commands in the script.
Quote:Original post by ncsu121978
i would think that everything would be c++ up to the point where you want a varied response based on what you do.

for example, all NPC's will probably have different dialog. so controlling your chararacter, walking up to the NPC, pressing space to initiate dialog will be c++, at that point your c++ code will load a dialog script and interpret it and perform the actions in that script.

another example. you want your character to catch on fire and explode when he steps on a certain tile. the varied response here is what to do each time you step on a tile, do you catch on fire and explode, does your arms fall off, does a monster spawn, or in the large majority of cases.....nothing happens. so each tile could store a point to the script that gets executed when you step on that tile. so controlling the character, walking on the tile is controlled in c++. c++ will detect if the tile has a script, if so, then c++ will load the script, interpret it, and execute the commands in the script.
Excellent, thank you. This is the kind of thing I was unsure of.

This topic is closed to new replies.

Advertisement