#Understanding interfaces

1 messages · Page 1 of 1 (latest)

ocean quest
#

Hi so I've came from C so I'm completely new to this class/OOP stuff.

I've been testing out very basic stuff in unity, right now I'm currently implementing sitting on a chair. But I realized the way I implemented it can not be scaled unless i do some Yandere Dev stuff. So, I've been researching and looked into interfaces. But before I actually jump into interfaces, I want to understand how they work so I'm not just blindly using them.

This is what I have gathered but idk if its correct:

  • Interfaces are essentially a universal file that any c# file within the project can interact with and use if they want

  • If a c# script includes said interface in their script (like here: public class playerMovement : MonoBehaviour, IInteractable), it must use the methods within that interface? so if i have for example, open(), close() and use() inside IInteractable, playerMovement must have a function/method called open(), close() and use(), assuming playerMovement is using the IInteractable interface

  • If a script does not use all of methods mentioned within the interface, the compiler will not compile

My brain has decided the following analogy is correct:

  • A interface is essentially a psuedo header file in C, if header files worked in a way where you didn't have to include, header files were just universal, and the function inside the header file isnt "linked" to a function within the C file, it just looks for the function in whatever file/script you have referenced

but i've been told this analogy is wrong. I'm wondering if someone can help me conceptualize in a way i actually understand how these things work. Thanks

meager crystal
#

Interfaces are essentially a universal file that any c# file within the project can interact with and use if they want
on this previously unmentioned point - technically correct (other than them being files), but this isn't specific to interfaces, all types in c# have this

ocean quest
#

im asking about interfaces

#

i will get to other files at a later time

meager crystal
#

and my answer was about interfaces

ocean quest
#

ur answer is confusing and also misguides my brain dude

#

idk what to tell u

meager crystal
#

Interfaces are essentially universal that any c# file within the project can interact with and use if they want
Classes are essentially universal that any c# file within the project can interact with and use if they want
Enums are essentially universal that any c# file within the project can interact with and use if they want
Attributes are essentially universal that any c# file within the project can interact with and use if they want

i'm saying it isn't an interface thing

ocean quest
#

like bro

#

i appreciate u tryna help

#

i am just heated rn and ur not helping

#

like i appreciate u

#

its just not getting through

ocean quest
#

like i already know what a enum is

meager crystal
#

"specifically interfaces are universal" is more misleading imo, hence why i mentioned that

teal shadow
#

if you wish to understand what makes an interface unique, you must also understand what is not unique about them

distant crater
#
  1. Interfaces are only accessible by things in the same namespace. In Unity if you don't include a namespace in the script, then it's part of the global namespace. So in that sense it's "universally" available.

  2. Yes, adding IInteractable to your class header means you have to implement those functions in that script

  3. Yes, assuming by "use" you mean have the functions present. You don't have to "use" in the sense that they have to do anything (they can be empty)

  4. If you want to make the connection make sense, then sure, they act similarly in some sense.

teal shadow
#

(i consider the analogy to be tortured, but you can certainly make it)

ocean quest
#

thank you

meager crystal
#

you could say things like "interfaces use {}", "interfaces have names", but these aren't specific to interfaces, so even though it's correct, they aren't super useful

teal shadow
#

trying to tie the concept to something else is making it more difficult to understand

ocean quest
#

if i didnt tie python to C i would have never learned C

#

its just the way i learn

meager crystal
#

we're not saying to not do that

ocean quest
#

either use real world concepts, or concepts in a language i already know

meager crystal
#

but you have to be careful about how you compare what's new to what you know

teal shadow
#

people often say that an interface defines a "contract" – a set of things that you must be able to do

meager crystal
#

taking c++ as an example, it has headers and requirements, and interfaces are far closer to requirements than headers
headers are the closest thing c has, sure. but it's not very close at all

teal shadow
#

perhaps that is a useful metaphor here

ocean quest
#

if i knew solidity or something then contract would have clicked

#

but ive done minimal solidity

meager crystal
#

point being, c# doesn't care about files (but unity does)

teal shadow
ocean quest
#

i get it now i think

teal shadow
#
public interface IDamageable {
  public void Hurt(float amount);
}

public class Enemy : MonoBehaviour, IDamageable {
  private float health;

  public void Hurt(float amount) {
    health -= amount;
  }
}

e.g.

#

A useful consequence of this is that Enemy is an IDamageable -- you can store it in a variable of that type

#

so, perhaps, your code could do this

ocean quest
#

it essentially allows the function to do multiple things so you dont have a static function

