playMaker

Author Topic: Help getting value from script...[SOLVED]  (Read 2126 times)

beowulfkaine

  • Playmaker Newbie
  • *
  • Posts: 47
Help getting value from script...[SOLVED]
« on: June 11, 2017, 05:27:23 PM »
Hello everyone! I was wondering if someone can help me with this.  I am NOT a coder, this script is from the Advanced Battle AI system that's free on the asset store.  Essentially I want to be able to get the "currenthealth"

Code: [Select]
using UnityEngine;
using System.Collections;


public class health : MonoBehaviour {
//max health. you set this to what you want his health to be
public int maxhealth=50;
public int currenthealth;
//cant die
public bool invincible;
public bool dead;
//check if ou want to enable health regeneration
public bool regenerate;
//time between each generate
public float regenerationtime=0.8f;
//the timer
private float regtimer;
//amount of hp to regenerate per generate
public int regenerationamount=2;
private int healthsave;
public bool givereward;
private bool rewardgiven;
public int xptogive=10;
public int goldtogive=10;


// Use this for initialization
void Start () {
givereward=true;

AI ai=(AI)GetComponent("AI");
if(ai) ai.health=maxhealth;

currenthealth=maxhealth;
healthsave=maxhealth;
}

// Update is called once per frame
void Update () {



//tell ai when health is changed
if(healthsave>currenthealth|healthsave<currenthealth){
AI ai=(AI)GetComponent("AI");
if(ai){
ai.health=currenthealth;
healthsave=currenthealth;
}
}

//death
if(dead){
AI ai=(AI)GetComponent("AI");
//tell ai that he is dead
if(ai)ai.dead=true;
}
else givereward=true;

if(currenthealth>=maxhealth) currenthealth=maxhealth;

//if health is less than zero
if(currenthealth<=0){
//check if invincible if not death
if(invincible)dead=false;
else dead=true;
currenthealth=0;
}
//regenerate
if(regenerate){
if(currenthealth<maxhealth&currenthealth>0){
regtimer+=Time.deltaTime;
if(regtimer>regenerationtime){
currenthealth=currenthealth+regenerationamount;
regtimer=0;
}

}
}
}
}

I want to be able to pass the currenthealth value to an fsm variable that I can then check to see if 0 then I can do anything else that I want with it.  But the checking the value thing is getting me stumped.

Also the XPtogive and the Goldtogive as well.  Is it possible to have this value also take away instead of giving?  An example is if an enemy kills this AI then xp is taken away, or anything else I choose.

Here is my intended use:

I have two armies that are fighting each other. Each uses the same AI script, my concept is that when an enemy dies gold is given when an ally dies gold is taken away.  The gold is then set as a value that can be used later for like power-ups ect..

Your fighting along allies and everything you kill garners gold as well as your allies.  But if your army gets overwhelmed, then the gold starts depleting and you end up losing because you can afford to repair your castle or buy new stuff, ect..
« Last Edit: June 13, 2017, 06:00:46 AM by jeanfabre »

Ofonna

  • Full Member
  • ***
  • Posts: 230
Re: Help getting value from script...
« Reply #1 on: June 11, 2017, 07:08:08 PM »
attach the script to your enemy gameobject and drag it down to your fsm state, select the get or set depending on what you're trying to do, you would see a drop down with a list of the scrips variables, functions etc, you can also use the invoke method action to call a functions script, depends on what you really want, try that, the script looks pretty old because i see some references that aren't initialized in the name space like Ai. but test it out tho.

beowulfkaine

  • Playmaker Newbie
  • *
  • Posts: 47
Re: Help getting value from script...
« Reply #2 on: June 11, 2017, 07:14:39 PM »
attach the script to your enemy gameobject and drag it down to your fsm state, select the get or set depending on what you're trying to do, you would see a drop down with a list of the scrips variables, functions etc, you can also use the invoke method action to call a functions script, depends on what you really want, try that, the script looks pretty old because i see some references that aren't initialized in the name space like Ai. but test it out tho.

When I drag the script into the Get property action in the fsm I do have options, but none that are listed within the script itself?

Ofonna

  • Full Member
  • ***
  • Posts: 230
Re: Help getting value from script...
« Reply #3 on: June 12, 2017, 07:39:59 PM »
hmmm it should, change the vairables to public and try again, i just tested it out and it worked for me

beowulfkaine

  • Playmaker Newbie
  • *
  • Posts: 47
Re: Help getting value from script...
« Reply #4 on: June 12, 2017, 09:08:31 PM »
hmmm it should, change the vairables to public and try again, i just tested it out and it worked for me

Thank you that did work, I appreciate your help!

Ofonna

  • Full Member
  • ***
  • Posts: 230
Re: Help getting value from script...[SOLVED]
« Reply #5 on: June 14, 2017, 11:03:26 AM »
no problem, goodluck!