• Advertisement
Sign in to follow this  

Triggers in Tile Map

This topic is 4280 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

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
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.


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 this post


Link to post
Share on other sites
Storing function pointers won't help you build a trigger system.

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 this post


Link to post
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 this post


Link to post
Share on other sites
Thank you very much, you helped me so much :).

-Read trigger properties from a file sounds good. Any little code snippets about this?

-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 this post


Link to post
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 this post


Link to post
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=False
BackgroundSound=False
FlybyHelp=False
InSpeed=9
OutSpeed=10
SortDuringDeal=False
AutoDiscard=False
RevealSkill=True
RevealAllHands=False
SaveIcon=True

[Misc]
Cardback=14
FirstSession=False
LastSavedGameNumber=2

[Rules]
StartingCash=999999
Ante=10
MaxRaises=3
MinBet=10
MediumBet=25
MaxBet=50
CheckRaise=True
DrawFourOnAce=True
LastRaiserStarts=False
RedealAfterAllCheck=True

[Player 1]
Name=Stranger

[Player 2]
Name=(random)
Active=True
Sex=0
Skill=0
Style=0

[Player 3]
Name=(random)
Active=True
Sex=0
Skill=0
Style=0

[Player 4]
Name=(random)
Active=True
Sex=0
Skill=0
Style=0

[Player 5]
Name=(random)
Active=True
Sex=0
Skill=0
Style=0

[Player 6]
Name=(random)
Active=True
Sex=0
Skill=0
Style=0

[Main Window]
Left=328
Top=134
[Options Dialog]
Left=386
Top=283
[Stats Window]
Left=424
Top=586
Visible=True
State=0
[Odds Window]
Left=77
Top=229
Visible=True
State=0


// Some random INI file on my computer

Share this post


Link to post
Share on other sites
Quote:
Original post by Iccarus
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)


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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by xEricx
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.


Hmm, than i'd rather learn xml. xml appears everywhere i go, it seems the only way is to learn it :)

I understand that i will load map file and trigger positions but i couldn't get the idea totally that (in short)"load trigger from file". Am i going to write trigger functions in my code and than just call them?

Share this post


Link to post
Share on other sites
Why not handle this the message-way?

You've got your map array, why not store a message into it?
For example:

enum ActionMessage {
amNothing = 0,
amGoToToilet = 1,
amJumpToAnotherTile = 2,
amBlabla = 4,
amBleBle = 8,
...
};

and in your map-array:
mapstruct map[128][128];

struct mapstruct {
...
ActionMessage msg;
ActionParameter parm; <= or something else that fits your needs
...
}

And then, when someone comes into a new piece of the map:

if (map[y][x].msg & amGoToToilet) { ... }
if (map[y][x].msg & amJumpToAnotherTime) { ... }
...

This is of course only good when you have a small amount of fixed actions.

If you are wondering how to store the map, xml might be a good option, but I prefer using my own C-style information-file format where you use { } to group things. Of course you'll need to parse that file yourself where xml does it for you.

Share this post


Link to post
Share on other sites
Quote:
Original post by scippie
Why not handle this the message-way?

You've got your map array, why not store a message into it?


Scippie, it's not a bad idea i think. But what if i want trigger to start more then an action? You can say that to make function that starts multiple actions but wouldn't this cause a messy code?

Share this post


Link to post
Share on other sites
If you want to have multiple actions set off by the same trigger, then you can do something like this:


enum ActionMessage {
amNothing = 0,
amGoToToilet = 1,
amJumpToAnotherTile = 2,
amBlabla = 4,
amBleBle = 8
};


int action = amGoToToilet | amBlabla;




Each tile would have it's own action variable. Then in your main message handler function, you can test to see which actions need to be done.



if(action & amBlabla)
{
std::cout << "BlaBla\n";
}

if(action & amGoToToilet)
{
std::cout << "Going to toilet\n";
}

if(action & amBleBle)
{
std::cout << "BleBle\n";
}


Share this post


Link to post
Share on other sites
Instead of thinking of it through tiles and events, think in terms of objects. You want a coin on the screen and when the player hits the coin, the coin disappears and their money goes up. This isn't just a tile and an event, this is a game entity.

Now you have some decisions to make. Do you want to have the player trigger the coin, or the coin trigger the player? If you want your player to trigger the coin, you'll have the player call a Collision(Player player) function for each entity it comes in contact with. Once you have that, you can easily implement your trigger system as you'll just override the Collision method for each of your game entities.

In Tile Studio, each tile has a hexidecimal code which is used for anything you want. This means that if you want a coin on the screen, you can set the MapCode to 1. When you load the map, you then just make a coin object on every tile that has a MapCode of 1.

Quote:
Original post by AcePilot
If you want to have multiple actions set off by the same trigger...
If you implement a state manager system, then you won't have to worry about having a huge set of if statements or switch statements as every state of each entity just derives from the base State abstract class.

Share this post


Link to post
Share on other sites
Yeah, a state manager system would be good. Ok, i choose state manager system because it produces less messy code and more suitable to my needs. Could you post a code snippet? It hass not to be very detailed.

Share this post


Link to post
Share on other sites
Quote:
Original post by by
Could you post a code snippet?
There was a great discussion on the matter not too long ago: Managing Game State to which someone posted a link to the article State-Driven Game Agent Design. Very interesting read. Later on in the tutorial series, he implements a Miner entity that can either dig or wait. You don't have to implement his system, it's just the concepts of state-driven design that will possibly make your life easier.

Share this post


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

  • Advertisement