Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] Is this effect assigning method satisfactory?

This topic is 2705 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This question probably stems from a lack of confidence in my own programming ability but I was wondering if there's an alternative to the following scenario?

I have a variety of different types of deferred lights, each light is drawn with its own effect which doesn't change for each light instance of a particular type. Therefore I have assigned the effect for each light type as follows:



public class DeferredDirectionalLight : BaseLight, IDirectionalLight
{
protected Vector3 target;

private static Effect effect;

public DeferredDirectionalLight(Game1 game, Vector3 position, Vector3 target, Color color, float intensity, bool castShadow)
: base(game, position, color, intensity, castShadow)
{
this.target = target;

if (effect == null)
{
effect = game.Content.Load<Effect>("Effects/Deferred/DeferredDirectionalLight");
}
}


I'm not sure I like the 'static' keyword and the '== null' condition check but I can't think of a decent alternative. Any suggestions?

Share this post


Link to post
Share on other sites
Advertisement
[font="Arial"]Well, having a static constructor could work, but that might cause problems due to the order of intialization. As stated in the linked page: The user has no control on when the static constructor is executed in the program.[/font]
[font="Arial"] [/font]
[font="Arial"]Edit: Ah, nevermind, I just realized your resource loader wouldn't be accessible in a static context...[/font]

Share this post


Link to post
Share on other sites
Personally I would avoid inheritance here and make the effect a Strategy; in this case probably a readonly Effect (or a plain old delegate, since I presume Effect is just a functor) and pass that in as part of the constructor (and then perhaps have Content.Load return a pre-constructed light).

... that said, this is perfectly satisfactory. It does what you need to do.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!