#Can someone teach me OOP (Object orientated programming)

1 messages · Page 1 of 1 (latest)

opal moss
#

i need to know what OOP is bc people say it can make u actually be able to script

dire yacht
#

you need to know how to script to learn OOPevilcat

void tide
#

Oop is hot garbage forget about it (everyone who says otherwise is coping)

neat vine
#

it is?

dire yacht
#

its one of the most important things to learn wehn you actually want good code

neat vine
#

i dont understand oop very well, i kinda understand that metatables can allow you to call things that may not exist yet but i personally havent found a good use for it yet... i did make a game without any oop i just dont really understand oop enough yet

#

i am currently making a game that contains no oop*

#

solely because i dont understand

dire yacht
#

OOP is kinda self expainable "Object Orientated Programming"

Heres an example of the use of OOP:

--No OOP

local part = script.Parent

local function killPlayer()
    --bla bla bla get player
    --set player health to 0
end

part.Touched:Connect(function(hit)
    killPlayer()
end)
--With OOP

local partsFolder = game.Workspace.KillParts:GetChildren()

local function killPlayer()
    --bla bla bla get player
    --set player health to 0
end

for _, part in partsFolder do
    part.Touched:Connect(function(hit)
        killPlayer()
    end)
end
#

we are trying to build our code easily expandable

#

and try to not reuse the same code snippet over and over again

neat vine
#

setting a metatable allows you to set classes with default values for objects even when the objects dont exist yet - something like that

also zyos73 i use that but i didnt know that was OOP????

dire yacht
#

well metatables is another example of OOP

neat vine
#

ohh

#

bruh

#

i been using oop this whole time and never even knew XD

dire yacht
#

you just try to not reuse code snippets over and over again and make code OBJECT ORIENTATED

dire yacht
#

like metatables is are advanced for example

neat vine
#

yeah i dont understand metatables very well myself - but i definitely understand functional coding and looping through folders

dire yacht
neat vine
#

using functions and just calling them whenever i want to do a specific action

neat vine
#

its cuz LUAU does not have traditional 'OOP' like java or c# does

#

so you kinda gotta make it up

dire yacht
#

ig idk I dont code with java or c#

neat vine
#

classes are built into the language - lua does not have classes

dire yacht
#
--!strict
-- @zyos73
-- 07/29/25

--Use: debugger(-1, script, debug.info(1, "l"), nil)

local debug_messages = require(script.debug_messages)
local config = require(script.Parent.config)

return function(code : number, script : Instance, line : number, info : any)
    if not config.global.debug then return end
    
    info = info or "No Info"
    local msg = debug_messages[code]
    
    if not msg then
        warn("\n[DEBUG] \n Message not found for: " .. code)
        warn("\n[DEBUG]", "\nScript:", script, "\nLine:", line, "\nMessage:", msg, "\nInfo:", info, "\n")
        return
    end
    
    print("\n[DEBUG]", "\nScript:", script, "\nLine:", line, "\nMessage:", msg, "\nInfo:", info, "\n")
end

heres another use case for modules you dont want to use this script every time you wanna debug something so you can just require the module and call the variable

dire yacht
neat vine
#

i think so

#

minecraft java is what i was talking about not javascript

#

but ye

#

i think thats right

dire yacht
#

ye i know but i only use javascript for school and luau lol

#

we also use a little C++ in school but I dont rlly like it tbf

neat vine
#

int main {

dire yacht
#

yes sum like thatevilcat

neat vine
#

why is main an integer

dire yacht
#

I got no idea bro

neat vine
#

lol

dire yacht
#

i dont even know if its C++

#
//www.elegoo.com
//2016.12.09
 
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
 
void setup() {
  pinMode(SW_pin, INPUT);
  digitalWrite(SW_pin, HIGH);
  Serial.begin(9600);
}
 
void loop() {
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin));
  Serial.print("\n");
  Serial.print("X-axis: ");
  Serial.print(analogRead(X_pin));
  Serial.print("\n");
  Serial.print("Y-axis: ");
  Serial.println(analogRead(Y_pin));
  Serial.print("\n\n");
  delay(500);
}
#

idk what this is

neat vine
#

yes its definitely C something

dire yacht
#

ye

#

something C

neat vine
#

void means return nothing

supple apex
# opal moss i need to know what OOP is bc people say it can make u actually be able to scrip...

OOP wont help you script, youre going to have learn scripting first before moving onto OOP, but OOP can help in cases of game/engine development whenever you do decide to tackle it!

now.. bare with me as im very bad at c++ but here is an example dance

struct Sprite
{
    SDL_Texture* SpriteTexture;
    float x,y,w,h;
    
    Sprite(SDL_Renderer* renderer, const char* SpriteLocation, int Nx, int Ny, int Nw, int Nh);
    void render(SDL_Renderer* renderer);
    void editTransform(float Nx, float Ny, float Nw, float Nh);
    void editTransformWithVector2(Vector2 vector);
    Vector2 GetVector2();
};

this is an example of an object base! it has in-game position variables like x,y,w,h aswell as functions.

also for the record just know that structs and classes are pretty much the same except for the exception of public and private categories for variables or functions, classes aren't needed for my environment but they work similarly!

now these functions only affect the specific struct you call them from, for example:

    Sprite MoveToolSelectedSprite(renderer, "Textures/MoveToolSelected.png",0,0,50,50);

here we define the object!

MoveToolSelectedSprite.render(renderer);

and here we render that specific object! smile

you can have as many "objects" you want then edit their attributes individually.

and thats about it. happy

neat vine
supple apex
neat vine