Announcement

Collapse
No announcement yet.

Locating a variable in another class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Locating a variable in another class

    So yeah, I havent been able to fix this and it has destroyed my day.
    It seems that the playercontroller class cannot find the variable named clips in the weaponbase class. but its clearly there.
    Im still working on ways to fix this without re writing the code.

    Compiler:
    Code:
    Analyzing...
    C:\UDK\UDK-2012-07\Development\Src\PlayerController.uc(291) : Error, Bad or missing expression for token: clips, in 'If'
    Compile aborted due to errors.
    PlayerController.uc
    Code:
    ...
    exec simulated function Reload()
    {
    	
    	if( clips > 0)
    	{
    	    ....;
    	}
    }
    WeaponBase.uc
    Code:
    class Z_DWGS_WeaponBase extends UTWeapon
    	dependsOn(PlayerController)	
    	Abstract;
    
    
    var int clips; //Number of clips in our weapon

    I have tried adding
    Code:
     dependson(weaponbase);
    in the player controller class but this creates a circular dependency and throws an error.

    Any help at all would be greatly appreciated, this was my last resort.
    -Chris

    #2
    Ill try removing the
    Code:
    dependson(Playercontroller);
    from the weaponbase class and try moving the code that depends on the playercontroller to an intermediate class extending utweapon. This class will be what the WeaponBase extends from..for testing purposes.

    If any one has any ideas, im open to hear them
    -Chris

    Comment


      #3
      PlayerController.uc
      Code:
      ...
      exec simulated function Reload()
      {
      	local Z_DWGS_WeaponBase weap;
      
      	weap = Z_DWGS_WeaponBase(Pawn.Weapon);
      
      	if (weap != None && weap.clips > 0)
      	{
      	    ....;
      	}
      }

      Comment


        #4
        What evr said. To summarise how you should be thinking about this:

        Clips is a class variable in YourWeapon (no, I'm not typing out the full class name on a tablet).

        The weapon is handled via the pawn. Pawn has a reference to it named Weapon, of type Weapon.

        Controllers have a reference to their pawn instance (assuming they've possessed one), named Pawn.

        Thus the weapon can be accessed from the controller using Pawn.Weapon

        Weapon, however, is typed to a super class of YourWeapon. This allows it to handle any type of weapon, but doesn't allow access to any specialisation defined in derived weapon types such as YourWeapon.

        Weapon can be typecast to YourWeapon, creating a new reference that can access Clips.

        Before using this reference we check that its valid. If its None it could have been a different weapon type, or there may be no weapon at all.

        Code:
        if ( Pawn != None && YourWeapon(Pawn.Weapon) != None )
        {
            YourWeapon(Pawn.Weapon).Clips = 0; // remove all clips
        }

        Comment


          #5
          The dependson modifier is not used to 'transfer' variables/references from one class to another; it is only used for the order of compiling for instance if class B has a struct and class A is trying to access this struct on a function it will not work because class A will be compiled first hence the struct on class B won't be known
          Code:
          class B extends Object;
          
          struct aStruct
          {
          	var int i;
          };
          Code:
          class A extends Object;
          
          function GetStruct( aStruct pStruct )
          {
          	pStruct.i = 0;
          }
          If the dependson modifier is used i.e. class A adds dependson( B ); then B will be compiled first hence the struct will be known. As for your issue that was related to Clips not being known in PlayerController; as this variable only exists in your weapon class and can be accessed by using a reference.

          Comment


            #6
            ok, I think I kinda understand how this type casting thing works.

            Code:
            exec simulated function Reload()
            {
            	local Z_DWGS_WeaponBase Weap;
            
            	Weap = Z_DWGS_WeaponBase(Pawn.Weapon);
            
            	if( Weap != None && Weap.clips > 0)
            	{
            	Weap.bIsReloading = false; //Set bIsReloading to false
            	Weap.clips = Weap.clips - 1; //Reduce 1 clip
                Weap.IncreaseAmmo(30); //Add the amount of ammo you want in your weapon
                ClearTimer();
            	}
            }
            Code:
            Build succeeded.
            
            Time Elapsed 00:00:08.67
            ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
            Thank you guys for helping and to you Spoof, for explaining where I was lacking knowledge.
            Now gunna go see if it still works..
            -Chris

            Comment


              #7
              Everything seems to be fine except now i need to modify the other reload related functions the same.

              Comment

              Working...
              X