Forum

> > CS2D > Scripts > What are rawset, rawget?
Forums overviewCS2D overview Scripts overviewLog in to reply

English What are rawset, rawget?

1 reply
To the start Previous 1 Next To the start

old Re: What are rawset, rawget?

MikuAuahDark
User Off Offline

Quote
It's used to respectively set and get table fields without invoking metamethods. Mind you, metatables and metamethods are advanced Lua stuff.

This code demonstrate it pretty well
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- rawget demonstration
local t = setmetatable({value = 123}, {__index = string})

print(t.value) -- 123
print(rawget(t, "value")) -- 123
print(t.sub) -- function: string.sub
print(rawget(t, "sub")) -- nil

-- rawset demonstration
t = setmetatable(t, {__newindex = function(self, key, value)
	print("Setting \""..key.."\" to \""..tostring(value).."\"")
	rawset(self, key, value) -- Note that we use "rawset" here to actually set the value and to prevent recursion.
end})
t.hello = "World" -- Setting "hello" to "World"
rawset(t, "sub", 456) -- nothing printed

print(t.hello) -- World
print(t.sub) -- 456
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview