How do I define a global variable that uses elements from a specific function

Hey, so just wondering how I would define a global variable using elements from a function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  switch (difficulty)
            {
                case 1:
                    health.max = 5.0;
                    health.current = 5.0;

                    mana.max = 40.0;
                    mana.current = 40.0;

                    attack.max = 1.5;
                    attack.current = 1.5;

                    defence.max = 0;
                    defence.current = 0;
                break;

                case 2:
                    health.max = 3.0;
                    health.current = 3.0;

                    mana.max = 20.0;
                    mana.current = 20.0;

                    attack.max = 1.0;
                    attack.current = 1.0;

                    defence.max = 0;
                    defence.current = 0;
                break;

                case 3:
                    health.max = 1.5;
                    health.current = 1.5;

                    mana.max = 10.0;
                    mana.current = 10.0;

                    attack.max = 0.5;
                    attack.current = 0.5;

                    defence.max = 0;
                    defence.current = 0;
                break;

                default:
                    difficulty = 2;
                    setup(1); //this is the function that the code is in
                break;
            }


This code, for example, would be in a function that has the variable, difficulty, given a value when the function is called. I was wondering if it's possible to make this a global variable. Also, the struct is further back, but in the same function

1
2
3
4
5
struct amount
    {
        float max;
        float current;
    };


Thanks in advance for any help I can get
Last edited on
Configuration options belong in a global. It should behave as a dictionary.
All other things should be classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct Configuration
{
  bool load() { /* load from file/registry/whatever */ }
  bool save() { ... }

  Configuration() { /* load() or initialize as empty or whatever */ }
 ~Configuration() { ... }

  template <typename T>
  T get( const std::string& key )
  {
    ...
  }
};

extern Configuration configuration;
 
Configuration configuration;

Use another dictionary for your player's stats:
1
2
3
4
5
6
7
8
9
10
struct Player
{
  Dictionary stats;
  ...

  double health() const { return stats.get<double>("health"); }
  void health( double x ) { stats.set( "health", std::min( x, configuration.get<double>("health.max") ); }

  ...
};

If you wish, you can use a singleton for the configuration object, or any other method convenient to manage its life function.

Hope this helps.
Hello Animatoms,

If I understand you correctly define "difficulty" as a global variable, i.e., at the top of the file before an functions start define int difficulty{ 0 };. Then in any other file that needs this variable, in the global scope of the file, define extern int difficulty;, or whatever type the variable is.

If it works correctly, I say correctly because I have had program where I had problems using an "extern", this should solve your problem.

Hope that helps,

Andy
Thanks guys.

Your solutions worked, and it's working now! Thanks for the help! I decided to move the struct into global, as well as making difficulty global

It's great that there are people willing to help beginners learn things, and I'm really grateful for that!

Thanks again,
Animatoms
Topic archived. No new replies allowed.