Results 1 to 9 of 9

Thread: Trying to avoid a derefernce...maybe

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Trying to avoid a derefernce...maybe

    Every frame a unit needs to acquire the TimeDelta to figure out how much time has passed to act upon their current velocity.

    The timer is wrapped in a class. GetTimeDelta() func.
    So I can either pass the TimeDelta to each unit that needs it each frame.

    Or

    I can create a static pointer in the unit class to the timer class TimeDelta and derefernce it each time it is needed.


    Well, I know dereferencing is laggy, and it could be done as of now at most twice per unit per frame. Usually just to apply to the velocity once.

    Which is the fastest method????
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Twice per frame is basicaly nothing, the difference in speed, if any, will not be noticable. Use whatever is easier for you.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Twice per frame is indeed nothing, but twice per unit per frame is more. Now, dereferencing a pointer is still extremely fast.

    I think you should make the timer class a global singleton. Aside from that, passing the time delta to the move function of the units is also a very good method.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Sorry, missed the 'per unit'. But I think you should use the first rule of optimization here: "Don't do it!".
    I don't think a singleton is a good idea unless you know exacly what you are doing. In most cases a singleton is only used over a global variable because other people say global variables are bad and design paterns are good.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You're mistaken there, twanvl. A singleton is used over a simple global variable because:
    1) You have finer-grained control over the access. E.g. you can prevent accidental assignment.
    2) You have better control over construction and destruction. This is a very hairy topic in C++ for global objects.
    ... and probably some other reasons.

    This is not a question of optimization, anyway. This is a question of application architecture. Yes, one might be faster than the other, but far more important is that it makes sense and is easy to use. The margin is likely trivial, and twanvl is right: it would be premature optimization, and thus a very bad thing.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    I don't see why you would need any of the features of a singleton here, a global timer class would work just as well. It is not that a singleton has no use, just that it is the most overused design patern. I have been guilty myself of using singletons when a global variable would work too.

  7. #7

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339
    Now, I tried to go some research on Singletons. So far I have found it to be declared like ClassName * ClassName::SINGLETON
    But I cannot find a drop of information as to what it actually is.

    So please an explanation would be loved, as it may help to ease my situation.

    I am rusty on using global vars, as far as I know I have a problem declaring them and then have any class use them. My header/implimentation seem to not understand of its existance.

    Well the chances of a unit needing the derefence the TimeDelta more than once even is iffy. But it will be forsure once a frame per unit.
    So I doubt I will design a game which that would become a problem, but who knows, I aint going to let an obviouse laggy spot escape.

    So I guess my options are to pass timeDelta each frame when the units update function is called from the gameEngine.

    Or

    Make the timer global and simply acsess the TimeDelta each frame.
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  8. #8
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    I aint going to let an obviouse laggy spot escape.
    You don't know whether it will be laggy. If you get to a point where your program is running slow then use a profiler, it will tell you exactly what the cirtical parts of your program are, then you can optimize those parts. If you jump at every optertunity for micro optimization your program will never get finished and the code will become a nightmere to maintain.

    Edit: a nice article about singletons

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Singleton is a design pattern. The Singleton Pattern describes a way of getting a single instance of a class for the complete application, like a global, but with several distinct advantages and without most of the disadvantages. Effectively, a singleton object is a global on steroids.

    There are various ways to go about implementing them, but a good and easy one would be to use the facilities already provided in the Loki library:
    http://www.moderncppdesign.com/
    http://sourceforge.net/projects/loki-lib/


    However, the last paragraph of the article twanvl linked is very relevant. I think you should have the timer in your main game loop and pass the delta down through the functions.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width