question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add events that fire before components are activated

See original GitHub issue

Summary

In order to integrate Singularity with duality I would like the dependencies to be injected before components are activated. Currently duality does not provide a event for this.

Iam currently using Scene.ComponentAdded and Scene.Entered for this purpose except for the fact they fire after component activation.

Analysis

  • Can be used to do quite neat stuff such as IOC
  • Like all things can also be abused.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
Barsonaxcommented, Oct 29, 2018

Ah ok makes sense. Not sure how you could word it better than what you did.

Before we start adding events all over the place let me clarify why I have a need for these events in the first place. Normally with DI constructor injection is preferred. However I do not know if this is possible in duality as duality itself calls these constructors. This is why I went for plan B and wanted to do it through method injection using those events. If you think constructor injection is still possible (maybe I can hook into the component creation logic somehow?) then I would prefer that route ofcourse.

Just FYI so I don’t keep thinking inside this event solution box.

EDIT: Found the logic creating the instances in ObjectCreator and in GameObject.AddComponent. Without engine modifications it doesn’t seem to be possible to put custom logic in here.

EDIT2: If we decide to change the logic here its not needed to change the current GameObject.AddComponent at all. You can make a new extension method that will be used when the type has a constructor with parameters (only works in C#7.3!):

class Program
{
	static void Main(string[] args)
	{
		var gameObject = new GameObject();
		gameObject.AddComponent<Program>();
		gameObject.AddComponent<ClassWithConstructorArguments>();
	}
}

public class ClassWithConstructorArguments
{
	public ClassWithConstructorArguments(int foo)
	{

	}
}

public static class GameObjectExtensions
{
	public static void AddComponent<T>(this GameObject gameObject) where T : class
	{
		//Will handle any types that don't fit the new() constraint
	}
}

public class GameObject
{
	public void AddComponent<T>() where T : class, new()
	{
		//The fast happy path
	}
}
1reaction
Barsonaxcommented, Jul 3, 2018

Ill release Singularity as a v0 version then with the warning this issue is yet to addressed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fire Component Events | Lightning Aura ...
Fire a component event to communicate data to another component. A component event can be handled by the component that fired the event...
Read more >
Creating and triggering events - MDN Web Docs - Mozilla
This article demonstrates how to create and dispatch DOM events. Such events are commonly called synthetic events, as opposed to the events ......
Read more >
Event handling (overview) - Event reference - MDN Web Docs
Events are signals fired inside the browser window that notify of changes in the browser or operating system environment.
Read more >
Dynamically creating JavaScript elements with event ...
This tutorial explores the concepts of events, event listeners, and dynamically creating new JavaScript elements by using document.
Read more >
React onClick function fires on render - javascript
I use a .map() function to display my list of objects(like in the example given in react tutorial page), but the button in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found