SOLVED Would this be 1 or more than 1 script?

flyinian

Member
I've been learning how scripts work and was wondering if the below code can be placed into one script or i will need multiple scripts for it to work.

My guess, I need multiple scripts.

Code:
if (Requirements_Met == true && Allow_Leveling)  
{
    if(Current_Experience < Next_Experience)  
    {// If leveling hasnt been achieved yet
        Current_Experience += 3;
    }
    else
    {// Level up achieved
        Next_Experience *= Experience_Multiplier;      
        Current_Level ++;    // Level up                          
    };

    Done_Completion = Current_Experience / Max_Experience * 100;  

    if (Current_Experience >= Next_Experience && Current_Level >= Max_Level)  
    {
        Allow_Leveling = false;
        Value_Checking = true;  
    };
}
else
{
    if (Value_Checking)  
    {
        if (Current_Experience > Max_Experience)
        {
            Current_Experience = Max_Experience;
        };
           
        if (Current_Level > Max_Level)
        {
            Current_Level = Max_Level;
        };
           
        if (Done_Completion > 100)
        {
            Done_Completion = 100;
        };
        Value_Checking = false;  
    };
};
 

Evanski

Raccoon Lord
Forum Staff
Moderator
It looks like your code can just be in one script but it would be determined on how you use the code, when you call it, and why you need it to be a script
 

kburkhart84

Firehammer Games
I don't see anything in there that screams "multiple scripts" at me. My general rule for scripts/functions is similar to how I design objects. I like to keep things separate and in re-usable chunks when possible. And I like for them to basically do one thing, and one thing only. The idea is that if in the future you want to do just part of a script that does multiple things, you will have to split it up.

The other idea I follow is to make sure that anything that will get re-used gets put into a script. My input system(which I'm re-writing for version 2.3) will throw errors if you call functions with invalid values. All those errors will say "FHInput: " at the beginning so it is clear where the error came from. I have a single function that throws that error, taking a string to add onto that prefix. Then, in any function that needs to throw an error, I just call that function.

I also expanded on that concept with what are called "asserts" in C++(and maybe other languages). It basically makes an assumption about some variable, and then if it isn't correct, it stops the program. It is usually used when the error is serious enough to not continue. In my input system, you have to call a function to initialize it first...then you can call functions to do things with it, like checking input. I doing a sort of "assert" on those function that verify that the thing has even been initialized. If I just assume they are initialized, then you will end up trying to access objects and data structures that haven't been created yet. Since the code that verifies the status of initialization of the system is used every time you try to call functions to check on inputs, I made that into a function. Note that by the time I'm done, many of my functions are only a few lines...but that's not a bad thing IMO. Each one does its one little job, and nothing else.
 
Top