MrB
MrB

Reputation: 35

Lua loadstring Function fail

Given this code:

local fruit = {}
fruit.name = "Bramley"
loadstring("fruit.pips = '2'")
fruit.skinc = 'Red'
print(fruit)

Why aren't the pips added to the table: table

'fruit'{
  'name'='Bramley',
  'skinc'='Red'
}

Upvotes: 3

Views: 1347

Answers (1)

Yu Hao
Yu Hao

Reputation: 122383

loadstring() (or load() in Lua 5.2 or higher) returns a function, you have to run that function to execute the code. Like this:

fruit = {}
fruit.name = "Bramley"
loadstring("fruit.pips = '2'")()

Note that fruit has to be global, or an error would be generated because the environment of the returned function of loadstring is the global environment.

Upvotes: 4

Related Questions