#OOP Self value

1 messages · Page 1 of 1 (latest)

pastel tundra
#

(OOP)

I js wanted to ask about the self value.

So my game is based on module scripts, which are using the Self value as the parameter.

It means that im supposed to get the self value for the every single script, right?

But.. wouldn't the self value just copy itself and be separate from the original SELF for the player?

For example, on the server i say it to get the self value, right? It gets. On another server script i ask it to get the self value again just to make everything work. QUESTION: are those two called self values THE SAME? and will the changes be applied to THEM ALL? (I mean, if the self value is only one then changes will be applied same)

The problem is: i got no clue how the saving of the self value works.

Current state (how i try to save it ONCE in game storage to reuse the same players' selfvalues:

I created a separate module script which behaves like a container for everything inside it (i basically created a table for self values)

Then i created one script on the server side which was getting the self value for every single player that joins the game and store it inside of that table. But it was working only for server, not for the client (i think it relies on the module script context)

So i created the same script but on the local.

And now i don't know if the self value that i got inside of server is basically the self value that is on the local and.... (Gibberish)

Just explain how the hell self value works and stores etc🫰

glass vigilBOT
#

studio** You are now Level 1! **studio

rigid topaz
mystic jewel
#

Lua(u) does not support object-oriented programming in its entirety. The language provides facets for streamlined emulation, of which is self

#

self functionally appears as an implicit parameter. Implicit parameters are parameters that are not explicitly defined in your function signature. Implicit parameters behave identically to explicit parameters

#

In order for self to appear as an implicit parameter, a function attached to a table must be declared in the following manner:

function Table:Function()
#

When a function is defined this way, the first argument passed to the function will become value of self

#

For example:

local Table = {}


function Table:Function(a, b, c)
    print(self)
    print(a, b, c)
end


Table.Function(1, 2, 3, 4)
1
2    3    4
#

When it comes to OOP, Table:Function is intended to be called as Table:Function, not Table.Function. This is because Table:Function is a special form of syntax (known as "syntax sugar") that aids in the proper assignment of self

#
Table:Function(1, 2, 3)

Gets redefined as:

Table.Function(Table, 1, 2, 3)
#

self now becomes a reference to Table, or more precisely, the table the function was called from

#

In OOP, the function is called from the object, making self equal to the object

#

The function called from the object isn't actually a part of the object; it is a part of the object's class. The function can be called from the object due to a link established through the __index metamethod

#

The function knows how to interact with the class' objects, but it must know which object to interact with. This is why self must be assigned to the object the function must act on at that moment

#

The execution flow is as follows:

local object = Class.new()
object:Method(...)

Syntax sugar ->

object.Method(object, ...)

Class link ->

class.Method(object, ...)
#

Example:

local Person = {}
Person.__index = Person


function Person.new(name)
    local self = setmetatable({}, Person)

    self.Name = name

    return self
end

function Person:Greet()
    print("Hello, my name is " .. self.Name .. "!")
end


local john = Person.new("John Doe")
local jane = Person.new("Jane Doe")

john:Greet()
jane:Greet()
Hello, my name is John Doe!
Hello, my name is Jane Doe!
pastel tundra
pastel tundra
# mystic jewel Example: ```lua local Person = {} Person.__index = Person function Person.new(...

Thanks for the explanation of how the OOP in roblox works, but I'm more interested in a separate case.

So in my situation, i call a function (can't show the script rn, will drop it later) Character.new() which basically creates a new self right?

So that function is located in different local and server scripts. And i wonder:

is the self object being same in all the scripts (basically, will my changes apply in all scripts for the called self, if in different scripts i (i think so) create another copy of self for one single object (like, the Character.new() in every script for one single object)?)

I just don't know, will the system create another copy of self, if i call character.new() in different scripts for ONE SINGLE OBJECT.

Like, for example in one script i create Triangle.new() and say self.Angle = 90, right?

And i say:

local self = Triangle.new()

In another script i say the same:

local self = Triangle.new()

Then*, if i decide to change some value* of this SELF in **FIRST **script, will it be applied to SECOND script? Thats what i ask about, because i'm worrying about the desynchronization of selfs for a player, which may cause a huge problems for my game

In other words (please forgive me): Does everything link to one object and does all the properties add into one single SELF, if i call the character.new() for one single object in different scripts

mystic jewel
#

It should never be used to refer to a instance of a class created with its .new constructor

#

A new, separate instance of the class is created each time its .new constructor is called

pastel tundra
#

р

#

oh

mystic jewel
#

If you cache the object in a module and access that object across different scripts, you will be interacting with the same object

#

Objects do not replicate across the network

pastel tundra
#

so i will have desynchronization with my scripts right..

pastel tundra
#

When i try to create a self for every new player that joins the game, i store it into a table inside of module script

#

but

#

it only seems to exist on server side

#

not on the client side

mystic jewel
#

It's no different from a variable

#

Writing

local apples = 5

On the server and the client does not automatically link both variables to one another

pastel tundra
#

so.. How do i do then? Do i need to like create two variables on server side and client side and try to say them that those selfs are linked to one object?

#

just how to avoid desynchronization

#

on server and client sides

mystic jewel
#

You need centralization. You will have either a server-sided and client-sided implementation of your class, or one implementation that adjusts dynamically based on RunService:IsServer calls. I recommend the former

#

You will create a service that creates and synchronizes these objects across the network

#

The service will be used to retrieve the cached objects, keeping everything abstracted and localized to the appropriate system

#

You can set up your own replication pipeline, or use a third party tool like ReplicaService

pastel tundra
#

Ok, i'll try to figure out how to make such service

#

Thanks

#

I'll drop here some attempts to make it in the future just to make sure that it would be good from your opinion

pastel tundra
#

I don't know if i could use this topic to communicate with you but I'll try.

(offtopic)

I got no clue why, but when i changed the dot before "BindToCharacter" method on colon, it just started working.

Also, it seems that my selfs for character are not having enough time to load up before other scripts start executing so i have to put task.wait(0.04) before it 😭🙏

glass vigilBOT
#

studio** You are now Level 2! **studio

pastel tundra
#

(task.wait(0.04) before the local script that uses the self)

rigid topaz
pastel tundra
#

fr

#

if it works don't touch it, no matter how it works

rigid topaz
#

I wouldn't recommend it though

glass vigilBOT
#

studio** You are now Level 11! **studio

rigid topaz
#

it's brittle

pastel tundra
#

well at least i got not clue how to make it work another way

mystic jewel