Sign in to follow this  

[C#] Is this effect assigning method satisfactory?

This topic is 2491 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:

[code]

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");
}
}
[/code]

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
[font="Arial"][size="2"]Well, having a [url="http://msdn.microsoft.com/en-us/library/k9x6w0hc(v=vs.80).aspx"]static constructor[/url] 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.[/size][/font]
[font="Arial"] [/font]
[font="Arial"][size="2"][i]Edit:[/i] Ah, nevermind, I just realized your resource loader wouldn't be accessible in a static context...[/size][/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

This topic is 2491 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.

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