Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

RPG - FREE Script

Discussion in 'Made With Unity' started by Wikened, Nov 27, 2011.

  1. Wikened

    Wikened

    Joined:
    Oct 31, 2011
    Posts:
    271
    Hey guys,
    Heres another free script script from me... :D
    This is a simple RPG script allowing you to save/load characters, load XP, leveling system and death system. You will need to tweak it to your liking and you will need to add all the NPC's. I will be creating a RPG script for NPC's. Soon, this one is only for players.

    Instructions:
    1. Attach the script to your player.
    2. Create a cube with a collider.
    3. Make your character collide into the cube, and this will add xp or if you comment out the damage input, kill you after a few hits.

    If you are going to use it please give credit. (My name is in the script)

    My website: Here
    My Youtube: Here

    YOU CAN DOWNLOAD IT HERE:
    http://dl.dropbox.com/u/31764591/Tutorials/PlayerStatus.js

    *Note: Sorry but coping it to Unity forums puts spaces everywhere...
    Code (csharp):
    1.  
    2. import System;
    3.  
    4. import System.Collections;
    5.  
    6. import System.Xml;
    7.  
    8. import System.Xml.Serialization;
    9.  
    10. import System.IO;
    11.  
    12. import System.Text;
    13.  
    14.  
    15.  
    16. //****************************
    17.  
    18. /*
    19.  
    20.     Update 0.1 Of the script.
    21.  
    22.     Owner: Vincent Racine
    23.  
    24.    
    25.  
    26.     Upgrade of the script
    27.  
    28.     coming soon.
    29.  
    30.    
    31.  
    32. Features:
    33.  
    34.     Save/Load Position
    35.  
    36.     Save/Load Name
    37.  
    38.     Save/Load Exp and Level
    39.  
    40.     Exp/Level System
    41.  
    42.     *BASIC* combat system
    43.  
    44.  
    45.  
    46. Credit:
    47.  
    48.     Vincent Racine
    49.  
    50.     Zumwalt (XML Code)
    51.  
    52.     Unity
    53.  
    54. */
    55.  
    56. //****************************
    57.  
    58.  
    59.  
    60. class DemoData
    61.  
    62. {
    63.  
    64.     var x : float;
    65.  
    66.     var y : float;
    67.  
    68.     var z : float;
    69.  
    70.     var name : String;
    71.  
    72.     var experience : float;
    73.  
    74. }
    75.  
    76.  
    77.  
    78. class UserData
    79.  
    80. {
    81.  
    82.    
    83.  
    84.    public var _iUser : DemoData = new DemoData();
    85.  
    86.  
    87.  
    88.    function UserData() { }
    89.  
    90. }
    91.  
    92.  
    93.  
    94. var target: Transform;
    95.  
    96.  
    97.  
    98. var generalWidth = 50;
    99.  
    100. var userHeight = 50;
    101.  
    102.  
    103.  
    104. var playerHealth = 1000;
    105.  
    106. var playerMana = 1000;
    107.  
    108. var playerEnergy = 100;
    109.  
    110.  
    111.  
    112.  
    113.  
    114. var playerCurrency = 5000;
    115.  
    116.  
    117.  
    118. var playerPositionX = 0;
    119.  
    120. var playerPositionY = 0;
    121.  
    122. var playerPositionZ = 0;
    123.  
    124.  
    125.  
    126. var damageNpc = 100;
    127.  
    128.  
    129.  
    130.  
    131.  
    132.  
    133.  
    134. //Variable For Experience *******************************************************
    135.  
    136. var curExperience = 1;
    137.  
    138. var curLevel = 1;
    139.  
    140. //EndofExpVariables *************************************************************
    141.  
    142.  
    143.  
    144. //Saving Variables **************************************************************
    145.  
    146. private var _Save : Rect;
    147.  
    148. private var _Load : Rect;
    149.  
    150.  
    151.  
    152. private var _SaveMSG : Rect;
    153.  
    154. private var _LoadMSG : Rect;
    155.  
    156.  
    157.  
    158. //var _ShouldSave : boolean;
    159.  
    160. //var _ShouldLoad : boolean;
    161.  
    162. //var _SwitchSave : boolean;
    163.  
    164. //var _SwitchLoad : boolean;
    165.  
    166. private var _FileLocation : String;
    167.  
    168. private var _FileName : String = "SaveData.xml";
    169.  
    170.  
    171.  
    172. //public GameObject player;
    173.  
    174. var player : GameObject;
    175.  
    176. var playerName : String = "Default";
    177.  
    178.  
    179.  
    180. private var myData : UserData;
    181.  
    182. private var _data : String;
    183.  
    184.  
    185.  
    186. private var VPosition : Vector3;
    187.  
    188. //EndOfSaving Variables *********************************************************
    189.  
    190.  
    191.  
    192. function Awake () {
    193.  
    194.       // GUI for the Save/Load
    195.  
    196.       _Save=new Rect(50,180,100,20);
    197.  
    198.       _Load=new Rect(50,210,100,20);
    199.  
    200.       _Name=new Rect(50,240,200,20);
    201.  
    202.       _SaveMSG=new Rect(50,120,200,40);
    203.  
    204.       _LoadMSG=new Rect(50,140,200,40);
    205.  
    206.       _NameMSG=new Rect(50,180,200,40);
    207.  
    208.        
    209.  
    210.       // XML Save/Load path
    211.  
    212.       _FileLocation=Application.dataPath;
    213.  
    214.      
    215.  
    216.          
    217.  
    218.       // Creating something to store information into.
    219.  
    220.       myData=new UserData();
    221.  
    222.    }
    223.  
    224.  
    225.  
    226. function OnGUI ()
    227.  
    228. {
    229.  
    230.  
    231.  
    232.     playerName = GUI.TextArea (Rect(generalWidth,userHeight,100,25),playerName);
    233.  
    234.    
    235.  
    236.     //Health ********************************************************************
    237.  
    238.     GUI.Box (Rect (50,75,150,25), "Health:" + playerHealth);
    239.  
    240.     //EndOfHealth ***************************************************************
    241.  
    242.    
    243.  
    244.     //Mana **********************************************************************
    245.  
    246.     GUI.Box (Rect (50,100,150,25), "Mana:" + playerMana);
    247.  
    248.     //EndOfMana *****************************************************************
    249.  
    250.    
    251.  
    252.     //Energy ********************************************************************
    253.  
    254.     GUI.Box (Rect (50,125,150,25), "Energy:" + playerEnergy);
    255.  
    256.     //EndOfEnergy ***************************************************************
    257.  
    258.    
    259.  
    260.     //Currency ******************************************************************
    261.  
    262.     GUI.Box (Rect (50,150,150,25), "Gold:" + playerCurrency);
    263.  
    264.     //EndOfCurrency *************************************************************
    265.  
    266.    
    267.  
    268.     //Experience ****************************************************************
    269.  
    270.     GUI.Box (Rect (50,25,150,25),"Level:" + curLevel);
    271.  
    272.     //EndOfExperience ***********************************************************
    273.  
    274.    
    275.  
    276.  
    277.  
    278.     //LoadingPlayers ************************************************************      
    279.  
    280.     if (GUI.Button(_Load,"Load")) {
    281.  
    282.      
    283.  
    284.       GUI.Label(_LoadMSG,"Loading from: "+_FileLocation);
    285.  
    286.       // Load our UserData into myData
    287.  
    288.       LoadXML();
    289.  
    290.       if(_data.ToString() != "")
    291.  
    292.       {
    293.  
    294.          myData = DeserializeObject(_data);
    295.  
    296.          VPosition=new Vector3(myData._iUser.x,myData._iUser.y,myData._iUser.z);            
    297.  
    298.          player.transform.position=VPosition;
    299.  
    300.          curExperience = myData._iUser.experience;
    301.  
    302.          Debug.Log("Load Successful");
    303.  
    304.       }
    305.  
    306.          
    307.  
    308.    }
    309.  
    310.    //EndOfLoadingPlayers *******************************************************
    311.  
    312.    
    313.  
    314.    //SavingPlayers *************************************************************
    315.  
    316.    if (GUI.Button(_Save,"Save")) {
    317.  
    318.            
    319.  
    320.       GUI.Label(_SaveMSG,"Saving to: "+_FileLocation);
    321.  
    322.       //Debug.Log("SaveLoadXML: sanity check:"+ player.transform.position.x);
    323.  
    324.      
    325.  
    326.       myData._iUser.x = player.transform.position.x;
    327.  
    328.       myData._iUser.y = player.transform.position.y;
    329.  
    330.       myData._iUser.z = player.transform.position.z;
    331.  
    332.       myData._iUser.name = playerName;
    333.  
    334.       myData._iUser.experience = curExperience;  
    335.  
    336.        
    337.  
    338.       // Time to creat our XML!
    339.  
    340.       _data = SerializeObject(myData);
    341.  
    342.       // This is the final resulting XML from the serialization process
    343.  
    344.       CreateXML();
    345.  
    346.       Debug.Log(_data);
    347.  
    348.    }
    349.  
    350.    //EndOfSavingPlayers *********************************************************
    351.  
    352. }
    353.  
    354.  
    355.  
    356. function Update ()
    357.  
    358. {
    359.  
    360.     if(playerHealth <= 0) {
    361.  
    362.         print(playerName + "has died...");
    363.  
    364.         die();
    365.  
    366.     }
    367.  
    368.     // Here is the level decider. If the current exp is higher then the number there then you +1 level.
    369.  
    370.     switch(curLevel){
    371.  
    372.         case 0:
    373.  
    374.         if(curExperience <= 24){
    375.  
    376.          curLevel += 0;
    377.  
    378.          }
    379.  
    380.         case 1:
    381.  
    382.         if(curExperience >= 25){
    383.  
    384.          curLevel += 1;
    385.  
    386.          }
    387.  
    388.        break;
    389.  
    390.         case 2:
    391.  
    392.         if(curExperience >= 55){
    393.  
    394.          curLevel += 1;
    395.  
    396.          }
    397.  
    398.        break;
    399.  
    400.        case 3:
    401.  
    402.        if(curExperience >= 95){
    403.  
    404.          curLevel += 1;
    405.  
    406.          }    
    407.  
    408.        break;
    409.  
    410.        case 4:
    411.  
    412.         if(curExperience >= 130){
    413.  
    414.          curLevel += 1;
    415.  
    416.          }
    417.  
    418.        break;
    419.  
    420.        case 5:
    421.  
    422.         if(curExperience >= 182){
    423.  
    424.          curLevel += 1;
    425.  
    426.          }
    427.  
    428.        break;
    429.  
    430.        case 6:
    431.  
    432.         if(curExperience >= 254){
    433.  
    434.          curLevel += 1;
    435.  
    436.          }
    437.  
    438.        break;
    439.  
    440.        case 7:
    441.  
    442.         if(curExperience >= 356){
    443.  
    444.          curLevel += 1;
    445.  
    446.          }
    447.  
    448.        break;
    449.  
    450.        case 8:
    451.  
    452.         if(curExperience >= 500){
    453.  
    454.          curLevel += 1;
    455.  
    456.          }
    457.  
    458.        break;
    459.  
    460.        case 9:
    461.  
    462.         if(curExperience >= 700){
    463.  
    464.          curLevel += 1;
    465.  
    466.          }
    467.  
    468.        break;
    469.  
    470.        case 10:
    471.  
    472.         if(curExperience >= 990){
    473.  
    474.          curLevel += 1;
    475.  
    476.          }
    477.  
    478.        break;
    479.  
    480.     }
    481.  
    482.    
    483.  
    484.    
    485.  
    486. }
    487.  
    488.  
    489.  
    490. function die()
    491.  
    492. {
    493.  
    494.     player.transform.position = Vector3(5,0,5); // A vector 3 teleport
    495.  
    496.     playerHealth = 1000;
    497.  
    498.     playerMana = 1000;      // Reseting the vitals or else it will loop the death(HP = 0)
    499.  
    500.     playerEnergy = 100;
    501.  
    502. }
    503.  
    504.  
    505.  
    506.  
    507.  
    508. function OnCollisionEnter(collision : Collision) {
    509.  
    510.    
    511.  
    512.     if(target == null  GameObject.FindGameObjectsWithTag("Enemy"))
    513.  
    514.     target = GameObject.FindWithTag("Enemy").transform;
    515.  
    516.        
    517.  
    518. }
    519.  
    520.  
    521.  
    522. function OnTriggerEnter()
    523.  
    524. {
    525.  
    526.     if(target.gameObject.tag == "Enemy")
    527.  
    528.     {
    529.  
    530.         collider.isTrigger = true;
    531.  
    532.         playerHealth -= damageNpc; // Comment this out if you dont wanna take damage.
    533.  
    534.         curExperience += 25; //Exp per interaction with the enemy.
    535.  
    536.     }
    537.  
    538. }
    539.  
    540.  
    541.  
    542. //XML Code *****************************************************************
    543.  
    544.  
    545.  
    546. function UTF8ByteArrayToString(characters : byte[] )
    547.  
    548. {    
    549.  
    550.    var encoding : UTF8Encoding  = new UTF8Encoding();
    551.  
    552.    var constructedString : String  = encoding.GetString(characters);
    553.  
    554.    return (constructedString);
    555.  
    556. }
    557.  
    558.  
    559.  
    560.  
    561.  
    562. function StringToUTF8ByteArray(pXmlString : String)
    563.  
    564. {
    565.  
    566.    var encoding : UTF8Encoding  = new UTF8Encoding();
    567.  
    568.    var byteArray : byte[]  = encoding.GetBytes(pXmlString);
    569.  
    570.    return byteArray;
    571.  
    572. }
    573.  
    574.    
    575.  
    576.  
    577.  
    578. function SerializeObject(pObject : Object)
    579.  
    580. {
    581.  
    582.    var XmlizedString : String  = null;
    583.  
    584.    var memoryStream : MemoryStream  = new MemoryStream();
    585.  
    586.    var xs : XmlSerializer = new XmlSerializer(typeof(UserData));
    587.  
    588.    var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
    589.  
    590.    xs.Serialize(xmlTextWriter, pObject);
    591.  
    592.    memoryStream = xmlTextWriter.BaseStream; // (MemoryStream)
    593.  
    594.    XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
    595.  
    596.    return XmlizedString;
    597.  
    598. }
    599.  
    600.  
    601.  
    602.  
    603.  
    604. function DeserializeObject(pXmlizedString : String)  
    605.  
    606. {
    607.  
    608.    var xs : XmlSerializer  = new XmlSerializer(typeof(UserData));
    609.  
    610.    var memoryStream : MemoryStream  = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
    611.  
    612.    var xmlTextWriter : XmlTextWriter  = new XmlTextWriter(memoryStream, Encoding.UTF8);
    613.  
    614.    return xs.Deserialize(memoryStream);
    615.  
    616. }
    617.  
    618.  
    619.  
    620.  
    621.  
    622. function CreateXML()
    623.  
    624. {
    625.  
    626.    var writer : StreamWriter;
    627.  
    628.  
    629.  
    630.    var t : FileInfo = new FileInfo(_FileLocation+"/"+ _FileName);
    631.  
    632.    if(!t.Exists)
    633.  
    634.    {
    635.  
    636.       writer = t.CreateText();
    637.  
    638.    }
    639.  
    640.    else
    641.  
    642.    {
    643.  
    644.       t.Delete();
    645.  
    646.       writer = t.CreateText();
    647.  
    648.    }
    649.  
    650.    writer.Write(_data);
    651.  
    652.    writer.Close();
    653.  
    654.    Debug.Log("File written");
    655.  
    656. }
    657.  
    658.    
    659.  
    660. function LoadXML()
    661.  
    662. {
    663.  
    664.    //StreamReader r = File.OpenText(_FileLocation+"\\"+ _FileName);
    665.  
    666.    var r : StreamReader = File.OpenText(_FileLocation+"/"+ _FileName);
    667.  
    668.    var _info : String = r.ReadToEnd();
    669.  
    670.    r.Close();
    671.  
    672.    _data=_info;
    673.  
    674.    Debug.Log("File Read");
    675.  
    676. }
    677.  
    678.  
    679.  
    680. //EndOfXML Code **************************************************************
    681.  
    682.  
    683.  
     
  2. Wikened

    Wikened

    Joined:
    Oct 31, 2011
    Posts:
    271
    Please comment on what you think so other can see it! :) If you guys need any help please post it and I WILL help you.
     
  3. impheris

    impheris

    Joined:
    Dec 30, 2009
    Posts:
    1,511
    oooh thanks buddy :) this is great :p

    ...and thanks for wanting to help
     
  4. PoisonedElixir

    PoisonedElixir

    Joined:
    Jul 10, 2013
    Posts:
    2
    i have my own character. and everything seems to be working but when i run/jump on the cube nothing is happening? any help please :)
     
  5. rivalprayudi

    rivalprayudi

    Joined:
    Sep 29, 2014
    Posts:
    1
    thanks bro!
     
  6. rkaetano

    rkaetano

    Joined:
    Aug 27, 2012
    Posts:
    61
    Wikened, you could create a specific function for the level decider, and only call that when you increase the amount of XP.