0

My game includes a series of 10 missions, each with 3 objectives to complete during gameplay (very similar to achievements). Once the 3 objectives are completed in any order, the mission is completed and the player is given the next mission objectives.

I'm struggling with how to structure this. I've created a 10-length array of mission classes, with each class containing a 3-length array of classes for each of the objective details (name, goal value, completionState, etc.) This is a good start, and allows me to easily see if an achieved objective event is part of the current mission or if it should be ignored as it's currently not active.

var missionControl : MissionClass[];
class MissionClass
{
    var mission = new ObjectiveInfo[3];
}
class ObjectiveInfo
{
    var name : String;
    var goalValue : int;
    var completionState : boolean;
    var total : int;
}

However, the way I'm intending to send achieved objective events is to send a string reference, rather than pointing to a location in the array (in case I shuffle the order later), but how can I use a string to locate an item in an array and then check if that item's parent Mission Class is the currently active mission?

function ObjectiveCheck ( objectiveName : String )
{
    // Find the objective by name
    // Find out if the objective we've been passed is part of the currently active mission
    // If it is, check if we've met the goalValue, if so set objective to completed
}

// Edit: New Code using a string comparison. missionStatus is an array where item 0 equals the current mission number (1/10), and items 1, 2, 3 hold the current status of each mission objective (0 = incomplete, 1 = previously complete, 2 = newly complete).

function ObjectiveCheck ( objectiveName : String, num : int )
{
    // Find out if the objective we've been passed is part of the currently active mission and see if it's met the goal value
    var currentMission = missionControl[ missionStatus[0] ];
    for ( var i=0; i < 3; i++ )
        if ( missionStatus[i+1] == 0 && currentMission[i].name == objectiveName && num >= currentMission[i].goalValue )
        {
            missionStatus[i+1] = 2;
            // Play mission complete sound
        }
}
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Essential
  • 391
  • 7
  • 14
  • I do not know unity3d much, so you have a `MissionClass` class that holds a collection of objectives and you want to know if the current mission holds some specific objective when they are achieved (event raised with objective name). That sounds quite simple, dont you have a `currentMission` variable or something? You then only have to loop over `currentMission.objectives` and check if you get a match. Also, may I ask how your are checking if an objective is complete without having a reference to it? The completion logic is not encapsulated within the objective? – plalx Oct 20 '13 at 01:38
  • The relevance to Unity3D isn't so important. Yes, I considered putting a `currentMission` boolean for every objective but it felt a bit of an ugly solution to me, as does comparing the names of each item by string. I was wondering if there might be some way to do what I want with a javascript object associative array (where I believe items can be fetched with a named String) but I don't know too much about how to do that or if it's suitable. – Essential Oct 20 '13 at 02:40
  • `"currentMission boolean for every objective"`? That wasn't my idea, `currentMission` would be an instance of `MissionClass` which would hold a collection of objectives or an object map of objectives which would allow for quick objective lookup by name. – plalx Oct 20 '13 at 02:43
  • Oh, my mistake. Hmm… I'm interested in learning more about your idea of an object map. In-between your previous reply, I wrote the following code, but maybe your method would be an improvement? – Essential Oct 20 '13 at 03:20
  • // Edit: Sorry, I don't know how to format code in a comment. I added it to the bottom of my original post above. – Essential Oct 20 '13 at 03:28

0 Answers0