Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Create my own 3D RPG ~ Learn to Code

Discussion in 'Scripting' started by Wazanoob, Apr 21, 2021.

  1. Wazanoob

    Wazanoob

    Joined:
    Apr 18, 2021
    Posts:
    10
    Hello everyone!
    First of all I just started two days ago to learn coding so be indulgent please :oops:
    I'm currently creating my own world to practice and learn faster about C#... but it's very hard to find real lessons online that explain the very basics.

    Anyway here one of my scripts, Players status ( I've also PlayerController, CameraController, ControlBinding, Controls but they look fine to me for now)

    Everything is working well, the health, mana, energy move as i want to and my char level up when he has experience
    BUT when I try to add this line: " XpNextLvl = 5 * (10 * level * Mathf.Pow(level, 1f)); "
    This other line give me an error " xpSlider.fillAmount = Slider(XpNextLvl, experience); "
    (62,38): error CS1503: Argument 1: cannot convert from 'float' to 'int'
    (73,38): error CS1503: Argument 1: cannot convert from 'float' to 'int'

    I honestly don't know what to do to correct that, I tried to used other codes lines in order to do my exp bar differently, but I m always missing something and it wont work :rolleyes:
    So please... How can I fix it without changing everything ? :(


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6.  
    7.  
    8. public class PlayerStatus : MonoBehaviour
    9. {
    10.     public string playerName;
    11.  
    12.     //Stats
    13.     //Secondary
    14.     public SecondaryStats currentSecondary;
    15.     [HideInInspector]
    16.     public SecondaryStats maxSecondary;
    17.  
    18.     //Primary
    19.     public PrimaryStats currentPrimary;
    20.     [HideInInspector]
    21.     public PrimaryStats basePrimary;
    22.  
    23.     //Experience
    24.     public float level;
    25.     public float experience;
    26.     public float XpNextLvl;
    27.     public Image xpSlider;
    28.     public TextMeshProUGUI xpText, levelText;
    29.     public bool addXp;
    30.     public int addXpAmount;
    31.  
    32.  
    33.  
    34.     //UI references
    35.     //Name
    36.     public TextMeshProUGUI nameText;
    37.  
    38.     //Health
    39.     public Image healthSlider;
    40.     public TextMeshProUGUI healthText;
    41.  
    42.     //Magic
    43.     public Image magicSlider;
    44.     public TextMeshProUGUI magicText;
    45.  
    46.     //Energy
    47.     public Image energySlider;
    48.     public TextMeshProUGUI energyText;
    49.  
    50.  
    51.     void Start()
    52.     {
    53.         StatSetup(10, 10, 10, 10, 10);
    54.         SetPlayerName();
    55.  
    56.         level = 1;
    57.         experience = 0;
    58.         XpNextLvl = 50;
    59.  
    60.         levelText.SetText(level.ToString());
    61.  
    62.         //xpSlider.fillAmount = Slider(XpNextLvl, experience);
    63.         xpText.SetText("XP: " + experience.ToString() + "/" + XpNextLvl.ToString());
    64.     }
    65.  
    66.     void Update()
    67.     {
    68.         LimitStatValues();
    69.         UpdateStatus();
    70.  
    71.         XpNextLvl = 5 * (10 * level * Mathf.Pow(level, 1f));
    72.  
    73.         //xpSlider.fillAmount = Slider(XpNextLvl, experience);
    74.         xpText.SetText("XP: " + experience.ToString() + "/" + XpNextLvl.ToString());
    75.  
    76.         if (addXp)
    77.         {
    78.             UpdateXp();
    79.             addXp = false;
    80.         }
    81.     }
    82.  
    83.     void StatSetup(int str, int agi, int intl, int stam, int spr)
    84.     {
    85.         basePrimary = new PrimaryStats(str, agi, intl, stam, spr);
    86.         currentPrimary = new PrimaryStats(basePrimary.strength, basePrimary.agility, basePrimary.intellect, basePrimary.stamina, basePrimary.spirit);
    87.  
    88.         maxSecondary = new SecondaryStats(currentPrimary.stamina * 10, currentPrimary.intellect * 10, currentPrimary.agility * 10);
    89.         currentSecondary = new SecondaryStats(maxSecondary.health, maxSecondary.magic, maxSecondary.energy);
    90.     }
    91.  
    92.     float Slider(int maxStat, int currentStat)
    93.     {
    94.         float sliderValue = 0;
    95.  
    96.         sliderValue = (float)currentStat / (float)maxStat;
    97.  
    98.         return sliderValue;
    99.     }
    100.  
    101.     void SetPlayerName()
    102.     {
    103.         nameText.SetText(playerName);
    104.     }
    105.  
    106.     void UpdateStatus()
    107.     {
    108.         //health
    109.         healthSlider.fillAmount = Slider(maxSecondary.health, currentSecondary.health);
    110.         healthText.SetText(currentSecondary.health.ToString() + "/" + maxSecondary.health);
    111.  
    112.         //Magic
    113.         magicSlider.fillAmount = Slider(maxSecondary.magic, currentSecondary.magic);
    114.         magicText.SetText(currentSecondary.magic.ToString() + "/" + maxSecondary.magic);
    115.  
    116.         //Energy
    117.         energySlider.fillAmount = Slider(maxSecondary.energy, currentSecondary.energy);
    118.         energyText.SetText(currentSecondary.energy.ToString() + "/" + maxSecondary.energy);
    119.     }
    120.  
    121.     void LimitStatValues()
    122.     {
    123.         currentSecondary.health = Mathf.Clamp(currentSecondary.health, 0, maxSecondary.health);
    124.         currentSecondary.magic = Mathf.Clamp(currentSecondary.magic, 0, maxSecondary.magic);
    125.         currentSecondary.energy = Mathf.Clamp(currentSecondary.energy, 0, maxSecondary.energy);
    126.     }
    127.  
    128.    void UpdateXp()
    129.     {
    130.         experience += addXpAmount;
    131.  
    132.         if(experience >= XpNextLvl)
    133.         {
    134.             experience -= XpNextLvl;
    135.             level++;
    136.  
    137.             levelText.SetText(level.ToString());
    138.         }
    139.     }
    140.  
    141. }
    142.  
    143. [System.Serializable]
    144. public class PrimaryStats
    145. {
    146.     public int strength, agility, intellect, stamina, spirit;
    147.  
    148.     public PrimaryStats(int str, int agi, int intl, int stam, int spr)
    149.     {
    150.         strength = str;
    151.         agility = agi;
    152.         intellect = intl;
    153.         stamina = stam;
    154.         spirit = spr;
    155.     }
    156. }
    157.  
    158. [System.Serializable]
    159. public class SecondaryStats
    160. {
    161.     public int health, magic, energy;
    162.  
    163.     public SecondaryStats(int heal, int mag, int nrg)
    164.     {
    165.         health = heal;
    166.         magic = mag;
    167.         energy = nrg;
    168.     }
    169.  
    170. }
    Thanks a lot for your help!

    Ps: I would like to keep this post if I have any other issues while I m learning to code, I will keep it edited with all my mistakes corrected for the others begginers that interested.


    Edit:
    I replaced float Slider(int maxStat, int currentStat) per float Slider(float maxStat, float currentStat)
     
    Last edited: Apr 26, 2021
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    you defined your experience and XpNextLvl as float type numbers (line 25 and 26)
    float are numbers that can have decimals, like 1.5; 0.3. etc

    you defined your Slider() function, to take in int type numbers as input, see line 92
    float Slider(int maxStat, int currentStat)
    int are whole numbers that dont have decimals, like 0; 1; 2; 3; 4 etc

    c# wont auto convert float into int values, which is what your error is saying
    you should think about wether you need your experience as float (numbers that can have decimals), or as int (only whole numbers) and depending on what you want you want to

    either:

    change your Slider function to take in floats, after all you are also calculating with floats in it:

    float Slider(float maxStat, float currentStat)

    or:

    change your experience variables in line 25 and 26 to int instead of float


    Edit:
    a quick bandage for thos problems is also to force convert the types, as in whenever such an int/ float conversion problem comes up, you could technicly just put a (float) or (int) in front of the value that throws the error

    like
    xpSlider.fillAmount = Slider((int)XpNextLvl, (int)experience);

    which would also "solve" your problem...
    tho with that you may be hiding design errors in your code and might later on run into bugs you have trouble finding

    because when casting float to int, you are loosing information: 3.5 as float would become 3 as int
     
    Last edited: Apr 21, 2021
  3. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    also because you said you have trouble finding real lessons online:

    Brackeys and Sebastian Lague have collabed on a 3D RPG Unity Tutorial a while back, here is the first video in their tutorial series:



    its what I watched to learn how Unity works
     
    Kurt-Dekker likes this.
  4. Wazanoob

    Wazanoob

    Joined:
    Apr 18, 2021
    Posts:
    10
    @TheNightglow Oh men thank you!!
    I feel so stupid I tried all afternoon fixing it, puting int or float everywhere in lines 62 and 73 but not in the current Slider function ..... silly me.

    Its working all well now thank you !!

    And yes I watched all their tutos but there's not what i want, I uselually find some code lines from a video to another and try to make them work together. Most of my lines comes from CodeMonkey or Soupertrooper
    (I think that guy is underated so I leave a little link for the others noobies like me)

    https://www.youtube.com/channel/UCQNgsbFV54eZHY3jYxQQaqg
     
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    I'm amazed that after just two days of learning to program you feel ready to take on making an RPG game!

    I was programming for a very long time before I had the knowledge to attempt something that complex, I guess I'm just not that bright :) (although to be fair, that was back in the days when you had to write everything from scratch, no ready made game engines to make use of).

    Anyway, good luck, try not to get too discouraged if it doesn't go as well you hoped to start with.
     
  6. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    Its generaly a good idea to start with a small learning project whenever you start with new tools and stuff...

    because the first code you write to solve a new problem or using new tools is usually not very well maintainable in the long term, so starting with a small mini game that maybe shares some of the mechanics you want for your later projects is a good idea

    starting with a large scale project when you have little experience with the tools you are using, may lead to you having to rework larger parts of your code/ scene structure later in the project once you have figgured out easier maintainable methods
     
  7. Wazanoob

    Wazanoob

    Joined:
    Apr 18, 2021
    Posts:
    10

    Haha you are to kind, maybe I'm just way to ambitious. But thank you! I will try to do my best to stay focus and overcome all the difficulties incoming.


    I can only be agree with you. I already know that I will have to change most of my Codes lines when my skills will have improve. But I think its a good exercices for me to see if I really understand my codes, in order to change anything that's wrong, to clean, order, and upgrade them and see how much I progressed since Day One :D
     
    Munchy2007 likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Remember, these are always super-simple errors. You can start a dialog in your head and solve them instantly. Here's some guidance:

    http://plbm.com/?p=263