Forum

> > CS2D > Scripts > What are rawset, rawget?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch What are rawset, rawget?

1 Antwort
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Re: What are rawset, rawget?

MikuAuahDark
User Off Offline

Zitieren
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
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht