Raised This Month: $32 Target: $400
 8% 

[Off-Topic] C++ Game


Post New Thread Reply   
 
Thread Tools Display Modes
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-21-2018 , 13:51   Re: [Off-Topic] C++ Game
Reply With Quote #11

Quote:
Originally Posted by edon1337 View Post
By 'external events' you mean the private functions? I'm sure all of the variables I've created must be global (score, gameOver, coordinates)
No, I mean events like Spawn, pfnSetModel, basically anything that is called from the outside.
None of your vars should be global actually.
__________________
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-21-2018 , 13:54   Re: [Off-Topic] C++ Game
Reply With Quote #12

Quote:
Originally Posted by HamletEagle View Post
No, I mean events like Spawn, pfnSetModel, basically anything that is called from the outside.
Oh.

Quote:
Originally Posted by HamletEagle View Post
None of your vars should be global actually.
Why? Isn't C++ same as Pawn when it comes to variables? How would I increase score if it's called in another function?
__________________
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-21-2018 , 14:13   Re: [Off-Topic] C++ Game
Reply With Quote #13

That's exactly what I was explaining. This is not a C++ thing, you should always try to keep your variables in the smallest possible scope.
When using pawn for amxx scripting you are working with things called from the outside, so the simplest solution is to use globals to pass data around function calls.

But take a look at this:
PHP Code:
new bool:IsServerLinux

public plugin_init()
{
      
IsServerLinux is_linux_server()
      
printOSMessage()
}

printOSMessage()
{
    
server_print("%d"IsServerLinux)

Did you need a global here? Not really, you can just add a parameter to printOSMessage:
PHP Code:

public plugin_init()
{
      
printOSMessageis_linux_server())
}

printOSMessage(bool:isLinux)
{
    
server_print("%d"bool:isLinux)

Of course you could say "why not simply call is_linux_server" in printOSMessage. It's just an example to illustrate the concept, clearly not the best one, but good enough.
printOSMessage is a function you created, therefore you have full control over it's structure.

Now think about RegisterHam(Ham_Spawn, "player", "CBasePlayer_Spawn). What do I do if I want to access some data besides player id? Can I simply add one more parameter? No, because the function is called from hamsandwich module with a clear header(only one parameter - the player id).
I also can not extend CBasePlayer class in pawn to add more members, so the simplest solution is to use global variables.

But why not always using globals? You can google "should I use globals" or "why are globals bad" or something around this lines, you will find tons of arguments(against or pro), but the main idea is that a global is visible everywhere, meaning in a relatively big codebase on which multiple people are working, one can screw things badly while using globals.

Of course, your code is small, there's very little chance you are going to run into issues because of globals, but I think it's good to know that in production code globals are usually banned and to basically be aware that using globals for everything is not the best idea.

Quote:
How would I increase score if it's called in another function?
You could have a player class which holds score, turns, name, whatever and pass around a player object to each function, then all the variables would be grouped and you could modify them.
But your game is small, you may end up with a class with only one field, which is not great.

Then what do you do? As I said in my previous replies, pass parameters from function to function(using references or pointers).

A concrete example:
Define your "Score" variable inside main(). Then let's say you want to modify the score in a function called CalculateScore. Then all you are going to do is call CalculateScore(Score).
And CalculateScore should be declared as CalculateScore(int &Score). What &Score does is to create a reference to the "Score" variable from main(the variable is not going to be passed by value - making a copy), but when you will modify Score inside CalculateScore it will modify directly the variable from main.

On the other hand, it would be okay to keep
PHP Code:
int MapX 20;
int MapY 20
in global scope if this are meant to be constants(but they should be declared as constexpr).
__________________

Last edited by HamletEagle; 11-21-2018 at 15:03.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-21-2018 , 15:45   Re: [Off-Topic] C++ Game
Reply With Quote #14

How exactly do we call these external functions (SetScore)
I tried doing this but it was wrong
PHP Code:
void SetScore(int &Scoreint value)
{
    
Score value;
}

SetScoreScorevalue ); 
__________________

Last edited by edon1337; 11-21-2018 at 15:51.
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-22-2018 , 02:20   Re: [Off-Topic] C++ Game
Reply With Quote #15

It is just a simple function. Call it like you call anything else.
__________________
HamletEagle is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-22-2018 , 04:28   Re: [Off-Topic] C++ Game
Reply With Quote #16

A nice way would be to have a GameState class that will contain all important game state - gameOver, score, fruitX/Y, snakeX/Y etc, and appropriate methods to change the state as needed. You can initialize an instance of that class in main() and pass it down by reference to functions that need to read(accept const GameState&) or mutate (accept GameState&) state.
__________________

Last edited by klippy; 11-22-2018 at 04:32.
klippy is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-22-2018 , 08:10   Re: [Off-Topic] C++ Game
Reply With Quote #17

Quote:
Originally Posted by HamletEagle View Post
It is just a simple function. Call it like you call anything else.
How?

PHP Code:
void SetScore(int &Scoreint value)
{
    
Score value;
}

void Setup( )
{
    
srandtime ) );

    
SnakeX MapX 2;
    
