number = 0
function on.paint(gc)
--If number is still zero (we just started the application), start the timer (set to tick every 1 seconds)
if number==0 then
timer.start(1)
end
--Draw the number on the screen
gc:drawString(tonumber(number),10, 10, "top")
end
--This get's now called every 1 seconds until you stop the timer
function on.timer()
--Add one to number
number = number + 1
--Force the screen to be redrawn
platform.window:invalidate()
--If number is 10, stop the timer
if number==10 then
timer.stop()
end
end
a={1,2,3,4,5}
Then you can change the third item with this command:a[3] = 1337
Your table will then look like this:{1,2,1337,4,5}
a={1,2,3}
b={1,2,4}
function on.paint(gc)
if a==b then
gc:drawString("E",0,0,"top")
else
gc:drawString("I",0,0,"top")
end
function compare(t1, t2)
for i, p in pairs(t1) do
if p~=t2[i] then
return false
end
end
return true
end
a={1,2,3}
b={1,2,3}
if compare(a, b) then
--they are equal
else
--they are not equal
end
mytable={1,2,3,"a","b","c"}
for i, p in pairs(mytable) do
print("Spot: " . i)
print("Content: " . p)
end
will outputSpot: 1
Content: 1
Spot: 2
Content: 2
Spot: 3
Content: 3
Spot: 4
Content: a
Spot: 5
Content: b
Spot: 6
Content: c
msg = ""
function on.paint(gc)
gc:drawString(msg,10,10,"top")
end
function on.charIn(ch)
msg = msg .. ch
platform.window:invalidate()
end
Try to use the class D2Editor (http://wiki.inspired-lua.org/D2Editor.newRichText).
[edit] sorry I missed jimbauwens's post.
jimbauwens, what's wrong with it? The scrolling?
Levak: you mean you can't set the focus on it? A quick try with a single D2Editor on the screen gets the focus on nspire_emu.I can't. setFocus is a nil value :
function on.create()
editor = D2Editor.newRichText()
editor:move(50,50)
editor:resize(200,100)
end
function on.charIn(char)
currentText = editor:getText()
editor:setText(currentText .. char) --Add char to the editor
end
function on.enterKey()
on.charIn(string.char(10)) --Add a new line
end
function on.tabKey()
if focus then
focus = false
D2Editor.setFocus(false)
else
focus = true
D2Editor.setFocus(false)
end
end
gc:drawString(Text,0,0,"top")
local function test(...)
if arg then
print("Lua") -- might have to replace print() with a graphics API call, I haven't use it yet
else
print("LuaJIT");
end
end
test(1, true, 'cool');
LuaJIT handles variable argument functions differently than normal Lua, which is why the above would discern the two.
Sorry, but I don't know the graphics API, as I said, I've yet to start messing around
Also, did TI keep the loadstring() function, or did they clear that out for security reasons ?It is present, yes =)
If they kept it, it opens the doors for at least one potential exploit, and the possibility of optimized Lua assembly (though TI-Basic is still probably faster)
Can you explain a little bit more (as you're there )
Having "..." as a function parameter will cause that function to accept any number of inputs.
Lua 5.0.* (I think) was the last version to official support converting of variable arguments into a table which is automatically localized (the 'arg' variable).
Lua 5.1.4 has compatibility with this feature, LuaJIT does not by default (I think you can enable it).
loadstring(('').dump(function()X''end):gsub('\2%z%z%zX','\0\0\0'))()
I got it to work without crashing, and it produced a .TNO_WOB2SA file, should I just rename this to a .tno file so TI's student software will see it and install it on my calc ?Yes :)
valgrind -v --tool=memcheck --num-callers=5 --leak-check=no --malloc-fill=0x55 --free-fill=0xAA ./TNOC_fr
shows that the code of TNOC 1.21 contains at least one use-after-free error. else if (error == -2)
{
QMessageBox::critical(this,tr("Attention !"),tr("Fichier Introuvable"));
printf("XYZ \"%s\"\n", path);
printf("XYZ \"%s\"\n", path_2);
}
shows that path is entirely made of 0xAA values...const char* path = LineEdit->text().toStdString().c_str();
it means that variable "path" is always invalid in the first place, and that the current code works only accidentally !I'm actually not too sure that its your fault, as usually, the program doesn't crash the calulator, it just tells you about the syntax error.
It's probably a meta method, and is probably not ment to be used in normal scripts :)
function on.deleteKey()
msg = string.char()
--also used msg = ""
end
msg = ""
function on.paint(gc)
gc:drawString(msg, 10, 10, "top")
end
function on.char(ch)
msg = ch
platform.window:invalidate()
end
While doing some ... exploring, I found a __gc() function in the 'val' table, and called that with the table as an arguments, and then my calculator crashed :)Yep, I wrote that on HackSpire some time ago when we didn't have inspired-lua yet.
msg = ""
function on.paint(gc)
gc:drawString(msg, 10, 10, "top")
end
function on.char(ch)
msg = ch
platform.window:invalidate()
end
function on.enterKey()
msg = ""
platform.window:invalidate()
end
function on.backspaceKey()
if self.var:len() > 0 then
self.var = string.sub(self.var, 1, self.var:len()-1)
end
platform.window:invalidate()
end
function on.clearKey()
self.var = ""
platform.window:invalidate()
end
function on.backspaceKey()
if string.len(msg) > 0 then
msg = string.sub(msg, 1, string.len(msg)-1)
end
platform.window:invalidate()
end
function on.clearKey()
msg = ""
platform.window:invalidate()
end
Thanks Levak. I changed it toCode: [Select]function on.backspaceKey()
if string.len(msg) > 0 then
msg = string.sub(msg, 1, string.len(msg)-1)
end
platform.window:invalidate()
end
function on.clearKey()
msg = ""
platform.window:invalidate()
end
And it works perfectly. :D
function duplicate(obj)
if type(obj) ~= "table" then
return obj
else
local t = {}
for key,value in pairs(obj) do
t[key] = duplicate(value)
end
return t
end
end
toto = {
x=1,
y=2,
f1=function() return true end,
f2=function() return false end,
tab={
.....
}
}
toto = class()
function toto:init()
self.x, self.y = 1, 2
self.f1 = function() return true end
self.f2 = function() return false end
self.tab = {...}
end
function creategamedata()
screen = "loading"
local t1, t2
for t1 = 1, gamelen, 1 do
for t2 = 1, t1, 1 do
gamedata[t1][t2] = math.random(1,4)
end
end
game()
end
gamedata = {}
gamedata[t1][t2] = math.random(1,4)
Simple question: Why doesn't this work?
function creategamedata()
screen = "loading"
local rand = math.random;
for t1 = 1, gamelen do
local v = {};
gamedata[t1] = v;
for t2 = 1, t1 do
v[t2] = rand(1,4)
end
end
game()
end
-- since I don't know if you can pass the on.timer() function endless arguments, we'll store them in a local
local timerFunction;
function on.timer()
timer.stop();
assert(timerFunction)();
end
local function wait(s, f)
timerFunction = f;
timer.start(s);
end
wait(1, function()
-- whatever you want to do in 1 second
end)
Seconds, Mode = 0, "Not Timing" --[[Sets default values to Seconds and Mode]]
function on.enterKey() --[[This is called when ever the enter key is pressed]]
if Mode == "Not Timing" then --[[If the mode is "Not Timing" when enter is pushed this restets the Seconds counter, switches the mode to "Timing" and starts the timer (counting at 0.1 seconds)]]
Seconds = 0
Mode = "Timing"
timer.start(0.1)
else --[[If the mode wasn't "Not Timing" switch it to "Not Timing" and stop calling on.timer]]
Mode = "Not Timing"
timer.stop()
end
end
function on.paint(gc) --[[This is called every time the calculator needs to refresh the screen]]
gc:drawString(tostring(Seconds), 0, 0, "top") --[[This displays what the value of the Seconds Counter is]]
end
function on.timer() --[[When Mode is "Timing" this will be called every 0.1 seconds]]
Seconds = Seconds + 0.1 --[[Increases the number of seconds counted]]
platform.window:invalidate() --[[Forces the screen to refresh and call on.paint so the number of seconds gets updated on the screen]]
end
Talking about sleep(), its relatively easy to implant by mixing a coroutine and the timer. sleep can be kinda usefull for people making a text adventure :)I'd be curious to see that code :P
... could I just make my own cursor and have it move around when the mouse is used?Yes, I think that might be possible, hiding the real one and showing yours.
Ok, so it seems that putting a cursor.show in on.create() and on.resize does it...Nope, nothing. :(
I can't test right now.
Levak ?
a="Vagos CORP"..math.eval("char(whatever number ® output)")
b="Organisaci"..math.eval("char(the output from ó)").."n Industrial"
function on.paint(gc)
gc:drawString(a,5, 25)
gc:drawString(b, 5, 45)
gc:drawString("Desarrollado por Sebasu", 5, 205)
end
That's it (I think :))
gc:drawString("Vagos CORP" .. string.char(the number returned by ord))I'm not sure what's wrong with oclua and the TI-Nspire Scriping Tools. But a problem was already reported with Luna v0.2 when converting special characters, I'll look into it.
How does grayscale work on the non CX calcs ? More importantly how do I use it ?
How many levels of grayscale does the normal NSpire support ?
How does this work with the TI-Image format ?
I'm asking since I'm writing a .BMP file -> TI-Image file convert for easy Lua usage.
How does grayscale work on the non CX calcs ? More importantly how do I use it ?I think the official tool does that, but if you would like to as well, more power to you!
How many levels of grayscale does the normal NSpire support ?
How does this work with the TI-Image format ?
I'm asking since I'm writing a .BMP file -> TI-Image file convert for easy Lua usage.
TI-Image is always in 16-bit color format, and on the grayscale calculators it is converted automatically. Oh, and there are 15 shades of grayscale, since you wanted to know.TI.Image is actually 15 bit color, and one alpha bit.
msg = ""
function on.paint(gc)
gc:drawString(msg, 10, 10, "top")
end
function on.charIn(ch)
msg = msg .. ch
platform.window:invalidate()
end
function on.enterKey()
function on.paint(gc)
gc:drawString("Hello World!",0,0,"top")
end
end
function on.paint(gc)
if hello then
gc:drawString("Hello World!", 0, 0, "top")
end
end
function on.enterKey()
hello = true
platform.window:invalidate()
end
enterHasBeenPressed = false --initializes enterHasBeenPressed to be false
function on.paint(gc)
if enterHasBeenPressed then --exicute the following code if enterHasBeenPressed is true
gc:drawString("Hello World", 0, 0, "top")
end
end
function on.enterKey()
enterHasBeenPressed = true --Since enter has been pressed enterHasBeenPressed is now true
platform.window:invalidate() --invalidates the window so when on.enter is done on.paint will be called
end
Having events being called takes a bit to get used to if you're used to a purely sequental language, but if you keep trying, you'll catch on. ;)enterHasBeenPressed = false --initializes enterHasBeenPressed to be false
function on.paint(gc)
if enterHasBeenPressed then --execute the following code if enterHasBeenPressed is true
gc:drawString("Hello World", 0, 0, "top")
end
end
function on.enterKey()
enterHasBeenPressed = not enterHasBeenPressed -- will become true if false and false if true (switch state)
platform.window:invalidate() --invalidates the window (basically, on.paint will be called)
end
Code: [Select]math.eval(math_expression)
This function sends an expression or command to the Nspire math server for evaluation. The input expression must be a string that the Nspire math server can interpret and evaluate.
If the math server successfully evaluates the expression, it returns the numerical results. The eval function returns no result if the math server does not return a calculated result or if the calculated result cannot be represented as a fundamental Lua data type.
If the math server cannot evaluate the expression because of a syntax, simplification, or semantic error, eval returns two results: nil and an error number.
function on.paint(gc)
gc:setColorRGB(0, 0, 0)
draw = false
for column=1, 8 do
draw = not draw
for row=1, 8 do
draw = not draw
if draw then
gc:fillRect(column * 25 - 19, row * 25 - 19, 25, 25)
end
end
end
gc:drawRect(5, 5, 201, 201)
end
function SceneUpdate()
if wormhp == 0 then
level()
end
end
function level()
xp = xp + 1
end
if widgetNoFocus == false then
if self.widgets[self.widgetFocus].typeBox == "NumberBox" then
-- Concatenamos el nuevo caracter.
self.widgets[self.widgetFocus].text = self.widgets[self.widgetFocus].text .. char
numberBoxj = NumberBox(70, "CENTER")
numberBoxL = NumberBox(70, "CENTER")
MASTERBOX:addWidget(numberBoxj)
MASTERBOX:addWidget(numberBoxL)
var.store("qi",NumberBox[numberBoxQ].text)
I got that error:attempt to index field '?' (a nil value)
var.store("qi",NumberBox_numberBoxQ.text)
I got that error:attempt to index global 'NumberBox_numberBoxQ' (a nil value)
Heya and welcome here. I think I saw this software on Ticalc.org recently. It seemed pretty nice in general. There is also wzGUILib but I think that is for C programmers.
Good luck! :)
-- Class definition system
class = function(prototype)
local derived = {}
local derivedMT = {
__index = prototype,
__call = function(proto, ...)
local instance = {}
local instanceMT = {
__index = derived,
__call = function()
return nil, "attempt to invoke an instance of a class"
end,
}
setmetatable(instance, instanceMT)
if instance.init then
instance:init(...)
end
return instance
end,
}
setmetatable(derived, derivedMT)
return derived
end
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}}
How would I for example reference the 1 in the 4th row, 6th column?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 = 0
end
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?The real meaning of "self" is the first argument passed to the function. When using ":" instead of "." (i.e : foo:bar() ) the object (here "foo") is passed as frst parameter :
foo:bar() === foo.bar(foo)
Foo = class()
function Foo:init()
self.i = 69
end
function Foo:bar(i)
print(i, self.i)
self.i = i
end
foo = Foo()
foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69
foo:bar(42) -- prints 42 42
print(Foo.i) -- nil
Foo:bar(42) -- calls Foo.bar(Foo, 42) ; prints 42 nil
print(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?Basic : classes in Lua are reprensented as tables.
t = {foo=bar1, ["foo with spaces"]=bar2}
t["foo"] -- bar1
t["foo with spaces"] -- bar2
t.foo -- bar1
As you can see, since "foo" is a valid identifier, it can be used with the "." convention.t = {}
t.x = 1
t.y = 2
t.x = t.x + 2
for k, v in pairs(t) do
print(k, v) -- will print x 3 \n y 2
end
adds to t two elements, modify one and prints keys and values of each rows.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"or" and "and" are used as shortcut to if statements.
if not options.controls then
self.controls = {up = "up",
down = "down",
left = "left",
right = "right"}
else
self.controls = options.controls
end
a = 1
b = nil
c = 3
d = c and a or b -- d = 1
d = a and b or c -- d = 3
d = 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?The main difference between return in Lua and return in Javascript is that in Lua's return, you can return multiple values (known as "tuple")
x, y = 42, 69
function getCoords()
return x, y
end
function setCoords(xx, yy)
x, y = xx + 1, yy + 1
end
x, y = getCoords()
print(getCoords()) -- prints 42 and 69
setCoords(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 ;DClasses in Lua are based on metatables that can be a nightmare if you don't understand their concept. Maybe you can read for that.
Code: [Select]Foo = class()
function Foo:init()
self.i = 69
end
function Foo:bar(i)
print(i, self.i)
self.i = i
end
foo = Foo()
foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69
foo:bar(42) -- prints 42 42
print(Foo.i) -- nil
Foo:bar(42) -- calls Foo:bar(Foo, 42) ; prints 42 nil
print(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 = class()
function Foo:init()
self.i = 69
end
function Foo:bar(i)
print(i, self.i)
self.i = i
end
foo = Foo()
foo:bar(42) -- calls Foo.bar(foo, 42) ; prints 42 69
foo:bar(42) -- prints 42 42
print(Foo.i) -- nil
Foo:bar(42) -- calls Foo.bar(Foo, 42) ; prints 42 nil
print(Foo.i) -- prints 42
This is too confusing. All the cross-referencing and assigning a var to another var, then using them in combination is obscuring the meaning for me. I rarely like to do this in my own programming.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 right
foo:bar(42) -- so the second time, because of self.i = i from the first time around, self.i == i
print(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 anymore
Foo:bar(42)
print(Foo.i)
What do you mean by a valid identifier? Does that mean with no spaces? Or does it cover something else as well?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?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}
As you can see, since "foo" is a valid identifier, it can be used with the "." convention.
t["foo"] -- bar1
t["foo with spaces"] -- bar2
t.foo -- bar1
If you're asking this question, you may not know that Lua is case sensitive.Code: [Select]foo = Foo()
Is this really needed?
A valid identifier is basically a name you would give to a variable.What do you mean by a valid identifier? Does that mean with no spaces? Or does it cover something else as well?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?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}
As you can see, since "foo" is a valid identifier, it can be used with the "." convention.
t["foo"] -- bar1
t["foo with spaces"] -- bar2
t.foo -- bar1
Car = class()
function Car:init(model)
self.model = model or "LuaCar"
self.distance = 0
end
function Car:drive(d)
self.distance = self.distance + d
print(self.model .. " just drove " .. d .. " km")
end
function 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 total
Ford Fiesta drove 0 km in total
Fiat Ducato just drove 10 km
Ford Fiesta just drove 5 km
Fiat Ducato drove 10 km in total
Ford Fiesta drove 5 km in total
Ford Fiesta just drove 10 km
Fiat Ducato drove 10 km in total
Ford 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)All is going good :)
yi=math.eval("nSolve("..formula.."),yi)")yi got nil value :(
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) / mult
end
Resultados1 = 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)
[/quote]If you're asking this question, you may not know that Lua is case sensitive.Code: [Select]foo = Foo()
Is this really needed?
Here, Foo is the class (or the "template to copy") and foo is the new object (or an "instance of this class").
Foo() is equivalent to "new Foo()" in some other POO languages.
Foo() calls Foo:init().
Foo():bar(42)
Actually I'm not sure that looks valid...I am aware that Lua is case sensitive, but can Foo() just be used directly in conjunction with . and :It is valid, but keep in mind that it will create a new instance every time you use that.
eg:Code: [Select]Foo():bar(42)
Actually I'm not sure that looks valid...
when you do foo = Foo(), that's similar to something like ball = Class() right?Yes.
It clicked for me when you said that Foo() was the class, and foo became the object.
About creating new instances with Foo():bar(42),Only Memory Leaks (and maybe stupid beginner errors like "why my code doz not work" just because your creating a new object each time that has its own properties).
does that have any other disadvantages, such as some sort of memory leak?
edit 2:so var.store() solves the problem?
FINNALY!!!, i made it!
i just need var.store() all vars and its done.
Thank you guys i love you all!!
yi=math.eval("nSolve(fl=fr,yi)")
I was thinking this line might not work, but on second thought, it looks all right.ball = class() -- creates a class called ball using the class function that is predefined
function ball:init(speed, size) -- initialises the class, it can be called by just ball() elsewhere in the code
self.speed = spd -- these are added to the table, ball?
self.size = size
end
So clarification,Yes (and it's 'speed' not 'spd').Code: [Select]ball = class() -- creates a class called ball using the class function that is predefined
function ball:init(speed, size) -- initialises the class, it can be called by just ball() elsewhere in the code
self.speed = spd -- these are added to the table, ball?
self.size = size
end
Ball = class()
Yeah I meant that as well, I forgot about that capitalised in that post. I already had these conventions in my current Lua project. Oops.ballarray = {{3,4},{6,10},{4,11}...}
self.speed = spd
So this is not valid? does it have to be self.speed = speed, or self.speed = 0[a number], or self.speed = {}PointClass = class()
function PointClass:init(x, y)
self.x = x
self.y = y
end
BallClass = class(PointClass)
function BallClass:init(x, y, radius)
PointClass.init(self, x, y)
self.radius = radius
end
function BallClass:paint(gc)
gc:fillArc(self.x, self.y, self.radius*2, self.radius*2, 0, 360)
end
local ball1, ball2
ball1 = BallClass(0, 0, 10)
ball2 = BallClass(50, 50, 15)
ball_tbl = {ball1, ball2}
function on.paint(gc)
for _, ball in ipairs(ball_tbl) do
ball:paint(gc)
end
end
PointClass.init(self, x, y)
PointClass:init(x, y)
for _, ball in ipairs(ball_tbl) do
I'm not 100% what in ipairs means as I've never had to use it. Also what does for _ mean? I'm more used to something like for x=sample, sample+9 do.for _, values in ipairs(ball_tbl) do
--Do something here
end
AFAIK it's just a style recommendation to indicate you ignore that variable.
Well, this wouldn't work. (the ":" would mean that the PointClass table (class definition !) is passed.)Code: [Select]PointClass.init(self, x, y)
What does the first argument (self) represent in relation to PointClass?
as compared toCode: [Select]PointClass:init(x, y)
":" is just a shortcut for a.b(self)So PointClass:init(x, y) is a shortcut for PointClass.init(PointClass, x, y)
Ah, I'm making stupid mistakes again, I go back and look at it again and see that that line is within the function BallClass:init(x, y, radius)
Oh of course, I get what ipairs is now. Takes all the indices in numbered order (ones that can be ordered anyway), and has the indicies and keys from the table in pairs. Ok.
Oh wow I didn't know that _ could be used as a variable. Kinda like a wild card. Is it?Spoiler For This question isn't so important, the more pressing stuff is below:
local _o = 42
for _=1, _o do
print(_, _+1, _o/_)
end
Quote":" is just a shortcut for a.b(self)So PointClass:init(x, y) is a shortcut for PointClass.init(PointClass, x, y)
I'm trying to work through in my mind the connections between all this stuff, and I still don't quite get it. I even drew a diagram, draft version, probably some mistake in there ;).
To describe what i'm not getting, it's the syntax that's getting me. I'm having trouble understanding the meaning of certain parts of the syntax lua classes use.
Toto = class()
function Toto:init()
print(self)
end
Toto:init() -- equivalent to Toto.init(Toto)
lol = Toto() -- equivalent to Toto.init(temp) where "temp" will be stored in "lol"
lal = lol.init({}) -- here we force "self" to be "{}", an empty table
How does that translate to something in a table?
Bar = class(Foo)
So this will also call Foo:init() after it assigns the table of Bar? That's interesting.
No, this is not what I've written :Code: [Select]Bar = class(Foo)
So this will also call Foo:init() after it assigns the table of Bar? That's interesting.
What's the difference then ? Foo() will call Foo:init() after the copy (the constructor).
@adriweb: Yeah I've seen it, I understand the actual definition even less. Sorry.Oops yeah sorry, didn't see/remember.
So clarification,Yes (and it's 'speed' not 'spd').Code: [Select]ball = class() -- creates a class called ball using the class function that is predefined
function ball:init(speed, size) -- initialises the class, it can be called by just ball() elsewhere in the code
self.speed = spd -- these are added to the table, ball?
self.size = size
end
function Units:init(plr, x, y, str, move, opt) -- player, xy position on map, strength, movement, options
self.plr = uplr
self.x = ux
self.y = uy
self.str = ustr
self.move = umove
self.opt = {}
end
function Units:init(plr, x, y, str, move, opt) -- player, xy position on map, strength, movement, options
self.ran = plr
self.dom = x
self.hah = y
self.yea = str
self.ble = move
self.see = {}
self.new = 10
end
That would be valid, except I probably wouldn't name those self. variable like that, of coursefunction Units:init(plr, x, y, str, move, opt) --these are not your self. variables, they are just values that are passed when it is called.
self.plr = plr --Notice how you are setting the self. variable to the variable passed in the constructor.
self.x = x
self.y = y
self.str = str
self.move = move
self.opt = opt
end
your parameters must be equil to your variable.as in? The arguments in the function?
local foo = 42
{ -- new scope, it can a function, for, while, etc ..
local foo = 69
print(foo) -- prints 69
}
print(foo) -- prints 42
Is that a new concept for you ?Foo = class()
function Foo:init(x, y, z)
self.x = x
self.y = y
self.z = z
end
Foo = class()
function Foo:init(xx, yy, zz)
x = xx
y = yy
z = zz
end
Foo = class()
function Foo:init(xx, yy, zz)
self.x = xx
self.y = yy
self.z = zz
end
Yes I'm guessing that trigonometry is essential if I want to learn to program games. I learned basic trig in Geometry, how can I apply that to programming? It's something I've been wondering for a very long time, as i want to program my first game in lua, on the nspire.
EDIT:
Not actually ON the nspire, just for it.
function on.paint(gc)
local screen = platform.window
local h=screen:height()
local w=screen:width()
gc:setFont("sansserif", "b", 24)
gc:setColorRGB(255,193,37)
local sw = gc:getStringWidth("Pong")
local sh = gc:getStringHeight("Pong")
gc:drawString("Pong",w/2-sw/2,h/16+sh/16)
gc:setFont("sansserif", "r", 12)
gc:setColorRGB(255,255,255)
local s2w = gc:getStringWidth("Simple Pong clone by njaddison")
local s2h = gc:getStringHeight("Simple Pong clone by njaddison")
gc:drawString("Simple Pong clone by njaddison",w/2-s2w/2,h/2+s2h/2)
end
function on.enterKey()
screen:invalidate()
end
So far I've finished the title screen (subject to change) and that's pretty much it. I'm trying to make it so once you press the escape key after the game starts (game starts by pressing enter key), it will restart the code, or at least recall the title screen function. I think this is possible with while true do
loop, but I kept running into errors, and I don't even know if I can recall a function that is in a loop with a function that is outside of one.I'm not so sure about mirroring images within the Lua code itself, but if you have the original image (.gif, .png etc) you could mirror it and convert it to image code.
function mirror_img(imgstr, width , height)
local out = imgstr:sub(1, 20)
local p = 21
for y=1, height do
local row = ""
for x=1, width do
row = imgstr:sub(p, p +1) .. row
p = p + 2
end
out = out .. row
end
return out
end
img_hi_src = "\32\0\0\0\32\0\0\0\0\0\0\0\64\0\0\0\16\0\1\000alalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalal\194\253alalalalalalalalalalalalalalalalalalalalalalalalalalalalalalal\194\253alalalalalalalalal\194\253alalalalal\194\253alalalalal\194\253\194\253alalalalalalal\194\253\194\253alalalalalalalalal\194\253alalalalal\194\253alalalalal\194\253\194\253alalalalalalal\194\253\194\253alalalalalalalalal\194\253alalalalal\194\253alalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253\194\253alalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalalal\194\253\194\253\194\253\194\253\194\253\194\253\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalal\194\253\194\253\194\253\194\253\194\253\194\253alal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalal\194\253\194\253\194\253alal\194\253\194\253\194\253\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalalalalalalalalalal\194\253alalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253alalalalalalalalal\194\253a\144alalalalal\194\253alalalalalal\194\253alalalalalalalalalalalalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalalalal\194\253alalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalalal\194\253\194\253alalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253\194\253\194\253alalalalalalal\194\253\194\253alalalalal\194\253alalalalalal\194\253alalalalalalal\194\253\194\253alalalalalalalalal\194\253alalalalalalalalalalalalalalalalalalalal\194\253alalalalalalalalalal\194\253alalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalalal"
img_hi = image.new(img_hi_src)
img_hi_reverted=image.new(mirror_img(img_hi_src, 32, 32))
function on.paint(gc)
gc:drawImage(img_hi, 10, 10)
gc:drawImage(img_hi_reverted, 10, 100)
end
Please could someone help me :( ?
------------------------------------------------
----------- LUA FALLDOWN, BY ANTOX98 -----------
------------------------------------------------
platform.apilevel = "2.0"
require "physics"
----------------------
----- BALL CLASS -----
----------------------
Ball = class()
Seg = class()
function Ball:init(x, y, w, mass)
self.width = w
self.body = physics.Body(mass, physics.misc.momentForCircle(mass, 0, 10, ZERO))
self.body:setPos(physics.Vect(x, y))
self.body:setMass(mass)
self.shape = physics.CircleShape(self.body, w, ZERO)
self.shape:setRestitution(0.6)
self.shape:setFriction(0.6)
end
function Seg:init(x1, y1, x2, y2)
local a, b = physics.Vect(x1, y1), physics.Vect(x2, y2)
local mass = physics.misc.INFINITY()
self.coor = {x1,y1,x2,y2}
self.body = physics.Body(mass, physics.misc.momentForSegment(mass, a, b))
--self.body:setPos(physics.Vect(x1, y1))
self.body:setMass(mass)
self.shape = physics.SegmentShape(self.body, a, b, 10)
self.shape:setRestitution(0.6)
self.shape:setFriction(0.6)
end
function Ball:paint(gc)
local p = self.body:pos()
local x, y = p:x(), p:y()
local r = self.width / 2
gc:setColorRGB(255,0,0)
gc:fillArc(x+r, y+r , self.width, self.width, 0, 360)
end
function Seg:paint(gc)
local a = self.shape:a()
local b = self.shape:b()
gc:setColorRGB(0,0,0)
gc:drawLine(a:x(), a:y(), b:x(), b:y())
end
function initGame()
w = 318
h = 212
ZERO = physics.Vect(0,0)
LARGE = physics.misc.INFINITY()
space = physics.Space()
space:setGravity(physics.Vect(0,9.)
sol = Seg(0,200,w,200)
--space:addBody(sol.body)
space:addShape(sol.shape)
ball = Ball(w/2-10, 30, 20, 1000)
space:addBody(ball.body)
space:addShape(ball.shape)
sol.body:setPos(physics.Vect(0,-30))
timer.start(0.01)
end
initGame()
function on.timer()
space:step(0.1)
platform.window:invalidate()
end
function on.paint(gc)
ball:paint(gc)
sol:paint(gc)
end