A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] Code design help for tech tree

  1. #1
    Member
    Join Date
    Feb 2008
    Posts
    79

    resolved [RESOLVED] Code design help for tech tree

    Hello

    I am writing a game with a Civ style tech tree. When the player discovers a technology on the tree, it opens up other technologies for research.

    The technologies are held in a multidimensional array. Here's a simplified version of it
    PHP Code:
    // The [0] element is the name of the technology
    // The [1] element is how much research the player must accumulate to discover it
    // The [2] element is whether it has been discovered yet (true or false)

    technologies[[NameOfTech1", 1000, true], ["NameOfTech2" 1500, false]] //and so on 
    As the player researches something, a variable called remainingSci1 decreases, and the idea is, when it gets to zero, the current technology has been discovered. The variable decrements properly

    Here is the function I am trying to use, that is going partially wrong.

    PHP Code:
    function checkDiscoveries()//checks to see if researchProject has been discovered
    {
    for (var 
    i-0i<technologies.lengthi++)// walk through the technologies array
    {
    if (
    researchProject ==technologies[i][0] && remainingSci1<1//if the current project matches the technology name and has accumulated enough science to be discovered
    {
    technologies [i][2] = true//set the technology to true - ie discovered
    }//end if
    }//end for
    }//end function 
    This works fine immediately after the technology has been discovered. The tech does indeed go to true and opens up other technologies, but as soon as I click on one of the other technologies, the variable remainingSci1 is no longer < 1 and so the technology reverts back to undiscovered again.

    I can see why this happens, but I am stuck on the code design that makes technologies[i][2]=true a permanant change. I am desperate not to have to program a paragraph for each technology as there are over 160 of them and it would be a very inelegant solution.

    Can anyone save my weekend and suggest a way that I can do this using my array.

    Thanks all.

  2. #2
    Member
    Join Date
    Apr 2006
    Posts
    32
    Hi I don't know if that is only problem but in the first code instead of " you have [ and in the second code you have i-0 and there should be i=0

  3. #3
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    The function you showed does not revert technologies[i][2] back to false.

  4. #4
    Member
    Join Date
    Feb 2008
    Posts
    79
    Thanks for getting back to me.

    What is reverting my technology back to false is me picking a subsequent technology to research. When that new technology button is clicked, the variable remainingSci1 is now > 0 again, so as soon as that happens, the discovered technology is seemingly reverting back to false. I'm trying to get my head around how to design my code so that once a technology is discovered, it no longer depends on the value of remainingSci1 for it's true/false status.

    Well spotted Zazuna. I rewrote this code rather than pasting it. The original reads i=0. I'll edit my first post to avoid confusion.

  5. #5
    Senior Member PRadvan's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    261
    I think you need to check remainingSci1 against technologies [i][1]
    and while you're substracting from remainingSci1, also substract from technologies [i][1]

  6. #6
    Member
    Join Date
    Feb 2008
    Posts
    79

    resolved Resolved

    Thanks everyone. Tonypa was quite right (as always), the problem lay elsewhere in my code.

    This means a crucial part of my tech tree is now working and the mess I made is beginning to look like a game! This flash stuff is addictive.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center