Jump to content




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


  • You cannot reply to this topic
12 replies to this topic

#1 Okyloky9

  • Members
  • 57 posts

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


#2 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 31 January 2013 - 05:08 AM

make the variable global and it works just like that without the hash

#3 Okyloky9

  • Members
  • 57 posts

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 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 31 January 2013 - 05:18 AM

in the apis file just declare a global variable
xxx="myvar"

then yyy.xxx will return "myvar"

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 31 January 2013 - 06:31 AM

View PostOkyloky9, 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?

View PostOkyloky9, 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 Okyloky9

  • Members
  • 57 posts

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
 


#7 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

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 by KaoS, 31 January 2013 - 09:50 AM.


#8 Okyloky9

  • Members
  • 57 posts

Posted 31 January 2013 - 09:55 AM

There! Thanks KaoS, it's working now. Thanks for the help guys!

#9 Doyle3694

  • Members
  • 815 posts

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 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 31 January 2013 - 12:11 PM

View PostDoyle3694, 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 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

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 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 01 February 2013 - 06:37 AM

yes you can do that in CC

#13 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 03 February 2013 - 11:41 AM

View PostKaoS, on 01 February 2013 - 06:37 AM, said:

yes you can do that in CC

nice! thanks





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users