- Home /
Call variable from other script but not allow it to be updated by that script
I have a weapon manager like so
#pragma strict
static var WeaponSelected : int = 1;
//Shotgun
var RateOfFireShotgun : float =1;
var RoundsShotgun : int = 5;
var ReloadTimeShotgun: float = 5;
//Magnum
var RateOfFireMagnum : float = 0.8;
var RoundsMagnum : int= 8;
var ReloadTimeMagnum: float = 4;
//Magnum
var RateOfFireMachineGun : float = 0.1;
var RoundsMachineGun : int = 30;
var ReloadTimeMachineGun: float = 3;
static var RateOfFire: float;
static var Rounds: int;
static var ReloadTime: float;
function Update ()
{
if(WeaponSelected == 1) //Shotgun
{
RateOfFire = RateOfFireShotgun;
Rounds = RoundsShotgun;
ReloadTime = ReloadTimeShotgun;
}
if(WeaponSelected == 2)//Magnum
{
RateOfFire = RateOfFireMagnum;
Rounds = RoundsMagnum;
ReloadTime = ReloadTimeMagnum;
}
if(WeaponSelected == 3)//MachineGun
{
RateOfFire = RateOfFireMachineGun;
Rounds = RoundsMachineGun;
ReloadTime = ReloadTimeMachineGun;
}
} I am calling the Static vars from my Gun script which will need the values of Rounds, RateOfFire, and ReloadTime.
Problem I am having is that the Rounds keeps updating to it orginal value like it shoulod (but I don't want it to).
I really like this script as it is easy to change the values and seems to allow me to swtich weapons very easily.
How can I get the current value of the Rounds from this script and not have it be effected from this script anymore? (like make a copy of its current value).
Answer by aldonaletto · Jan 09, 2012 at 01:04 AM
Since you're defining the variables every Update, any change will be overwritten. The solution is to reload the variables only when the weapon is switched, what you can detect using an auxiliary variable lastWeapon. You could also use a switch statement instead of several *if*s - works the same, but is a little faster and looks better:
#pragma strict
static var WeaponSelected : int = 1;
//Shotgun var RateOfFireShotgun : float =1; var RoundsShotgun : int = 5; var ReloadTimeShotgun: float = 5;
//Magnum var RateOfFireMagnum : float = 0.8; var RoundsMagnum : int= 8; var ReloadTimeMagnum: float = 4;
//MachineGun var RateOfFireMachineGun : float = 0.1; var RoundsMachineGun : int = 30; var ReloadTimeMachineGun: float = 3;
static var RateOfFire: float; static var Rounds: int; static var ReloadTime: float; private var lastWeapon: int = -1; // force loading variables in the first Update
function Update () { if (WeaponSelected != lastWeapon){ // if WeaponSelected changed... lastWeapon = WeaponSelected; // update lastWeapon... switch (WeaponSelected){ // and redefine variables case 1: //Shotgun RateOfFire = RateOfFireShotgun; Rounds = RoundsShotgun; ReloadTime = ReloadTimeShotgun; break; case 2: //Magnum RateOfFire = RateOfFireMagnum; Rounds = RoundsMagnum; ReloadTime = ReloadTimeMagnum; break; case 3: //MachineGun RateOfFire = RateOfFireMachineGun; Rounds = RoundsMachineGun; ReloadTime = ReloadTimeMachineGun; break; } } }
Answer by syclamoth · Jan 09, 2012 at 01:31 AM
This isn't really related to the question, but you should really restructure your classes so that it's neater (and has less copy-pasted code!)
Make a class, 'Weapon', at the top.
public class Weapon
{
var rateOfFire : float;
var maxRounds : int;
var reloadTime : float;
// possibly you may also want a 'currentRounds' value
var currentRounds : int;
}
Then, where you had all that 'reloadTimeShotgun, reloadTimeMagnum' etc, just put-
var shotgun : Weapon;
var magnum : Weapon;
var machineGun : Weapon;
private var currentWeapon : Weapon;
When you switch weapons, use
currentWeapon = shotgun;
or
currentWeapon = machineGun;
and then for your 'RateOfFire' or 'Rounds' just use
currentWeapon.rateOfFire;
and
currentWeapon.maxRounds;
As for you original question, instead of decreasing a weapons maximum ammo capacity when it fires, decrease the 'currentRounds', which makes sure that it doesn't forget the original max value.
Thanks again, I''m new to program$$anonymous$$g and don't really get classes. I get this tho! Working on a project and I'm sure my code looks silly, 7 scripts now.