How select between diferent variables from a referenced script with one variable
Hi there to everybody!
Well the question is, i have a public class called AmmoScript with obviously is in a separated script. This class have diferent Ammo variables such as pistol, machinegun, rocket, shotgun shells and so on.
And in the other hand i have a WeaponScript with is the main script for the weapons. And i need some versatile or simple way to get one var or another from AmmoScript.
You probably ask yourself why i need the ammo variables in a diferent script and so on. Well thats becouse i'm going to do diferent weapons that share the same Ammo variable. For example a minigun using the same amunition as the assaultrifle or an automatic shootgun have the same as the classic shotgun.
I was thinking in use a string but seems to be to painfull convert a string into a variable name. The other way i think could be using an enum and depending on wich var is selected from there pick on or another var from ammoscript. The last obviously way si to put the ammo var inside weaponscript and look to the floor and fall defeated by the chimera xD.
Thanks in advance for any sugestion!
Answer by jgodfrey · Jun 26, 2016 at 08:03 PM
We probably need some more details to give you a decent answer. For instance, what are the ammo variables in the AmmoScript? Are they each just a simple scalar variable (such as an INT) or do they represent a more complex object?
Off the top of my head, I might suggest that you store the information in AmmoScript in a Dictionary, where the key could be either 1) a string representing the weapon type ("pistol", 'machinegun", etc), or 2) an ENUM representing the weapon type (weapontype.pistol, weapontype.machinegun, etc) and the value could be the stored information (an INT, a FLOAT, an object, or whatever you need).
With that, you could simply request the info associated with a given key (via a string, an enum, or whatever you made the key).
But, without more info, it's hard to be sure what might be appropriate...
Well with the passing of the days I was testing different solutions. And in the end I stay what you recomend, the dictionary. As always when I find something new I take time to experiment with it and see its possibilities. But eventually we could say that I take your advice.
Thank you!
Answer by UrielKane · Jun 26, 2016 at 08:31 PM
//Active ammo vars
static var PistolAmmo : int = 0;
static var ShotgunAmmo : int = 0;
static var AssaultAmmo : int = 0;
static var RocketAmmo : int = 0;
static var EnergyAmmo : int = 0;
static var SoulAmmo : int = 0;
// Other Variables
static var MaxAmmoLvl : int = 1;
static var Currency : int = 0;
static var HealKit : int = 2;
//Starting ammo
public var SPistolAmmo : int = 30;
public var SShotgunAmmo : int = 0;
public var SAssaultAmmo : int = 0;
public var SRocketAmmo : int = 0;
public var SEnergyAmmo : int = 0;
public var SSoulAmmo : int = 0;
//Max Amount of ammo vars
public var MaxPistolAmmo : int = 200;
public var MaxShotgunAmmo : int = 200;
public var MaxAssaultAmmo : int = 200;
public var MaxRocketAmmo : int = 200;
public var MaxEnergyAmmo : int = 200;
public var MaxSoulAmmo : int = 200;
public var StartingAmmo : boolean = true;
function Awake () {
if(StartingAmmo == true){
PistolAmmo = SPistolAmmo;
ShotgunAmmo = SShotgunAmmo;
AssaultAmmo = SAssaultAmmo;
RocketAmmo = SRocketAmmo;
EnergyAmmo = SEnergyAmmo;
SoulAmmo = SSoulAmmo;
}
}
Thanks for your answer
This is the entire AmmoScript And now i look again at it. I realize that at least there are static vars, but i dont know if that would be useful for simplify the thing.
As we see its just a bunch of int variables. Wich im tryng to do is a large number of weapons. And the main reason why i need to get less usable ammo than one per weapon. Is becouse it will be a pain in the ass to do for example 30 diferent pickups for ammo.
if(Input.GetButtonDown("Reload") && FPStatus.IsShooting == false && FPStatus.IsReloading == false){
if(AmmoScript.PistolAmmo > 0 && WScript.AmunitionLeft < WScript.AmunitionMax){
ReloadWeapon();
}
}
This fragment is from my weaponscript where you can see why i need this. What i was doing before is making one script per weapon and work fine. But as soon as the number of weapons increase and begins bigger everything become a super mess. Now i want to do a single script for all weapons and i need this problem of the ammo solved in one way or another.
function ReloadWeapon () {
if(WScript.AnimatedGun) {
WScript.AnimatedGun.Stop();
WScript.AnimatedGun.Rewind("Reload");
WScript.AnimatedGun.Play("Reload");
}
FPStatus.IsReloading = true;
if(ReloadSFx){
audioPlayer.PlayOneShot(ReloadSFx);
}
yield WaitForSeconds(WaitForReload);
var AmmoToReload : int;
var TotalAmmo : int;
if(AmmoScript.PistolAmmo >= WScript.AmunitionMax){
if(WScript.AmunitionLeft == 0){
WScript.AmunitionLeft = WScript.AmunitionMax;
AmmoScript.PistolAmmo -= WScript.AmunitionMax;
}
else{
AmmoToReload = (WScript.AmunitionMax - WScript.AmunitionLeft);
WScript.AmunitionLeft = WScript.AmunitionMax;
AmmoScript.PistolAmmo -= AmmoToReload;
}
}
else{
if(WScript.AmunitionLeft == 0){
WScript.AmunitionLeft = AmmoScript.PistolAmmo;
AmmoScript.PistolAmmo = 0;
}
else{
TotalAmmo = (WScript.AmunitionLeft + AmmoScript.PistolAmmo);
if(TotalAmmo >= WScript.AmunitionMax){
WScript.AmunitionLeft = WScript.AmunitionMax;
AmmoScript.PistolAmmo = (TotalAmmo - WScript.AmunitionMax);
}
else{
WScript.AmunitionLeft = TotalAmmo;
AmmoScript.PistolAmmo = 0;
}
}
}
FPStatus.IsReloading = false;
DrawAmmo ();
}
In the end this is the reload behaviour of my weapon script. Wich is only a magazine reload for now but thats another matter. Well i think its obviously why i need a way to chose one or another ammo variable to work in the weaponscript.
Your answer
![](https://koobas.hobune.stream/wayback/20220612083725im_/https://answers.unity.com/themes/thub/images/avi.jpg)