if #yyy.xxx == 0 then
print("No")
else
print("Yes")
end
[Lua] [Question] Pulling a variable from an API?
Started by Okyloky9, Jan 31 2013 04:50 AM
12 replies to this topic
#1
Posted 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 :
#2
Posted 31 January 2013 - 05:08 AM
make the variable global and it works just like that without the hash
#3
Posted 31 January 2013 - 05:12 AM
I want to find the length of the variable in the API, how do I do that?
#4
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"
#5
Posted 31 January 2013 - 06:31 AM
Okyloky9, on 31 January 2013 - 05:12 AM, said:
I want to find the length of the variable in the API, how do I do that?
Okyloky9, on 31 January 2013 - 04:50 AM, said:
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() (?)
#6
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() endand 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
#7
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 by KaoS, 31 January 2013 - 09:50 AM.
#8
Posted 31 January 2013 - 09:55 AM
There! Thanks KaoS, it's working now. Thanks for the help guys!
#9
Posted 31 January 2013 - 12:01 PM
1 more thing, string is an API. You are going to do bad overwriting standard API's.
#10
Posted 31 January 2013 - 12:11 PM
Doyle3694, on 31 January 2013 - 12:01 PM, said:
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
#11
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
#12
Posted 01 February 2013 - 06:37 AM
yes you can do that in CC
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











