- Home /
Adding from one script to another with a String Name
Bear with me here. I'm sorry if this has been asked before, I just didn't know how to phrase this in search. Does anyone know how I could add an integer from one script into another without actually adding directly from a variable? I know THIS Works: (SCRIPT 1) Playerhealth has a class called "hp" So in Script 2 I could say
PlayerhealthScript.hp += 1;
But let's say I want that to be dynamic. Let's say that I want a space in the inspector of Script 2 to write in which class in Playerhealth will have that 1 added. Something like: (Script 2)
var chooseaclass : String;
PlayerhealthScript.chooseaclass +=1;
Now, I know that isn't the correct syntax (already tried) but is something like this even possible? If so, what is the correct syntax? Thanks!
Answer by BloodMarked · Jan 31, 2018 at 10:29 AM
i think a pointer is what you are looking for. dont know enough about using them though. pointers are pretty advanced stuff.
something like this is usually solved with a function in the playerHealthScript like p.e.:
var health1, health2 = 100;
function changeHealth(var change){
if (condition) {
health1 += change;
} else {
health2 += change;
}
}
,then call PlayerHealthScript.changeHealth(-4);
in the other script
Answer by KittenSnipes · Feb 01, 2018 at 06:00 AM
Well I think you just need public functions and private variables for each and every variable so you can easily change them. Oh here is a giant example script to emphasize what I mean. It is in UnityScript which I am not too fluent in but I gave it my best shot. Cheers! :
#pragma strict
import UnityEngine;
//Max Amounts
var maxCancerCells: int = 100;
var maxDiabetesCells: int = 100;
var maxFluCells: int = 100;
var maxHealth: int = 100;
var maxInsanity: int = 100;
var maxPanic: int = 100;
var maxResourcefulness: int = 100;
//Current Stats
private var cancerCellCount: int;
private diabetesCellCount: int;
private fluCellCount: int;
private currentHealth: int;
private insanityAmount: int;
private panicAmount: int;
private resourcefulnessAmount: int;
//Current Statuses
var cancer: boolean;
var diabetes: boolean;
var dead: boolean;
var flu: boolean;
var insane: boolean;
var panicked: boolean;
function Start() {
this.currentHealth = this.maxHealth;
this.insanityAmount = 0;
this.panicAmount = 0;
this.resourcefulnessAmount = 0;
this.cancer = false;
this.diabetes = false;
this.dead = false;
this.flu = false;
this.insane = false;
this.panicked = false;
}
function Update(){
if (this.cancerCellCount >= this.maxCancerCells) {
this.cancer = true;
}
if (this.currentHealth <= 0) {
this.dead = true;
}
if (this.diabetesCellCount >= this.maxDiabetesCells) {
this.diabetes = true;
}
if (this.fluCellCount >= this.maxFluCells) {
this.flu = true;
}
if (this.insanityAmount >= this.maxInsanity) {
this.insane = true;
}
if (this.panicAmount >= this.maxPanic) {
this.panicked = true;
}
}
public GetCancerCellCount() : int {
return this.cancerCellCount;
}
public function SetCancerCellAmount(cancerCells: int) {
this.cancerCellCount = cancerCells;
}
public GetDiabetesCellCount() : int{
return this.diabetesCellCount;
}
public function SetDiabetesCellAmount(diabetesCells: int){
this.diabetesCellCount = diabetesCells;
}
public GetFluCellCount(): int {
return this.fluCellCount;
}
public function SetFluCellAmount(fluCells: int){
this.fluCellCount = fluCells;
}
public GetHealth(): int {
return this.currentHealth;
}
public function SetHealth(healthToSetAs: int){
this.currentHealth = healthToSetAs;
}
public AddHealth(healthToAdd: int){
this.currentHealth += healthToAdd;
}
public GetInsanityAmount(): int {
return this.insanityAmount;
}
public function SetInsanityAmount(insanityToSetAs: int){
this.insanityAmount = insanityToSetAs;
}
public function AddInsanityAmount(insanityToAdd: int){
this.insanityAmount += insanityToAdd;
}
public GetPanicAmount(): int {
return this.panicAmount;
}
public function SetPanicAmount(panicToSetAs: int){
this.panicAmount = panicToSetAs;
}
public function AddPanicAmount(panicToAdd: int){
this.panicAmount += panicToAdd;
}
public HasCancer(): boolean {
return this.cancer;
}
public HasDiabetes(): boolean {
return this.diabetes;
}
public HasTheFlu(): boolean {
return this.flu;
}
public function SetCancer(checkForCancer: boolean){
this.cancer = checkForCancer;
}
public function SetDiabetes(checkForDiabetes: boolean){
this.diabetes = checkForDiabetes;
}
public function SetTheFlu(checkForFlu: boolean){
this.flu = checkForFlu;
}
You would then use the script like this:
Playerhealthscript.AddInsanityAmount(1);
Answer by Bonfire-Boy · Jan 31, 2018 at 04:38 PM
There are many ways to achieve the kind of thing you're after, and the best solution is going to depend on the details of what you're trying to achieve.
But one simple solution that gives you exactly what you're asking for, would be to use a Dictionary. By "what you're asking for" I am talking not only about the fact that you want to be able to specify the variable to modify by using a string, but also the implied fact that all those variables are integers.
In your Playerhealth class you would replace the int hp;
field with something like
Dictionary <string, int> namedInts = new Dictionary< string, int >() { { "name0", 0 }, { "name1", 1 } };
You would then be able to access and use the ints with
string intToModify = "name1"; // or whatever
PlayerhealthScript.namedInts[intToModify] += 1; // and so on
This would be totally dynamic (eg you'd be able to add new "namedInts" to a Playerhealth object at runtime)