teal shadow
#

yep, that's a core pillar of object-oriented programming

ocean quest
#

and can still be called by other scripts

teal shadow
#

Polymorphism means that the exact code that runs is determined at runtime

#

If you give me an IDamageable, it could be an Enemy or a Prop or a Player

ocean quest
#

i totally get it now

#

ty

meager crystal
#

also, i'd like to mention again - implementing an interface means you have to define methods, not use them
using methods refers to calling them, the opposite side of the interaction

teal shadow
#

indeed

meager crystal
#

thing implements interface -> thing must define [methods], caller can use [methods] of thing

ocean quest
#

yea i get it, the script has to have the function, but it doesnt necessarily have to ever be called

#

u could even just pass i guess

#

idk if pass exists in c#

meager crystal
#

pass in python is only necessary because of whitespace being used as scope

#

all the c-family languages can just use empty blocks

teal shadow
#

indeed

#

the closest thing to pass in C# would be an empty statement

#

for example, this is perfectly legal

#
if (true);
#

this is equivalent to

if True:
  pass
meager crystal
#

while(1); my beloved (for freezing the browser, not c#)

teal shadow
ocean quest
#

so my interface literally just looks like this?

#

not wrapped in a class or anything?

teal shadow
#

Correct.

meager crystal
#

interfaces exist on the same level as classes

teal shadow
#

You can nest type declarations inside of each other

teal shadow
#
public class Foo {
  public class Bar {
  
  }
}
meager crystal
#

collectively called "types"

worthy compass
teal shadow
#

i promise I can do XYZ

ocean quest
#

if you wrapped a interface inside a class, would only the class inside be able to interact with the interface?

meager crystal
#

depends on the access level

ocean quest
#

like if u want a universal interface it gets declared outside the class

meager crystal
#

same as with fields or props or methods - if it's marked public, anything can use it

teal shadow
#

This will affect how you identify the type

#
public class Foo {
  public interface Bar {
 
  }
}

The resulting interface type is named Foo.Bar

ocean quest
#

hm im kinda getting the whole oop thing, so private would be like if u have multiple functions the same name?

teal shadow
meager crystal
#

nah, just having them nested in different places solves name collisions

teal shadow
#

Access modifiers allow you to restrict which parts of your code third parties interact with

ocean quest
#

so public/private is just coding practice, it doesnt necessarily help with the flow of code

teal shadow
#

Right; you could make everything public with minimal issues

worthy compass
#

(also access modifiers do not really exist at runtime, it's purely a way to manage what things should touch other things as a developer writing code)

teal shadow
#

You could start experiencing name collisions that didn't used to occur

meager crystal
#

access modifiers just control access - who can and can't use this member, for abstraction and consistency reasons

e.g.
(consistency) some method might only make sense in a specific circumstance in the middle of other operations, it wouldn't make sense for other things to call it - that would be private
(abstraction) some interface or class might only be used as an implementation detail, and is wholly irrelevant outside the class - that would be private

teal shadow
#

e.g. if a library declares Foo in the global namespace, and you also have a Foo

#

if they switch it from internal to public, you'll suddenly have a name conflict

#

This rarely comes up

#

notably, C# does not support "top level" functions

#

you can't just declare a function out in the open

#

it must be part of a type

ocean quest
#

i get it

#

you guys are actually good helpers

#

my bad for getting heated

#

i get annoyed if i cant figure out something within 5 mins

#

feel like an idiot

#

and interfaces took like 2 hours total of figuring out so

teal shadow
#

it's alright; i think i was too quick to get frustrated too

#

glad you could figure it out (:

meager crystal
#

(to be extremely pedantic, in modern c#, you can write functions outside classes, but only in the case of "top-level statements", where these are automatically wrapped in a class+method - so they can be written "outside", but they still can't exist on their own. you won't ever need this in unity though)

ocean quest
ocean quest
#

is there even a default class in regular c#

#

i guess main?

meager crystal
#

they get wrapped in something like

class Main {
  static void Main() {
    /* top-level statements */
  }
}
```which would be the typical entrypoint for c# programs
#

unity doesn't have this since it has its own entrypoints

#

it's not really "the default class", just a valid main class

#

c# and java use that static void Main() (different spelling in java) as the entrypoint, regardless of the class, it's the equivalent to c's int main

teal shadow
#

yeah, you always need an "entrypoint" if you're starting a program up

#

You aren't responsible for that when you're writing code for a Unity game

#

the native side of Unity has already started up the C# runtime

#

if you ignore things like rendering, Unity's only real job is to call your code when appropriate (e.g. running Update once per frame)

#

(we are ignoring a lot there 😉 )

meager crystal
#

really though if you have something for rendering, something to call update, and perhaps some primitive physics, that's a viable "game engine" right there lmao.
(did just that for a uni project in javafx)

teal shadow
#

oh yeah, one thing that's going to look weird if you're new to Unity

#

you might have noticed that things like the Update method can be private

#

and that you aren't implementing an "Updatable" interface

#

Unity is using reflection to directly find and execute your methods

#

This was very confusing to me at first

#

similarly, there is absolutely nothing wrong with doing something like this

#
public class Foo : MonoBehaviour {
  private void Start() {
    Update();
  }

  private void Update() {
    Debug.Log("Hello");
  }
}
#

there is nothing magical about Update; calling it doesn't have any effects on the game engine

teal shadow
#

you can see a bunch of them here

meager crystal
#

the "magical" part is the name, because unity looks for that method by name (through reflection)
(well, the entire signature)

teal shadow
#

yeah

#

there is no guarantee that you can actually call the OnTriggerEnter method on a MonoBehaviour

#

if a method does exist, Unity will grab it and call it

#

if the engine were designed today, I'm pretty sure these would all be parts of interfaces

ocean quest
#

i thought of another analogy when i was in the shower

#

a interface is essentially like a function struct

#

and instead of initializing the struct, you call it in the namespace or whatever its called

meager crystal
#

kinda lost on what you're you're referring to in each part there

teal shadow
#

maybe you're thinking of function pointers?

hazy folio
#

I have to question why ask about such a core OO topic here instead of reading documentation

ocean quest
#

idk, maybe because everyone has their own way of learning?

#

becuase reading documentation works for you doesnt mean it works for everyone else

#

not sure why you had to chime in anyway, the question was answered lol

hazy folio
#

I see many things here and there was much discussion so I found it odd

ocean quest
#

i mean theres nothing wrong with finding it odd

#

just unnecessary to comment on it is all

#

the question was answered

#

humans are weird

hazy folio
#

We can both do what we did and all is well

ocean quest
#

except yours was completely unnecessary and provided absolutely no extra info

#

was literally just to get your own rocks off

#

lol

meager crystal
#

it's obnoxious

ocean quest
#

the comment was just unnecessary

#

the guy typed just to type

#

its a common problem with humans

#

imo its obnoxious to tell someone to read the docs, imagine if a teacher told you every time you asked for help to just go read the docs

#

anyone who has a different path of learning would learn nothing

#

especially without knowing if ive already read the docs or not (i spent like an hour figuring it out myself before coming into this discord, without you guys guiding me i would have been clueless)

meager crystal
meager crystal
ocean quest
#

but its unnecessary communication, like all communication is technically communication

#

yeah but the convo was done, he just wanted to say his 2 pieces

meager crystal
#

all communication is technically unnessary communication

#

what's your point

ocean quest
#

its not though

#

telling someone their loved one is dying is not unnecessary communicatoin

#

its quite literally the opposite

meager crystal
#

definitely unnecessary

#

it's good communication. that doesn't mean it's necessary

ocean quest
#

i mean i guess its subjective, but if we're not being literal and technical, to a societys point of view, its necessary communication

meager crystal
#

the entire concept of community support is based on people being willing to chip in their 2¢

ocean quest
#

you can use the literal definition if you want

#

but in the real world

#

its necessary

meager crystal
#

necessary to society, sure. not necessary overall.

#

i'm using the actual definition

ocean quest
#

😭

meager crystal
#

my 2¢ is you've been quite resistant to receiving advice.

ocean quest
#

idk my friends who are cryptography majors i listen to every advice they give me, i wouldnt know half of what i know without them

meager crystal
#

good for you

ocean quest
#

i think the unity community just has a chip on their shoulder

#

but thats not limited to unity

#

i feel like most dev communities i join do the same thing

meager crystal
#

every interaction i've had with you seems like you have it lmao

ocean quest
#

SDL is fantastic though

#

idk man, i give off the same energy i receive

#

if i perceive someone is being a smartass or tryna belittle me, why would i give respect back

meager crystal
#

you've been looking for people to cater to the answer you expect with no resistance

ocean quest
#

idk i rarely have this issue

#

only rly in this discord

meager crystal
#

half of what you say is self-contradictory goddamn

ocean quest
#

but alot of u guys are kids too so

#

not really

meager crystal
#

that's hilarious

#

what a waste of time