UE3 - General UDK Compiler Errors (noob here, please help!)

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

FlamingRain

New Member
May 31, 2010
4
0
0
I'm making a script for realistic (reloadable) weapons in UDK.
I'm basing it off of this tutorial, but am using my own variables and such, and I'm using states so that you won't be able to do things like shoot, or switch weapons while reloading.

http://forums.epicgames.com/showthread.php?t=704840&page=2

Here's the tutorial

Here's my code.

Code:
/** Created by Sascha Burckhardt for ECHO **/

class EOWeapon extends UTWeapon;
// Size of the magazine
var int MagazineSize;
// How much ammo we have spare
var int SpareAmmo;
// Name of the reload animation
var name ReloadAnimation;
// The time it takes to reload.
var float ReloadTime;
// Are we reloading?
var bool bIsReloading;



exec function DisplaySpareAmmo()
{
     WorldInfo.Game.BroadcastHandler.Broadcast(self, "Weapon Has:"$SpareAmmo);

}


// If we are out of ammo, we must reload. //
simulated function AmmoCheck()
{
          Start:
          if(AmmoCount == 0)
             {
              GoToState( 'ReloadState' );
             }

          else
              {
              GoTo Start;
              }
              
}


// If we press the reload button, we must reload //

simulated exec function ReloadWeapon()
{
     GoToState( 'ReloadStart' );
}

// Begin Reloading

simulated state ReloadStart
{
    Begin:

          loginternal( "THIS IS THE RELOADING STATE LOLOLOLOLO" );
          WorldInfo.Game.BroadcastHandler.Broadcast(self, "Buildin' A Sentry");
    

    
        simulated function BeginReload()
        {
          if (bIsReloading || AmmoCount <= 0)
          {
            return;
          }

    
          bIsReloading = True;
    
          setTimer(ReloadTime, false, 'Reloading');
    
         
        }
        
        /** DERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRP **/
    
        simulated function ReloadAmmo()
        {
                       bIsReloading = True;
                     if (SpareAmmo >= Default.AmmoCount)
                     {
                      // Set the AmmoCount to default
                         AmmoCount = Default.AmmoCount;
                      // Subtract SpareAmmo from AmmoCount
                         SpareAmmo -= AmmoCount;
                      }
    
                      else
                      {
                       // Set AmmoCount to how much we have left
                          AmmoCount = SpareAmmo;
                       // Set SpareAmmo to zero
                          SpareAmmo = 0;
                       // Set to active state.
                      }
                          
                          GoToState ( 'Active' );
                          WorldInfo.Game.BroadcastHandler.Broadcast(self, "ALARM: FRIENDLY TARGET. CEASE FIRE. ");
                          bIsReloading = False;
        }



}

(ignore the weird broadcasts and comments i add onto the code)

Please someone help to explain why I can't seem to call a function in the middle of a state.
I get the ERROR, 'Function' is not allowed here, error, and can't find a way to stop it from happening.

I'm goign to keep fiddling around with it until I can, but please someone help!!
 

brold9999

New Member
Apr 5, 2009
142
0
0
You can call a function in the middle of a state, but you can't declare one there. The function declarations must be before "Begin:".
 

Wormbo

Administrator
Staff member
Jun 4, 2001
5,913
36
48
Germany
www.koehler-homepage.de
You also have an infinite loop in your AmmoCheck function.
You basically wrote the following there:
Code:
while (AmmoCount == 0) {
  // do nothing and just loop
}
GotoState('ReloadState');
Nothing wrong with such a function in other languages that support multiple threads, but UnrealScript is strictly single-threaded. "latent" functions, such as Sleep, actually just interrupt state code execution and set up a check performed every time code execution is resumed to see if the latent condition is met, e.g. for Sleep that a certain amount of time has passed.
 

FlamingRain

New Member
May 31, 2010
4
0
0
Code:
/** Created by Sascha Burckhardt for ECHO **/

class EOWeapon extends UTWeapon;
// Size of the magazine
var int MagazineSize;

// Name of the reload animation
var name ReloadAnimation;
// The time it takes to reload.
var float ReloadTime;
// Are we reloading?
var bool bIsReloading;
// Amount of Bullets in the magazine
var int BulletCount;



exec function DisplaySpareAmmo()
{
     WorldInfo.Game.BroadcastHandler.Broadcast(self, "Weapon Has:"$BulletCount);

}

// If we press the reload button, we must reload //

exec function ReloadWeapon()
{

          Begin:

          loginternal( "THIS IS THE RELOADING STATE LOLOLOLOLO" );
          WorldInfo.Game.BroadcastHandler.Broadcast(self, "Buildin' A Sentry");

          if (bIsReloading || BulletCount <= 0)
          {
            return;
          }
          else
          {
          PlayWeaponAnimation( ReloadAnimation, ReloadTime );
          PlayArmAnimation ( ReloadAnimation, ReloadTime );
          bIsReloading = True;
          setTimer(ReloadTime, false, 'ReloadAmmo');
          }
}

// Begin Reloading

simulated state Reloading
{
  simulated function bool AllowSwitchTo(Weapon NewWeapon)
{
        if (bIsReloading)
        {
	   GoToState( 'Reloading' );
           return false;
	}
	
        else
        {
          return true;
        }

}
}






        /** DERRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRP **/

simulated exec function ReloadAmmo()
        {
         bIsReloading = True;
                     if (AmmoCount >= Default.BulletCount)
                     {
                      AmmoCount -= ( default.BulletCount - BulletCount );
                      BulletCount = default.BulletCount;
                      GoToState ( 'Active' );
                      WorldInfo.Game.BroadcastHandler.Broadcast(self, "ALARM: FRIENDLY TARGET. CEASE FIRE. ");
                      bIsReloading = False;
                      }
    
                      else
                      {
                       // Set AmmoCount to how much we have left
                          BulletCount = AmmoCount;
                       // Set SpareAmmo to zero
                          AmmoCount = 0;
                       // Set to active state.
                          GoToState ( 'Active' );
                          WorldInfo.Game.BroadcastHandler.Broadcast(self, "ALARM: FRIENDLY TARGET. CEASE FIRE. ");
                          bIsReloading = False;
                      }
                          

        }


simulated function InstantFire()
{
    // If we have bullets then fire
    if(BulletCount > 0)
    {
       // deducts 1 from our bulletcount 
       BulletCount -= 1;

       // Shots the bullet
       super.InstantFire();
    }
    // if we dont have bullets then reload
    else
    {
         // Stop firing the weapon
         StopFire(0);

         // Run the reload function
         ReloadWeapon();
    }
}
Here is my current code.
My code works to an extent. If i run out of ammo, and it automatically reloads, it will not reload the ammo and will just sit there after it broadcasted Buildin' A Sentry, and not do anything unless i go into the console and type ReloadAmmo().
I'm unsure what's going on here.
 
Last edited:

Wormbo

Administrator
Staff member
Jun 4, 2001
5,913
36
48
Germany
www.koehler-homepage.de
You still seem to mix up the idea behind state and function code. States define behavior that may take some time to do, while functions describe behavior that happens immediately without any delay and finishes immediately.

About your actual problem: You have a conditional return in your ReloadWeapon function. How about logging the condition's variables to see what's actually going on?