View Full Version : Code design help for a tech tree
John Barbarossa
August 22nd, 2008, 06:47 AM
Hello. I've been recommended this forum by some people at my university as somewhere that can help me when my n00b flash skills aren't up to the job. :}
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
// 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.
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.
bluemagica
August 22nd, 2008, 08:52 AM
Hello. I've been recommended this forum by some people at my university as somewhere that can help me when my n00b flash skills aren't up to the job. :}
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
// 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.
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 functionThis 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.
err, two things:-
1) for( var i-0,.... why are you doing i-0?
2) I see you setting it to true, but to revert it will have to be set back to false, where are you doing that? I don't see that....
John Barbarossa
August 22nd, 2008, 09:48 AM
Thanks for getting back to me.
I rewrote my code, rather than pasting it. That i-0 reads i=0 in the actual code, but well spotted :) I'll edit the original post to avoid confusion.
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.
bluemagica
August 22nd, 2008, 10:09 AM
hmm, but still, anything cant just get set to false, unless you tell it to....also
researchProject ==technologies[i][0]
so as long as researchProject is some other research name, i don't see how it can revert back. If you have a flash8 version of your work, then you should show it here! the error is some other place than the code you have already shown!
John Barbarossa
August 22nd, 2008, 10:33 AM
Hi
Unfortunately the code is now 8Mb or so, so I can't post it on this forum. I also thought it should remain true, because even though remainingSci1 is no longer <0, the setting of the true/false element to true should be absolute. I am struggling to get my head around this.
One thought was to create a new multidimensional array holding just discovered technologies, along with their x and y values, but I have only ever pushed into a simple array before and I'm not sure how to push into a multi-dimensional array.
For this I would like to push the [0] element (the name) along with the [3] element (it's x position) and the [4] element (it's y position), from the technologies array, into the discovered array, making my discovered array contain three elements per item ([0] Name, [1] x pos and [2] y pos).
Is this possible using push? Do you know what the syntax for this might be?
bluemagica
August 22nd, 2008, 11:04 AM
First i dont know if you are using as2 or as3....and second i am not so good with as3, but in as2, you can make array properties, these are like multidimensional push but are easier to work with...you can read it up on the manual!
John Barbarossa
August 22nd, 2008, 11:07 AM
I am using as2.0. OOP scares the pants off me so I have steered clear of as3.0
I'll look into array properties and see if that can workaround the problem. Thanks for all your help on this, Blue.
ArmoredSandwich
August 22nd, 2008, 01:45 PM
I would just create a simple technology class. The only problem is that you're not using OOP. I wonder, why not? How can you start a huge *** project like that without OOP and how can you have 8 mb of code :P
Anyways, this piece should work. How can you tell that it's being set to false?
John Barbarossa
August 23rd, 2008, 06:02 AM
It does work, to some extent. Technologies trace out as true until I go to the tech tree and select another technology, then they trace out as false.
The code to select the next technology is typically something like this (Remember I gave you a simplified version of my array - this uses the actual array)
technology2Button.onRelease = function()
{
researchProject1 = technologies[1][0];//sets the name of the new tech
researchProjectTotal = technologies[1][2];//sets the total research required to complete this tech
remainingSci1 = researchProject1Total;//Sets the decreasing variable remainingSci1 to the researchProject1Total
accumulatedResearchProject1 = 0;
removeTechTree();//removes the tech tree interface
gotoAndStop("research");
attachResearch();//attaches the research interface
attachNavigation();//attaches the nav buttons
}
I think the problem may be with this line
remainingSci1 = researchProject1Total;//Sets the decreasing variable remainingSci1 to the researchProject1Total
as that is the only line that refers to the remainingSci1 variable and so it may be this that is resetting my technology to false, but at this stage I am stalled as to why.
You're quite right about OOP, Armoured, except this is my first stab at a programming language and I am self taught. Progress is slow, and procedural programming is as far as I have gotten in my knowledge. I am sure when the community sees some of my code, their newbie alarms will go off, but we all have to start somewhere, eh? :)
ArmoredSandwich
August 23rd, 2008, 11:53 AM
Are you sure you're not tracing the technology you just clicked and is indeed false?
Fact is that a variable can't just change value, it's impossible.
The code you gave looks flawed, no dynamical variables (instead: technologies[0][1]).
If you can host it somewhere then do so because what you're asking is for us to fix a bug with only a small amount of code ;) .
DrRobot
August 24th, 2008, 11:32 AM
are you restating the variable in one of your functions? sounds to me that's what your doing.
oh and you could do this (it's not the best way, and since you're using as2 you could make the variable and always reference to it from _global., but in as3 that doesn't exist):
package {
public class global {
//vars, access like global.techArray[0][2];</font></i></p>
public static var techArray:Array = [];
public function global():void {
}
}
}
John Barbarossa
August 28th, 2008, 08:43 AM
:thumb2:Thanks everyone. I've been tearing my hair out on this, but you were all of course quite right. The problem did indeed lie elsewhere in my code.
Thanks for pointing me in the right direction. Armoured, you'll be glad to know I went round my friends house yesterday to get my first lesson in OOP. Debugging an entirely procedural program is a nightmare. It seems the biggest advantage of OOP is that I (and you guys) will be able to see at a glance what the program is doing - or not doing.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.