if #yyy.xxx == 0 then
print("No")
else
print("Yes")
end
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[Lua] [Question] Pulling a variable from an API?
Started by Okyloky9, 31 January 2013 - 03:50 AMPosted 31 January 2013 - 04:50 AM
If I have a local variable called xxx in an API called yyy is there a way I can pull xxx from the yyy API to a whole new program (lets say zzz)? Something like :
Posted 31 January 2013 - 05:08 AM
make the variable global and it works just like that without the hash
Posted 31 January 2013 - 05:12 AM
I want to find the length of the variable in the API, how do I do that?
Posted 31 January 2013 - 05:18 AM
in the apis file just declare a global variable
then yyy.xxx will return "myvar"
xxx="myvar"
then yyy.xxx will return "myvar"
Posted 31 January 2013 - 06:31 AM
I want to find the length of the variable in the API, how do I do that?
If I have a local variable called xxx in an API called yyy is there a way I can pull xxx from the yyy API to a whole new program (lets say zzz)? Something like :if #yyy.xxx == 0 then print("No") else print("Yes") end
That's how you do it, or
string.len(xxx.yyy)
--or
xxx.yyy:length() (?)
Posted 31 January 2013 - 09:34 AM
I appreciate all the help but it's still not working. The full code to the API and program is:
local w,h = term.getSize()
function CLocate(Cstring)
term.clear()
term.setCursorPos(w/2 - #Cstring/2,h/2)
print(Cstring)
end
function prompt(string)
CLocate(string)
answer = read()
end
and the test is just
os.loadAPI("JS")
JS.prompt("What's up?")
if string.len(JS.answer) == 0 then
print("No")
elseif JS.answer == ("The sky.") then
print("That's an old one.")
elseif JS.answer == ("Not much.") then
print("Cool, me too.")
end
Posted 31 January 2013 - 09:44 AM
try just printing JS.answer
EDIT: wait a second. I see your problem. if the variable is set after execution (in a function after the API is loaded) it is not added
rather just say
EDIT: wait a second. I see your problem. if the variable is set after execution (in a function after the API is loaded) it is not added
rather just say
local w,h = term.getSize()
function CLocate(Cstring)
term.clear()
term.setCursorPos(w/2 - #Cstring/2,h/2)
print(Cstring)
end
function prompt(string)
CLocate(string)
JS.answer = read()
end
Edited on 31 January 2013 - 08:50 AM
Posted 31 January 2013 - 09:55 AM
There! Thanks KaoS, it's working now. Thanks for the help guys!
Posted 31 January 2013 - 12:01 PM
1 more thing, string is an API. You are going to do bad overwriting standard API's.
Posted 31 January 2013 - 12:11 PM
1 more thing, string is an API. You are going to do bad overwriting standard API's.
Doyle3694 is 100% right. in this case it wouldn't cause any issues because it is in a 2-line function but I apologize for not pointing that out. avoid overwriting your APIs with other values
Posted 01 February 2013 - 03:45 AM
idk if CC supports this or not, but in vanilla Lua, you can use # on a string to get the length
> #("a string") == 8
true
Posted 01 February 2013 - 06:37 AM
yes you can do that in CC
Posted 03 February 2013 - 11:41 AM
yes you can do that in CC
nice! thanks