SnakeY MapY 2;

    
RandomizeFruit( );

    
SetScore(Score,0);
}

int main()
{
    
ShowConsoleCursor(false);

    
int Score;
    
SetScore(Score0);

    
Setup();
    
DrawMap();
    while (!
gameOver)
    {
        if (
Input() == 1)
        {
            
Logic();
            
DrawMap();
            
Sleep(1);
        }
    }

    return 
0;

Code:
SetScore(Score,0);

How do I call it when Score doesn't even exist in that function?
__________________
edon1337 is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 11-22-2018 , 08:27   Re: [Off-Topic] C++ Game
Reply With Quote #18

Why is it so hard for you to grasp the concent of passing it as an argument in all functions where you need it? Pass Score to Setup. SetScore was just an example.
__________________

Last edited by HamletEagle; 11-22-2018 at 08:30.
HamletEagle is offline
edon1337
Penguin Enthusiast
Join Date: Jun 2016
Location: Macedonia
Old 11-22-2018 , 09:53   Re: [Off-Topic] C++ Game
Reply With Quote #19

Quote:
Originally Posted by HamletEagle View Post
Why is it so hard for you to grasp the concent of passing it as an argument in all functions where you need it? Pass Score to Setup. SetScore was just an example.
I still don't get this part, if I create 'Score' in Setup(), how am I supposed to be retrieving Score's value in DrawMap()?

PHP Code:
void SetScore(int &Scoreint value)
{
    
Score value;
}

void Setup( )
{
    
srandtime ) );

    
int Score;

    
SnakeX MapX 2;
    
SnakeY MapY 2;

    
RandomizeFruit( );

    
SetScoreScore);

Code:
void DrawMap( ) {     system( "cls" );     int x, y, spaces;     for( y = 0; y < MapY; y++ )     {         if( ( y == 0 || y == MapY - 1 ) ) // first or last row         {             for( x = 0; x <= MapX; x++ ) // print X 10 times             {                 cout << "#";             }         }         else // 1-9 row         {             cout << "#"; // # which goes to the first space of the line             spaces = 0;             while( spaces != MapX - 1 ) // keep putting spaces till we reach the end of the line - 1             {                 if( spaces == SnakeX && y == SnakeY )                 {                     cout << "O";                 }                 else if( spaces == FruitX && y == FruitY )                 {                     cout << "F";                 }                 else                 {                     cout << " ";                 }                 spaces++;             }             cout << "#"; // finally print # at the end of the line         }         cout << endl;     }
    cout << "Score: " << Score;
}
__________________

Last edited by edon1337; 11-22-2018 at 09:53.
edon1337 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-22-2018 , 10:42   Re: [Off-Topic] C++ Game
Reply With Quote #20

You initialize it in main() and pass it to both Setup() and DrawMap(). You pass it to any function that needs to access it.
__________________

Last edited by klippy; 11-22-2018 at 10:45.
klippy is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:15.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode