Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


#Actualkunos

Posted 27 April 2013 - 03:10 PM

And, yes, it HAS to be global. That's just plain retarded.

 

No it HAS NOT. You are missing the entire point of programming in any language above assembler: encapsulation. Even by keeping a procedural approach to the problem, wanna see how the global goes away? Here it is:

 

int main()

{

vector<Player*> players;

 

// CREATE 10 players

for (int i=0;i<10;i++)

{

players.push_back(new Player());
}

 

// GAME LOOP

 

while (true)

{

updatePlayers(players);

render(players);

}

 

// clean up yada yada

 

return 0;

}

 

void updatePlayers(vector<Player*>& players)

{

for (auto player : players)

{

// UPDATE THE PLAYER
}
}

 

there you go.. players is not global anymore and only functions that REQUIRE access to it receive it as a parameter.

This gives you the huge advantage that now you know which subset of your code can access to players.. and when your code reaches 100k lines of code possibly multithreaded, trust me, you'll be glad to be limited to 3-4 functions instead of the EVERYWHERE you get from a global.


#1kunos

Posted 27 April 2013 - 03:09 PM

And, yes, it HAS to be global. That's just plain retarded.

 

No it HAS NOT. You are missing the entire point of programming in any language above assembler: encapsulation. Even by keeping a procedural approach to the problem, wanna see how the global goes away? Here it is:

 

int main()

{

vector<Player*> players;

 

// CREATE 10 players

for (int i=0;i<10;i++)

{

players.push_back(new Player());
}

 

// GAME LOOP

 

while (true)

{

updatePlayers(players);

render(players);

}

 

// clean up yada yada

 

return 0;

}

 

void updatePlayers(vector<Player*>& players)

{

for (auto player : players)

{

// UPDATE THE PLAYER
}
}

 

there you go.. players is not global anymore and only functions that REQUIRE access to it receive it as a parameter.

This gives you the huge advantage that now you know which subset of your code can access to players.. and when your code reaches 100k lines of code, trust me, you'll be glad to be limited to 3-4 functions instead of the EVERYWHERE you get from a global.


PARTNERS