Skip to main content
GameDev.net gamedev.net
🔒 Locked

Event Based Scripting?

Started by pcmattman Jun 27, 2007 at 9:45 PM 1 replies 1.2k views
Original Post
pcmattman
pcmattman
At the moment I've hit a problem. I can load in level data, display it and the player can move around within it (I haven't yet added collision detection). However, I'm having trouble with adding interactivity. In my level files, which at the moment are nothing more than INI files full of level information, I have a section called "Triggers". Each trigger has a name, action text (The game prints "Press E to " before this text) and a position. So far, I have the engine detecting the presence of the player within 1 unit of the trigger, and it handles all this well. However, I have no idea how to get from actually having the trigger to being able to do something with it. I would like to be able to write something like this in the level script:

onhit(trigger0)
    subtitle( "Triggers are working properly!" )
    end

Of course, I'm happy if I can do something similar with less work. The main problem I have with using something like Python is the direct links it needs to make to my game, and how to make it event driven. Any ideas?
AdamGL
AdamGL
In your level file, you could include the trigger's x and y position. On game start up, load all of the level data into memory and then each time the character moves, check whether or not his x and y position is the same as the trigger's x and y position. You do this by looping through an array of the trigger's locations until you have a match. When you have a match, just use the index of the array in another array that holds the text. Confused...?

//Pseudo codestruct Triggers{   int x[50];   int y[50];   int text[50];};void main (){   //loads all triggers into the structure above   Triggers triggers = loadTriggersFromFile("level1.lvl");      character.move();      //set to -1 because an array can't possibly have that index. Safeguard if no     //trigger is hit.   int hitTrigger = -1;       for(int n = 0; n < 50; n++)   {      if(character.x == triggers.x[n] && character.y == triggers.y[n])      {           hitTrigger = n;      }   }   if(hitTrigger == -1)       display("No trigger hit");   else       display(triggers.text[n]);}


EDIT: I just realized you wanted to make your own script. That takes lots of work and is usually needed for reusable projects, kind of how game engines are designed for many games. You won't need to make that kind of script if you code your game around this problem. My example should get you started. The main thing to consider is to make a trigger file that contains generic action variables, and then values for those variables based on what you want the trigger to do. They should be preset, mind you, something like:

[Trigger 1] : standard
location_x = 23
location_y = 45;
color = red;
[Trigger 1] : active
color = blue;

In this example, the trigger is initialized to the x and y coordinates specified and its colour is set to red. When the trigger is activated, its color turns blue.

In the game file itself, each trigger will already have these properties, and when the trigger is hit, according to my above example, you simply change its colour to reflect the activated state's colour.
bi-FreeSoftware
pcmattman
pcmattman
Thanks for the pointers. Before I posted, this is what my level files looked like:

[LevelData]Name=SurfaceTension Test Level[LevelWorkings]Lighting=1Grid=0[Shapes]ShapeCount=2Shape0_type=XFileShape0_file=Assets/Meshes/Teapot_Hi.xShape0_x=0.0Shape0_y=0.0Shape0_z=10.0Shape0_scalex=0.05Shape0_scaley=0.05Shape0_scalez=0.05Shape0_tex=noneShape1_type=XFileShape1_file=Assets/Meshes/floor.xShape1_x=0.0Shape1_y=0.0Shape1_z=0.0Shape1_scalex=1.0Shape1_scaley=1.0Shape1_scalez=1.0Shape1_tex=none[Models]ModelCount=1Model0_file=Assets/Models/pillar.md2Model0_tex=Assets/Textures/auvBG.pngModel0_x=5.0Model0_y=0.0Model0_z=2.0Model0_sx=0.05Model0_sy=0.05Model0_sz=0.05[Lights]LightCount=1Light0_type=pointLight0_x=0.0Light0_y=30Light0_z=5.0Light0_a0=0.9Light0_range=31.0Light0_r=1.0Light0_g=1.0Light0_b=1.0[Triggers]TriggerCount=1Trigger0_name=Test1Trigger0_ActionText=test triggersTrigger0_x=5.0Trigger0_y=1.0Trigger0_z=2.0


You can see I'd already put in the triggers' x, y and z position.

My main problem is that I do want this to be reusable, and my friend (who I'm making this with) would probably want to edit behaviors of the level.

I also really don't want to hard-code in all the level stuff. Perhaps I could have a set of default trigger actions, and have something like:
Trigger0_type=SpeechTrigger0_SpeechText=I wonder why I didn't notice that before?


Hang on, I think I just solved my own problem... Thanks for your tips!

As an afterthought, this is all really beginning to make sense now :D. It's quite amazing, I think. I've just been doing OS development for six months and come to making a game and have had no end of trouble :D

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.