Discord
Login
Community
DARK THEME

Objects and Classes - What exactly *is* an Object and when to use it?

Hey!

I discovered microStudio just a few days ago when looking for a GameEngine that can be used in the Browser. Immediately fell in love with that thing, it seems to be so "tiny" but it is insanely capable of doing stuff and I love how it's kinda low-level, this leaves so much freedom to all the different approaches of code architecture :D

However, I have to admit that i never used any Lua(-like) programming languages before (i'm assuming microScript is mainly Lua based, in terms of it's syntax and features?!) and, as the title probably states, have no idea about the why/when for the "object" type.

I'm a Unity/C# developer for several years and I generally tried a few different programming languages, but my main experience is within OOP, so my kinda natural intuition after my first tinkering with microStudio was to just use classes for everything because that's what i'm used to.

Can somebody give me a little input? :)

Hi there :)

I am by no way an expert in this, just didn't want to let you hang there without an answer (I'm an OOP or whatever you call it newbie). There are quite a few discussion about this on the discord, might be worth while to join it to have a snoop.

Here an extract of one of those:


Gilles: I confirm, @Abr00 is right on this! A class in microScript is just an object. You can explore it like an object. You can change it ; you can create an instance of any object.
Sebastian: So there's no difference between:

foo = class
  constructor = function()
    this.bar = "lorem"
  end
end

instance = new foo()

and

foo = object
  constructor = function()
    this.bar = "lorem"
  end  
end

instance = new foo()

Because this is how I understood The Class and Object keywords are interchangeable in Microscript.

Gilles : that's absolutely right ; only thing you won't get if you don't use the keyword class, is inheritance through the keyword extend but you can make A extend B by just setting A.class = B

Sebastian : My Object Oriented religion forbids this 😜

Gilles : think that this language reflection capability allows your code to create new classes dynamically, at runtime! But yes when it is a class, better call it a class, for readability ...


Personally, if I just need a one off entity I define it as an Object. If I need multiple similar ones I use a Class definition so that I can extend it when required.

I hope this gives you at least a rough idea :)

Live Long and Tinker

Your post is highly confusing...WOW
lua is high-level language that works well in conjunction with low-level like C for example.
Low-level languages don't have garbage cleaning and dont have dynamic memory sizes and allocation.
You do understand that OOP means Object Oriented Programming right.. keyword beeing Object

Nevermind I will try to explain anyways:
Since you are familliar with classes I will explain on class example

xyz = class
  constructor = function()

  code inside here is object that contain data like x=0 and y=0 

  end
  
  update = function()
  
  here you update that object

  end
  
  draw = function()

  here you have code for drawing it

  end
end

If you do bla = new xyz() and then print(bla.x) you will see x of bla object that was created using your xyz class.
So what is difference between using class and object you usualy define object and parameters for it and then code functions that will read data and change data or even store data to that object so thats it you have made one object and thats it.
But class on the other hand contains all object code with one or multiple functions and can also serve as template for creating, updating, drawing, controling objects.
So it all depends on usecase if you want multiple objects you do template/class but if you need just one then define it yourself and update it by making function dedicated to that or update it thru other functions..

Edit: @ThinkerSmith You were faster.

Thank you @TinkerSmith, that was helpful and answered my question!

@Lucifer, I appreciate the time you've taken to respond to my question, can you tell me what exactly was confusing about it? I don't understand the reminder on low-level and high-level languages in the above context, i was referring to low-level in regards to the features that are within the API when just using microScript, as in it's not totally bloated and just the minimum amount of stuff that is needed to create some higher-level architecture/API around it.

I do know that OOP means Object Oriented Programming, and i stated that my main experience is within that area because i don't consider Lua (and deriving languages) to be object oriented, the languages i'm used to refer to Object as the "foundational" type of almost all other Types that come with the language, and not as a standalone keyword to define new data types :)

thank you for the reminder on classes as well, but as written above, i'm already using classes for almost everything because i do know what they are.. the question was mainly about "object" and the difference between that and a class.

