How can i get - or set - Prefab variable like HP or EXP via c# ?
  • i can't use something like this :

    Combatant.Status["HP"].GetValue();

    but only can use :

    Combatant.Status[0].GetValue();

    any possible to use :

    Combatant.Status["HP"].SetValue(2000);

  • When you say prefab - are you talking about a defined combatant that also happens to have a prefab, or are you talking about an Object Variable attached to your prefab? Your example leads me to believe it's the former, but I'm not sure.

    You don't need to specifically use the name "HP" to look stuff up. Status here is looking for the id number of your defined status value. Go look in your ORK project file and see what ID numbers are for the values you want to change. So for example, if your HP value was the very first thing you entered, it'd be in the ID slot 0. Combatant.Status[0].GetValue(); would then return the value of your HP of that combatant. If it were in ID slot 6, you'd just use Combatant.Status[6].GetValue(); instead.

    Assuming this script it attached to the prefab of your combatant you want to use (which has a combatant component on it):

    You need to include this at the top of your script file:
    using ORKFramework;
    using ORKFramework.Behaviours;
    and make yourself a variable:
    private CombatantComponent combantantComponenet;

    Then in the Start function grab a reference to your combatant
    component:
    combantantComponenet = gameObject.GetComponent<CombatantComponent> ();
    Now you can do this to change your HP (assuming HP is id 0):
    combantantComponenet.combatant.Status [0].SetValue (2000);

    My little chunk of the internet: http://artemic.com
  • If instead HP is an Object variable you made attached to your prefab, you can reference it like this:

    VariableHandler OBJHandler = gameObject.GetComponent<ObjectVariablesComponent> ().GetHandler ();
    OBJHandler.Set ("HP", 2000);


    assuming HP is the name of a float Object Variable on the prefab.
    My little chunk of the internet: http://artemic.com
  • Hi Firrerreo , thanks for support

    First i spawn my player by attach script to empty gameobject contain :

    using UnityEngine;
    using System.Collections;
    using ORKFramework;
    using ORKFramework.Behaviours;

    public class myspwan : MonoBehaviour {

    Combatant my_Hero;

    void Start () {

    Group group = ORK.Game.PlayerHandler.GetGroup(0);
    my_Hero = ORK.Combatants.Create(0, group);
    my_Hero.Init();

    ORK.Game.PlayerHandler.SpawnPlayer(transform.position,false, 180f, false,new Vector3 (1f,1f,1f)); // --> Spawn OK

    }
    }

    ======================

    here is my attached script to my player prefab :

    using UnityEngine;
    using System.Collections;
    using ORKFramework;
    using ORKFramework.Behaviours;

    public class exp : MonoBehaviour {

    Combatant me;

    void Start () {

    me = ComponentHelper.GetCombatant(gameObject);

    me.Init();

    if (me != null)
    {

    me.Status [1].SetValue (909, true, true, true, false, true, true); //-->Set OK

    }

    print (me.Status[1].GetValue()+""); //-->Get OK


    void Update () {

    if ( me.Dead )
    {
    print ("i m dead");

    }

    }

    public void do_dead()
    {
    //me.DestroyPrefab(); // --> this give me error !
    //me.Death(); // --> this give me error !

    if (me != null) {

    me.Status [1].SetValue (1009, true, true, true, false, true, true); //--> give me error

    }
    else{
    print (" any use of Combatant ( me ) in this function give me error : NullReferenceException: Object reference not set to an instance of an object"); //-> this message appear !!!

    }

    }
    ===================
    my new Qs is :

    - is the code above good ?

    - how can i solve problem of die ?

    - did ork control any thing if i just only play, example :if HP =0 is the player die automaticly ? what about exp , is't increase by ork in play or i must do it by code ?

    - can i call some appility , start combat,change turn and do damage to other enemy manually by script ?

    Sorry for long Q...
  • edited August 2015
    the following code gets you your status values toss it on a update but beware it will drop your fps if you do to many at once.


    if (combatantComponent != null) {
    Combatant combatant = combatantComponent.combatant;

    currentHealth = combatant.Status [0].GetValue ();
    maxHealth = combatant.Status [1].GetValue ();
    }




    and if you just need to do something on change you can do it this way,

    void Start {
    CombatantComponent combatantComponent = playerCharacter.
    GetComponent<CombatantComponent> ();
    if (combatantComponent != null) {
    Combatant combatant = combatantComponent.combatant;
    combatant.Status[11].Changed += StatusValueChanged;/* <-- allows registration the vlue so every time it changes you can do somthign int he function it calles */
    }

    }



    public void StatusValueChanged/* <-- this can be named what ever you want */(Combatant combatant, int id/*<-- so can this */, int change/*<-- change can also be named what ever*/){
    id = 11;

    // do what ever in here from if statements to just callign other functions
    currentPistolAmmo = combatant.Status [11].GetValue ();


    }

    void OnDestroy(){

    combatant.Status[11].Changed -= StatusValueChanged;/*<-- unregisteres it when object is destroyed */

    }




    good luck have fun :p
    Post edited by wtyson on
    new website can be found here http://www.fore-loregames.com

    Follow the game Development on Twitter https://twitter.com/Fore_Lore_Games

    or check out the face book page here https://www.facebook.com/ForeLoreGames
  • and to answer your question you need to get the combatant component not seeing where your getting it at all at least try to get i ton start or awake.
    new website can be found here http://www.fore-loregames.com

    Follow the game Development on Twitter https://twitter.com/Fore_Lore_Games

    or check out the face book page here https://www.facebook.com/ForeLoreGames
  • In your start function, you should always do a null check first to make sure that your prefab has a combatant component to grab in the first place, and not just assume it's always going to be there.
    CombatantComponent combatantComponent = gameObject.GetComponent<CombatantComponent>();
    if(combatantComponent != null)
    {
    Combatant me = combatantComponent.combatant;
    }
    else
    {
    Debug.Log("OH NOES I have no combatant component");
    }
    so "me" will now be a reference to your combatant, provided that component exists (not just the prefab). If it doesn't, you'll get a debug message. Does your prefab have a combatant component on it, cause it kind of sounds like from your error message that it doesn't have one. And if you're creating combatants at runtime and not using the ones you make in the project file, you're going to need to make sure it's got one.

    And as @Wtyson said, don't do status checks in an update function -it'll destroy your framerate. Register to the event like he says to see if/when your HP drops.

    As for death handling, did you actually set up the status value stuff in the project file? ORK will handle a lot of the death stuff if you tell it to there- in the base settings of your HP there's a "Death On" field - set it to on minimum to have it die when it reaches your floor value. EXP is also handled by ORK, but you also have to set up the base parameters in the project file.

    You can do stuff like start combat from a script, but I wouldn't personally mess with the battle stuff that way. Have you tried making an event and just hooking up your custom code in there through callbacks? That might be a bit more straightforward.
    My little chunk of the internet: http://artemic.com
  • Firrerreo ,wtyson : very important information.

    thank you very very mutch
Sign In or Register to comment.