Tuesday, October 17, 2017

architecture - How should I check if a player has completed an achievement?


I'm making an MMO game and I just got to a point where I need to implement achievements... How do I do that? The most straight forward thing to do would be to run this once every 100ms,:


for a in achievements
for p in players
if a.meetsRequirements(p) then p.completeAchievement(a)

But that just raises even more complications. For example, how do I check if the achievement has actually been completed? Do the players have custom properties on themselves just for a specific achievement? I did this kind of thing with quests, because they are mainly "collect 100 wood", so active quests on a player check that. Also, there must be a better time to check it, this would periodically slow my server down, I think.



Answer



What you do depends on the nature of the achievement. Unless your achievements all fit a simple pattern (collect X number of Ys), you're going to have to special-case them to some degree.



Using a message-based communication system, you can provide hooks that makes the special-case coding localized. You can have certain actions fire messages to listeners who register themselves. Then your achievement code/script can just register itself with the appropriate listeners and do whatever testing is necessary to fire off the achievement.


You would have messages for the typical events you might want achievements to listen for. Things like "player has acquired item X" or "entity Y killed entity Z". This way, you can track things like how many Z's that the player has killed.


That's probably the best you can do for a system for achievements. It centralizes the code as much as possible, and puts the onus on the listeners for the actual achievement detection.


Also, it should be noted that, for centralized achievement systems (X-Box Live, Steam achievements), progress towards achievements is typically stored on the server. So for accumulation achievements ("perform task X Y number of times"), the achievement script just detects when X has been performed and bumps the server's count. For other kinds of achievements ("perform task X"), the server achievement is binary: either you have done it, or you haven't.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...