|
-
[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-0; i<technologies.length; i++)// 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.
-
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
-
Senior Member
The function you showed does not revert technologies[i][2] back to false.
-
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.
-
Senior Member
I think you need to check remainingSci1 against technologies [i][1]
and while you're substracting from remainingSci1, also substract from technologies [i][1]
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|