"Pointing" to objects?

So heres my dilemma. Why clone and then change and then insert a value when i can just “point” to it in a sense. Say I have this

local x = 0
local xPointer = x:GetMemoryAddress()
xPointer = 5;
print(x)
print(xPointer:UnPoint)
--returns 5 and 5

This would fix issues that would happenw when just doing it like this

local x = 0
y=x
y=5
print(x)
print(y)
--returns 0 and 5, bad!

I know the concept of pointers and know how to code in them in C so if there is like some secret assert() function that i dont know of pls tell

I am not sure why you would need this. Numbers aren’t objects either. You could probably make use of metatables using __newindex and __index but that seems overly complex for something so trivial.

Lua is a high-level language with automatic garbage collection, so we have no access to memory. I’m afraid there is no secret function for doing that. Perhaps you might be interested in the concepts of passing by value or passing by reference that implement the functions.

1 Like

That would be useful if Roblox really did implemented this, but no you can’t. Numbers aren’t passed by reference since they aren’t garbage collected but manually removed from memory, they’re passed by values.

What you should know is that:

  • Tables and userdatas’ are passed by reference since they are garbage collected.

  • Strings are also passed by reference since they are also garbage collected, same goes for functions. Also, strings are immutable.

local x = "a"
local b = x --> passed by reference but since strings are immutable, the value of x is copied to b
local x = 5
local a = x --> not a pointer to x, the value is copied