# Triggers in Tile Map

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

## Recommended Posts

Hi, i wonder about putting a action trigger into a tile, that held in a 2d map array. like tile[128][128]. For example, when player comes to tile[29][10], how can i trigger an "x" action. i think function name would be taken from directly tile's info. then use pointers to start "x" action? example: falldownsomerocks(29, 10); thanks.

##### Share on other sites
Not entirely sure what your question is, but if you are asking how to store a function in an array, (and you are programing in C), you are looking for 'function pointers'.

In C++ you would want to look into boost::function once you have learned about function pointers.

##### Share on other sites
Quote:
 Original post by swiftcoderNot entirely sure what your question is, but if you are asking how to store a function in an array, (and you are programing in C), you are looking for 'function pointers'.In C++ you would want to look into boost::function once you have learned about function pointers.

Yes, i am using C++. And yes, i am asking about storing a function in a array. Maybe than i would point it to the real function :)

##### Share on other sites

Usually you'll have your triggers instanciated and wait until someone (the player in your case) walks into them, and if some conditions are valid (most likely you'll want your "falldownsomerocks" to be called just once...) then trigger the action.

If you can, avoid scripting your gameplay directly in code... use configuration files or something, then maybe use a factory to instanciate your triggers.

Hope this helps

##### Share on other sites
I agree, I don't think function pointers are a good approach. Of course it depends on the complexity of your game, but writing functions for every situation is going to be annoying in the end. You're better off designing an entity system where various entities can trigger and interact with each other.

An example: you write an Entity class, containing a virtual think() and trigger() function and a location and what other data every entity would need.
You then derive a Trigger class from it, which contains a list of pointers to entities (it's targers) and has a rectangle, or shape, to check collision against. You give it a think() function of it's own, where it checks if it's hit by the player. When it is, it calls the trigger() function of the entities it is configured to trigger.
The Rock (or probably better, FallingObject) class, also derived from Entity, has a trigger() function of it's own, that makes it start to fall.

And indeed, initializing a triggers target and shape by reading it's properties from a script or level file isn't a bad idea. Level-designers usually don't like to modify code to create levels. :)

Above is just an example, but I hope you get the idea. Thinking a little more object-oriented...

##### Share on other sites
Thank you very much, you helped me so much :).

-So, how bad it would be if i...

make a triggermanager class that has got addtrigger, update etc. functions. (addtrigger function creates a new trigger object and let it read its own properties from a conf file, and adds it to vector container for update)(update function does a for loop in vector's items. updates every single triggers state)

make a trigger class that has got update, load etc. functions. (update function checks if someone collides with trigger area, amd if so, runs action function) (load function is confusing please describe :))

thanks again.

##### Share on other sites
If you want to do it in an array of somekind it might be worth doing:

[tilex][tiley][function_number]

where the function number is referenced to another file containing a list of all the functions on that map or globablly (with 0 being none)

##### Share on other sites
You can store trigger properties in an .INI file.
http://www.codeproject.com/useritems/IniReader.asp has some classes that help you do this. Editing INI files is relatively easy, their syntax is something like:
[General]Sound=FalseBackgroundSound=FalseFlybyHelp=FalseInSpeed=9OutSpeed=10SortDuringDeal=FalseAutoDiscard=FalseRevealSkill=TrueRevealAllHands=FalseSaveIcon=True[Misc]Cardback=14FirstSession=FalseLastSavedGameNumber=2[Rules]StartingCash=999999Ante=10MaxRaises=3MinBet=10MediumBet=25MaxBet=50CheckRaise=TrueDrawFourOnAce=TrueLastRaiserStarts=FalseRedealAfterAllCheck=True[Player 1]Name=Stranger[Player 2]Name=(random)Active=TrueSex=0Skill=0Style=0[Player 3]Name=(random)Active=TrueSex=0Skill=0Style=0[Player 4]Name=(random)Active=TrueSex=0Skill=0Style=0[Player 5]Name=(random)Active=TrueSex=0Skill=0Style=0[Player 6]Name=(random)Active=TrueSex=0Skill=0Style=0[Main Window]Left=328Top=134[Options Dialog]Left=386Top=283[Stats Window]Left=424Top=586Visible=TrueState=0[Odds Window]Left=77Top=229Visible=TrueState=0

// Some random INI file on my computer

##### Share on other sites
Quote:
 Original post by IccarusIf you want to do it in an array of somekind it might be worth doing:[tilex][tiley][function_number]where the function number is referenced to another file containing a list of all the functions on that map or globablly (with 0 being none)

At first i was thinking same with you. :) But i think making a management system for all triggers with a class would be more useful. :)

AcePilot@ The link you've given is a great source. Thank you.

##### Share on other sites
I'd definitely go with XML, as its easy to use and quite standard...

I've used TinyXML in the past, and its quite good and easy to use.

Also, I'd inverse the logic you said... I'd read a file which would call some AddTriggers, instead of having addtrigger to read a file. This way, if you want to add a new trigger in the map, all you need to do is change the configuration file.

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009328
• ### Who's Online (See full list)

There are no registered users currently online

×