• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Danicco

Why are static variables bad?

57 posts in this topic

So how would someone resolve such Sleep trouble ?

By making it part of the system that it is designed for.

Generally you want to make data as object local as you can, for instance if I had an enum I created that defined ordinal directions in a 2d game like north, east, south, etc. Quite a few systems might use that, in that case I might place it into a header file under a namespace that can be included by any parties interested in using that enum. For something like a sleep variable that isn't a constant you would probably want to make it belong to the thing that uses it the most, i.e. your window or engine class or whatever. Then just allow it to be accessed from other parts of code that need to determine what it is or modify it.

Using another example you may create an enum of substates for a state machine, well if only that state machine uses it and any other objects would -have- to include the state machine to ever be interested in using the enum then you could place it in the header file. It's a balance of trying to keep information local to where it is used while not mashing everything together in globals or giant include files. It's actually a bad idea to have a header file like "constants.hpp" or something as well due to the fact that everytime that file changes it will force recompilation in the other files.
0

Share this post


Link to post
Share on other sites

For something like a sleep variable that isn't a constant you would probably want to make it belong to the thing that uses it the most, i.e. your window or engine class or whatever.

 

 

This one seem to be reasonable advice i think. Indeed probably removing it from the globals module (I put it there because I was tending to forgot where it can be so it is easy to find) and defining it into window module - and reference from all the others. This is some improvement.- But I am still not sure if this removes some troubles I eventually see still related

 

(such problems are related maybe to fact that 1-1 'linear' dependencies in code are clear, but when you have such 'accesible spots' you can have uncontrolled many-many 

dependencies and it brings some trouble

 

[but I am not sure if this is just about that, this is I think related not only to system of referencing but to some code flow architecture] 

 

- I "will be must" (Wiill had to ? future neccesity do not know how to write this in english  ) rethink it still more, to find, if there is no way to repairing it more, by some other elaborate design or something)

Edited by fir
0

Share this post


Link to post
Share on other sites

(blank, sorry mistaken click)

Edited by fir
0

Share this post


Link to post
Share on other sites

I'd like to thanks for the replies, I kept thinking of how could I change my code design and I think I got something a little better than before.

 

I didn't really like having static variables in my engine since the game class (exposed to other programmers) could access it as well and potentially break something.

I added a "resourceLoader" so all objects that wants to use the engine resource module must go through, and then it can manages itself and I don't need to have it global static for the code:

 

void Game::SomeFunction()
{
    Image* myImage = new Image();
    myImage->Load("assetName"); //I accessed the static resourceModule here
}

 

Now it is:

 

void Game::SomeFunction()
{
   //Since I'm already inside game, and game has access to engine
   Image* myImage = engineResources.Load<Image>("assetName");
   //If "assetName" hasn't been loaded yet, it'll call "Image->Load()" so I don't have all load functions inside my resource module
   //I want to make objects manage themselves so it's easier to handle
}

 

I still left the original constructor open so Game can build their own objects if needed (such as simple Quad, or loading something from outside the resource Module, though they'd need to manage it themselves).

 

So that's something less that I need to worry about outside access that could mess things up, thanks for the help!

0

Share this post


Link to post
Share on other sites

Static variables can be used. But they must not be used with a lack of knowledge of what static is capable of doing to the results you expect not to have but in fact have gottern.

 

Suppose you made private static int life for all instances of the Monster class, what would happen to the monster when your ship shot a laser at it? Assuming the laser can kill it with one hit and also assuming there are more than one instance of the Monster class in the game winodw, both monsters will in fact be affected with the hit even though you only aim at that one monster.

 

Static variables has its benefits. Suppose when you want to access a method of a class without having creating the object of the class This acts as a convenience.

 

If you find yourself misusing static, this is a clear indication to re-evaluate your code design in terms of object-oriented design.

 

A good example of a Student class using non-static and static variables would be

 

 

public class Student

{

 

private static int unitsToGraduate = 120;

private int name;

private int age;

 

}

Edited by warnexus
0

Share this post


Link to post
Share on other sites

Static variables has its benefits. Suppose when you want to access a method of a class without having creating the object of the class This acts as a convenience.

You want to do so when you have Code unrelated to any class or you're not able to use a non-static way (e. g. because your function does some mathematics and you're not able to extend the basic datatypes). If you're working object oriented you want to use methods and in most cases it's possible to do so. It requires a bit more time to get to a better solution and sometimes you're just using the first solution coming to you're mind.
 

A good example of a Student class using non-static and static variables would be

 

public class Student {
    
    private static int unitsToGraduate = 120;
    private int name;
    private int age;
    
    // [...]
    
}

... assuming there is only a single university or wathever the students are used by in your system. As soon as you have multiple universities with different unitsToGraduate values, you have to move the static variable into the university (regular member) and you should recognize: this value is a universities property.
That's why also this example is at last also not a good example for the usage of static variables.

In general speaking: You won't have to use static variables in most cases. And if you believe there is no other way without a static variable, there is probably still an other way. ;)
0

Share this post


Link to post
Share on other sites

In general speaking: You won't have to use static variables in most cases. And if you believe there is no other way without a static variable, there is probably still an other way. ;)

 

Well you would actually have to use a static. Specifically, you would need to use a static method instead of the static variable to present encapsulation in your code.

 

 

 


As soon as you have multiple universities with different unitsToGraduate values, you have to move the static variable into the university (regular member) and you should recognize: this value is a universities property.

 

Would a programmer really do something like that? Given your scenario, I would actually make the unitsToGraduate an instance variable for each instance of University class.

 

The example I gave was demonstrated by a Stanford University professor.

 

I do agree, it depends on the situation.

Edited by warnexus
0

Share this post


Link to post
Share on other sites

As soon as you have multiple universities with different unitsToGraduate values, you have to move the static variable into the university (regular member) and you should recognize: this value is a universities property.

 
Would a programmer really do something like that? Given your scenario, I would actually make the unitsToGraduate an instance variable for each instance of University class.

Aren't you saying the same thing I did?
public class University {
    private int unitsToGraduate;
    private List<Student> students;
    // no static
    
    [...]
}

public class Student {
    private String name;
    private int age;
    // no static
    
    [...]
}
 

The example I gave was demonstrated by a Stanford University professor.

I'm uncertain about the situation in other countries, but as far as I heard about you should not rely on everything IT professors are telling.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0