Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Crayz · Jan 06, 2015 at 12:27 AM · inputeventdelegate

C# Delegates, a question

I began researching delegates and events recently to try and improve the versatility of my classes. Considering various classes in my game are going to be needing player input (keyboard or mouse), I've set up delegate events such as OnMouseDown, OnMouseUp, and OnKeyPressed. Each event passes an object containing data for the input (screen position of click, world position, targeted unit, etc), which gives me super easy access to a flexible player input from anywhere.

While it seems effective and clean, I'm curious if there's any downfalls to it or perhaps any better methods?

Also another question, if anybody is experienced in Garry's Mod Lua, you're able to hook into various events such as : http://wiki.garrysmod.com/page/ENTITY/OnTakeDamage

And modify the parameters of the event (CTakeDamageInfo) before its passed to the entity taking damage. Is this possibly the work of delegate events on a lower level?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiwasi · Jan 06, 2015 at 02:11 AM 1
Share

You do realise you are describing what Unity already did in the EventSystem introduced in 4.6. Its a great idea, that's why Unity did it. But you could have saved yourself some work and used the existing solution.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KMKxJOEY1 · Jan 06, 2015 at 01:15 AM

Personally, instead of delegates I prefer to use abstraction or just plain virtual overriding. This is just stylistic personal preference though, as I haven't noticed any efficiency differences between the 2 methods.

Pertaining to your other question, could you be a bit more specific (maybe an example)? I have dabbled in Lua, but not anything to do with G-Mod.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Crayz · Jan 06, 2015 at 01:43 AM 0
Share

Hmm, scripting in Garry's $$anonymous$$od you're able to hook into various events such as when an entity takes damage. ENTITY:OnTakeDamage( CDamageInfo dmginfo )

Before the Entity actually receives damage it's possible to take the CDamageInfo object and modify its values to your desire, such as CDamageInfo:SetDamage(1) so that the Entity takes only 1 damage, even though the original event may have called for 20 damage.

Back to delegates, I'm not sure if it could work, but it seems like I would be able to hook into my delegate events (somehow) and read or write parameters in a similar fashion to Garry's $$anonymous$$od.

 public struct $$anonymous$$eyboardState
 {
     public char $$anonymous$$ey;
     public DateTime TimeSent;
     // etc
 }
 
 public delegate void $$anonymous$$eyboardInputHandler($$anonymous$$eyboardState state);
 
 public static event $$anonymous$$eyboardInputHandler on$$anonymous$$eyPressed;
 
 public static void On$$anonymous$$eyPressed($$anonymous$$eyboardState state)
 {
     if(on$$anonymous$$eyPressed != null)
     {
         // If the player pressed "k" state.$$anonymous$$ey's original value is "k"
         // Hook in right here somehow and if I want, I can set the value of
         // state.$$anonymous$$ey to anything, for example, "P"
         on$$anonymous$$eyPressed(state);
     }
 }
 
 public class SomeOtherClass
 {
     void Start()
     {
         InputEvents.on$$anonymous$$eyPressed += this.On$$anonymous$$eyPressed;
     }
     
     public void On$$anonymous$$eyPressed($$anonymous$$eyboardState state)
     {
         Debug.Log (state.$$anonymous$$ey);
         // Because I hooked into and modified state earlier, output should be "P"
     }
 }


edit: This functionality wouldn't be entirely useful for me on the back-end, more out of curiosity and ideas of how I might be able to implement community modding

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

27 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to unsubscribe from InputSystem event properly 0 Answers

How to unsubscribe from InputSystem event properly 1 Answer

Create delegate from MethodInfo 1 Answer

What part of the delegate gets passed back?The return type? 1 Answer

Prevent fire event from all instances. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges