This site requires JavaScript, please enable it in your browser!
Greenfoot back
ElwoodHogan
ElwoodHogan wrote ...

2020/11/1

How to access Variables from touching subclass?

ElwoodHogan ElwoodHogan

2020/11/1

#
I am making a farming game, and a mechanic i have is placing seeds on soils, but theres different types of soils and different types of seeds. I am trying to get the newly placed seed to access the soils soilQuality which is a double tat will directly affect the growth rate of the seed. The problem is there can be multiple plots of multiple soils, and the seed needs a different growth rate depending on which soils its on. All the soils are a subclass of Soil.
public class cornSeedTile extends Seeds
{
    
    public double growthRate = 1;
    
    public void act() 
    {
    }
    
    public void addedToWorld() {
            Soils soil = (Soils)getOneIntersectingObject(Soils.class);
            growthRate *= soil.getGrowthRate();
        }
}
public class basicSoilTile extends Soils
{  
    public double soilQuality = 2;
}
danpost danpost

2020/11/1

#
Line 3 in basicSoilTile class should probably be in the Soils class (not where it is). Either way, however, in Seed class::
basicSoilTile bst = (basicSoilTile)getOneIntersectingObject(basicSoilTile.class);
if (bst != null)
{
    double quality = bst.soilQuality;
    // ...
ElwoodHogan ElwoodHogan

2020/11/1

#
That wont work. The seed should be able to get the soilQuality from any soil it is planted on. It could be richSoilTile, crappySoilTile, etc. The soilQuality variable changes from soil to soil. Thats why I didnt just put it directly in the Soils class because it it NOT just one number.
danpost danpost

2020/11/1

#
ElwoodHogan wrote...
That wont work. The seed should be able to get the soilQuality from any soil it is planted on. It could be richSoilTile, crappySoilTile, etc. The soilQuality variable changes from soil to soil. Thats why I didnt just put it directly in the Soils class because it it NOT just one number.
Put it ONLY in the Soils class. It will be inherited into its subclasses. Change ALL "basicSoilTile" in line 1 above to "Soils". It should then work. (you can also change "bst" to "soil" throughout). You can add constructors to the subclasses to assign the appropriate values to the field. E.G.
public basicSoilTile()
{
    soilQuality = 2;
}
ElwoodHogan ElwoodHogan

2020/11/1

#
danpost wrote...
ElwoodHogan wrote...
That wont work. The seed should be able to get the soilQuality from any soil it is planted on. It could be richSoilTile, crappySoilTile, etc. The soilQuality variable changes from soil to soil. Thats why I didnt just put it directly in the Soils class because it it NOT just one number.
Put it ONLY in the Soils class. It will be inherited into its subclasses. Change ALL "basicSoilTile" in line 1 above to "Soils". It should then work. (you can also change "bst" to "soil" throughout). You can add constructors to the subclasses to assign the appropriate values to the field. E.G.
public basicSoilTile()
{
    soilQuality = 2;
}
I have implemented the code:
public class Soils extends Tiles
{
    public double soilQuality = 3;
}
public class basicSoilTile extends Soils
{
   public basicSoilTile(double costIn) {
            cost = costIn;
            double soilQuality = 2;
   }
}
public class cornSeedTile extends Seeds
{
    public void act() 
    {
        getSoil();
    }

    public void getSoil() {
            Soils soil = (Soils)getOneIntersectingObject(Soils.class);
            if (soil != null) {
                    double quality = soil.soilQuality;
                    getWorld().showText("" + quality, getX(), getY()+39);
                }
            
        }
However, the showText command still prints a 3, which is the soilQuality of the Soils class.
danpost danpost

2020/11/1

#
Remove the word "double" from line 5.
ElwoodHogan ElwoodHogan

2020/11/1

#
danpost wrote...
Remove the word "double" from line 5.
That fixed it! It now works perfectly. Thank you for your help danpost. Appreciate it.
danpost danpost

2020/11/2

#
ElwoodHogan wrote...
That fixed it! It now works perfectly. Thank you for your help danpost. Appreciate it.
The reason why the "double" word needed to be removed is that with it, you are defining a NEW local variable with the same name as the pre-defined field; without it, it will refer to that pre-defined field. Line 5 can also be written as:
this.soulQuality = 2;
You need to login to post a reply.