0 Members and 3 Guests are viewing this topic.
eg = {{1,1,1,2,2,1,3,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,3,2,2,2,3,1,3,3,1} ,{1,1,2,2,2,1,3,3,1,1,2,3,3,3,3,3,1,1,1,2,2,2,2,2,3,3,3,3,3,3,1} ,{1,1,3,3,3,1,1,1,3,3,3,3,3,1,1,1,2,2,2,2,2,3,3,3,3,3,3,1,2,2,2} ,{1,1,3,3,3,1,3,3,3,2,2,2,1,3,1,2,3,1,3,2,3,1,3,2,1,3,3,1,1,2,1} ,{2,2,3,3,3,3,3,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,1,1,1} ,{2,1,3,2,3,1,3,3,1,1,2,2,2,2,1,1,1,1,1,2,1,2,2,3,3,1,2,3,1,2,3} ,{3,3,1,1,1,2,2,2,3,1,1,3,2,1,3,2,1,1,1,2,2,1,2,2,3,2,3,3,3,3,1} ,{3,1,2,3,3,2,1,3,1,2,3,1,3,1,2,2,1,2,1,2,3,1,2,3,1,2,3,1,2,3,1}}
Coyote = class()function Coyote:init(x, y, width, height, options) options = options or {} self.x = x self.y = y self.width = width self.height = height self.tiles = {} self.controls = options.controls or {up = "up", down = "down", left = "left", right = "right"} self.player = {} self.tileSize = options.tileSize or 16 self.scrollThreshold = options.scrollThreshold or 1 self.outOfBoundsColor = options.outOfBoundsColor or {0, 0, 0} self.tileCount = 0end
Q1: This is more of a confirmation. Does self refers to the classname itself? So self.x would refer to Coyote.x in this case? Likewise for the others?
foo:bar() === foo.bar(foo)
Foo = class()function Foo:init() self.i = 69endfunction Foo:bar(i) print(i, self.i) self.i = iendfoo = Foo()foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69foo:bar(42) -- prints 42 42print(Foo.i) -- nilFoo:bar(42) -- calls Foo.bar(Foo, 42) ; prints 42 nilprint(Foo.i) -- prints 42
Q2: What does . refer to? From what I gather it would mean it would add it to a list/table so that if Coyote was called up, you'd get Coyote == {"x"==x, "y"==y, "tiles"=={} etc...}. Is this right?
t = {foo=bar1, ["foo with spaces"]=bar2}t["foo"] -- bar1t["foo with spaces"] -- bar2t.foo -- bar1
t = {}t.x = 1t.y = 2t.x = t.x + 2for k, v in pairs(t) do print(k, v) -- will print x 3 \n y 2end
Q3: How does or affect the variable assigning? in self.controls = options.controls or {up = "up", down = "down", left = "left", right = "right"}, does that mean self.controls becomes both possible values? This is one I'm stumped on
if not options.controls then self.controls = {up = "up", down = "down", left = "left", right = "right"}else self.controls = options.controlsend
a = 1b = nilc = 3d = c and a or b -- d = 1d = a and b or c -- d = 3d = b and a or c -- d = 3
Q4: Also another one, I've never been very confident in what return means (even though I've encountered it in several languages such as JavaScript. I've known it to return a value if it refers to a variable but it seems that elsewhere in the code it can represent some other things too? Clarification?
x, y = 42, 69function getCoords() return x, yendfunction setCoords(xx, yy) x, y = xx + 1, yy + 1endx, y = getCoords()print(getCoords()) -- prints 42 and 69setCoords(getCoords())print(getCoords()) -- prints 43 and 70
Q5: Generally I don't understand classes enough, I don't think I finished reading through the inspired lua tutorial though. Might go back and read it
Code: [Select]Foo = class()function Foo:init() self.i = 69endfunction Foo:bar(i) print(i, self.i) self.i = iendfoo = Foo()foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69foo:bar(42) -- prints 42 42print(Foo.i) -- nilFoo:bar(42) -- calls Foo:bar(Foo, 42) ; prints 42 nilprint(Foo.i) -- prints 42
Foo = class()function Foo:init() self.i = 69endfunction Foo:bar(i) print(i, self.i) self.i = iendfoo = Foo()foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69foo:bar(42) -- prints 42 42print(Foo.i) -- nilFoo:bar(42) -- calls Foo:bar(Foo, 42) ; prints 42 nilprint(Foo.i) -- prints 42
-- Foo:bar(42) -- calls Foo:bar(Foo, 42) ; prints 42 nil++ Foo:bar(42) -- calls Foo.bar(Foo, 42) ; prints 42 nil
foo = Foo()Is this really needed?
foo:bar(42) == Foo():bar(42) -- that makes some sense now, does it call upon Foo() first, then Foo:bar(i) ? doesn't seem rightfoo:bar(42) -- so the second time, because of self.i = i from the first time around, self.i == iprint(Foo.i) -- why is this nil? Is it because Foo:bar(i) took over the 'control' of self.i so Foo:init() doesn't control it anymoreFoo:bar(42)print(Foo.i)
Quote from: Jonius7 on July 21, 2013, 02:26:57 amQ2: What does . refer to? From what I gather it would mean it would add it to a list/table so that if Coyote was called up, you'd get Coyote == {"x"==x, "y"==y, "tiles"=={} etc...}. Is this right?Basic : classes in Lua are reprensented as tables."." let you access to one element (defined of not) of a table. In Lua tables are associatives using all sort of hash key (you can use functions or tables as hash key). In general you access an element using its key with [ and ] such as : Code: [Select]t = {foo=bar1, ["foo with spaces"]=bar2}t["foo"] -- bar1t["foo with spaces"] -- bar2t.foo -- bar1As you can see, since "foo" is a valid identifier, it can be used with the "." convention.
Code: [Select]foo = Foo()Is this really needed?
Quote from: Levak on July 21, 2013, 08:11:15 amQuote from: Jonius7 on July 21, 2013, 02:26:57 amQ2: What does . refer to? From what I gather it would mean it would add it to a list/table so that if Coyote was called up, you'd get Coyote == {"x"==x, "y"==y, "tiles"=={} etc...}. Is this right?Basic : classes in Lua are reprensented as tables."." let you access to one element (defined of not) of a table. In Lua tables are associatives using all sort of hash key (you can use functions or tables as hash key). In general you access an element using its key with [ and ] such as : Code: [Select]t = {foo=bar1, ["foo with spaces"]=bar2}t["foo"] -- bar1t["foo with spaces"] -- bar2t.foo -- bar1As you can see, since "foo" is a valid identifier, it can be used with the "." convention.What do you mean by a valid identifier? Does that mean with no spaces? Or does it cover something else as well?
Car = class()function Car:init(model) self.model = model or "LuaCar" self.distance = 0endfunction Car:drive(d) self.distance = self.distance + d print(self.model .. " just drove " .. d .. " km")endfunction Car:totalDistance() print(self.model .. " drove " .. self.distance .. " km in total")end
myCar1 = Car("Fiat Ducato")myCar2 = Car("Ford Fiesta")myCar1:totalDistance()myCar2:totalDistance()print()myCar1:drive(10)myCar2:drive(5)print()myCar1:totalDistance()myCar2:totalDistance()print()myCar2:drive(10)print()myCar1:totalDistance()myCar2:totalDistance()
Fiat Ducato drove 0 km in totalFord Fiesta drove 0 km in totalFiat Ducato just drove 10 kmFord Fiesta just drove 5 kmFiat Ducato drove 10 km in totalFord Fiesta drove 5 km in totalFord Fiesta just drove 10 kmFiat Ducato drove 10 km in totalFord Fiesta drove 15 km in total
function Car.drive(self, d) self.distance = self.distance + d print(self.model .. " just drove " .. d .. " km")end
cvi=cvi*10^(-7) ui=(qi*4)/(math.pi*di*di) rei=tostring((ui*di)/(cvi)) di=tostring(di) ki=tostring(ki) formula = "yi^(−0.5)=−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)" var.store("formula", formula) equacao = "string(nSolve(expr(formula), yi)|yi<0.1 and yi>0" yi = math.eval(equacao)
attempt to concatenate global 'yi' (a nil value)
--whitout var.store("formula", formula)equacao = "nSolve(formula, yi)|yi<0.1 and yi>0"
Hi, Im triing make a program that need solve complex stuff, and i need to use nsolve( or solve( function since is for cas version.here is my code:Quotecvi=cvi*10^(-7) ui=(qi*4)/(math.pi*di*di) rei=tostring((ui*di)/(cvi)) di=tostring(di) ki=tostring(ki) formula = "yi^(−0.5)=−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)" var.store("formula", formula) equacao = "string(nSolve(expr(formula), yi)|yi<0.1 and yi>0" yi = math.eval(equacao)When i go display, i got error.Quoteattempt to concatenate global 'yi' (a nil value)I have trie:Quote--whitout var.store("formula", formula)equacao = "nSolve(formula, yi)|yi<0.1 and yi>0"Please help me. thanks!
rei=tostring((ui*di)/(cvi)) di=tostring(di) ki=tostring(ki) yi="0.01" formula="yi^(−0.5)=−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)" yi="nSolve("..formula.."),yi)"
yi=nSolve(yi^(−0.5)=−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)),yi)
yi=math.eval("nSolve("..formula.."),yi)")
function Tipo1:Save() --Turn string into number-- qi=math.eval(numberBoxQ.text) li=math.eval(numberBoxL.text) di=math.eval(numberBoxD.text) cvi=math.eval(numberBoxcv.text) ki=math.eval(numberBoxK.text) --If no imput variable get 0-- if numberBoxQ.text == "" then qi=0 end if numberBoxL.text == "" then li=0 end if numberBoxD.text == "" then di=0 end if numberBoxcv.text == "" then cvi=0 end if numberBoxK.text == "" then ki=0 end --Solving the problem-- ui=(qi*4)/(math.pi*di*di) rei=(ui*di)/(cvi) --Using tostring to insert into formula-- rei=tostring(rei) di=tostring(di) ki=tostring(ki) --Declaring yi close value from the real expected-- yi="0.01" --full formula-- math.eval("yi^(−0.5)=:fl") math.eval("−2*log(((ki)/(di*3.7))+((2.51*yi^(−0.5))/(rei)),10)=:fr") --solving the formula-- yi=math.eval("nSolve(fl=fr,yi)") --disp results-- helpLaunch(Resultados1)end --simple round--function round(num, idp) local mult = 10^(idp or 0) return math.floor(num * mult + 0.5) / multendResultados1 = class()function Resultados1:init() labelBoxu = LabelBox(" U = "..round(ui,7).." m/s") labelBoxre = LabelBox(" Re = "..round(rei,7)) --Got problem here--171: attempt to concatenate global 'yi' (a nil value)-- labelBoxy = LabelBox(" λ = "..yi)