This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Okyloky9's profile picture

[Lua] [Question] Pulling a variable from an API?

Started by Okyloky9, 31 January 2013 - 03:50 AM
Okyloky9 #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 :

if #yyy.xxx == 0 then
print("No")
else
print("Yes")
end
KaoS #2
Posted 31 January 2013 - 05:08 AM
make the variable global and it works just like that without the hash
Okyloky9 #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?
KaoS #4
Posted 31 January 2013 - 05:18 AM
in the apis file just declare a global variable

xxx="myvar"

then yyy.xxx will return "myvar"
remiX #5
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() (?)
Okyloky9 #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()
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
 
KaoS #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

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
Okyloky9 #8
Posted 31 January 2013 - 09:55 AM
There! Thanks KaoS, it's working now. Thanks for the help guys!
Doyle3694 #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.
KaoS #10
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
tesla1889 #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
KaoS #12
Posted 01 February 2013 - 06:37 AM
yes you can do that in CC
tesla1889 #13
Posted 03 February 2013 - 11:41 AM
yes you can do that in CC

nice! thanks