That question definitely did get answered, and seemingly there isn't a real difference between those two. I was wondering how to implement something similar to an enum within microScript, and classes just felt so wrong for this because i associate them with something that's being instanced, i've now used a simple object for this..

@wtch28 well by definition low-level languages grant you massive control over how your code interacts with hardware...
In regards of features yes it's not bloated but not low-level by any means
And yes lua isn't Object Oriented. lua actually came to be as an API for C language...

MicroScript isn't lua based it just uses lua syntax.

Let me explain Microstudio and how languages work here you chose language to write your code in it but that is illusion because microstudio has multiple interpreters so actually MicroScript doesn't get executed but interpreted as javascript that gets executed same goes for python or any other syntax.
interpreter might not be best word to use here but you get the point...

As for classes goes I explained that I will use class as example because you know how classes work...
Also microstudio is trying to be as beginner friendly as possible so that's why object keyword I guess
I'm sorry but I don't have any idea how to make enum in microstudio I guess you can make something out of object data type..
but the best course of action is to ask about enum in discord and as separate question that way maybe @Gilles will reply himself...
I came here from C language and programming Operative Systems (Linux,BSD) and that's why I found your low-level/high-level comparison confusing.

Just end note MicroStudio has learned me how to use objects and classes.
in C I needed to make multidimensional arrays and write handler function in order to use it as some form class and that array will serve as object.

edit: Also forgot to mention this idk it might help I use object type to store data like settings or custom variables to wrap variables up so
I don't end up with 125 global variables. if its settings variable it goes in settings object if its debug goes into debug etc..

"enum" with object data type worked. I was thinking about suggesting an actual enum data type implementation, but decided to first try getting it to work with existing data types, and in my opinion this is more then fine enough! i also made a small experiment with "implementing" Events/EventListeners and that worked pretty good too. I'd say, an enum data type built into microScript is not necessarily needed, but it would be nice to have a "switch" statement :D

something = myEnumName.someName
if something == myEnumName.someName then ... end

myEnumName = object
  someName = "blablabla"
  someOtherName = "blabla"
end

everything you've said makes perfectly sense, thank you for the explanation! and sorry for causing confusion, i agree that using low/high-level in relation to something different then programming languages may have been unnecessary confusing.

There is another thing that i'd like to ask, i don't want to clutter the forum with new threads for every small question i have. Basically, i've noticed that no errors pop up when i pass arguments into a function that, by it's definition, doesn't want any, and i am wondering if it's possible to check for those "unwanted/undefined" arguments?

example:

myFunction("something")
myFunction = function() ... end  // is there a way to retrieve that "something" argument even if no parameters are defined? 

Well natively no microstudio doesn't do that and undefined parameters gets disregarded as noise aka function doesn't need it so it doesn't even bother to check what it is...
I don't know how that will be useful but I guess you can do it like this

myFunction = function(t)
  if not t == 0 then // by default t = 0
    print('error: undefined argument "' + t + '"')
  end
  //rest of code
end

but then you will need to expect some parameter in every function and to have that piece of code in every function as well.
So I guess it will be pain to implement, maybe there is a better way but nothing comes to mind.

Sorry Lucifer ... it does ;)

Functions have a predefined list variable called 'arguments'. (Update 2022-12-19)

So you could do this:

myFunction = function() 
  print(arguments)
end   

Then myFunction("test",1,object x=3 end) would return ["test",1,[object]]
This way you can write functions that might have different parameter counts depending on the situation. You just have to evaluate the arguments list. With the [parameter].type indicator you can even identify their type (as the name obviously suggests...)

There is so much info on the Discord, it's just so hard to find. Maybe we need a knowledge Wiki?

omg @TinkerSmith, thank you so much! I was scared that i'd have to create explicit Events for every kind of parameters :D

@TinkerSmith You are right we need some form of wiki or better documentation..
I know that MS is passion project and there are not many people working on it so doc usually suffer from lack of attention.

I'm using MicroStudio for so long and didn't know that I mean that's maybe kinda good thing:
On one side we relay on each other and that makes great community,
On the other we need to search for answers or ask them because we cannot find answers our-self.
So I guess it is double-edged sword. Might be good thing, might be bad.

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community