Jump to content




Ask User for Variable


5 replies to this topic

#1 LuckyLuke

  • Members
  • 52 posts
  • LocationMinecraft

Posted 23 July 2015 - 11:00 AM

Hi,

im using this code to ask a user for a global variable, this works fine in another code that i saw on the internet, but it seems that i dont get it. Unfortunalty i cannot provide you with an error, cause at the place where i am at this moment, i cannot run Java :/

testvar = AskForVar("Write something")
function AskForVar(help)
UserInterface()
print("Die Variable \""..varname.."\" wird erstellt.")
write("Bitte geben Sie den Startwert ein (Hilfe: "..help.."):")
return varname = read()
end
print(testvar)

Edit: damn, tab's havent been copied to [ CODE ] tag

Edited by LuckyLuke, 23 July 2015 - 11:12 AM.


#2 wieselkatze

  • Members
  • 221 posts
  • LocationGermany

Posted 23 July 2015 - 11:24 AM

You just want to return read() - also you need to put testvar = AskForVar() below your function as AskForVar is not declared yet at the position it currently is at. It might still be loaded as you set it to be global, but still it'll use the instance created on the pervious run.

The code should then look like this:
local function AskForVar ( help )
    UserInterface() -- Unless this is just a snippet and the function has been declared before, this will error out
    print( "Die Variable \"" .. varname .. "\" wird erstellt." )
    write( "Bitte geben Sie den Startwert ein (Hilfe: " .. help .. "):" )
    return read() -- just return what read() returns
end

local testvar = AskForVar( "Write something" )
print( testvar )

I've localised your variables aswell - it just is better practice as your variables will not show up in the global scoope. Also note that local variables are faster then globals.

#3 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 23 July 2015 - 12:05 PM

View PostLuckyLuke, on 23 July 2015 - 11:00 AM, said:

Edit: damn, tab's havent been copied to [ CODE ] tag

Disable the rich text editor. Little light switch up the top left of the posting box.

#4 LuckyLuke

  • Members
  • 52 posts
  • LocationMinecraft

Posted 23 July 2015 - 12:20 PM

Oh, thank you for your advise!

UserInterface() is a very simple function that clears and sets CursorPos (will be expaned later on), its defined above the function.

Why did you put "local" to the function?

#5 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 23 July 2015 - 12:23 PM

View PostLuckyLuke, on 23 July 2015 - 12:20 PM, said:

Why did you put "local" to the function?

That's something you should always do: localizing all your variables. They are faster and you will not run into problems as much as with global variables.

#6 LuckyLuke

  • Members
  • 52 posts
  • LocationMinecraft

Posted 23 July 2015 - 03:03 PM

Ah, didnt know, i localized only in functions or loops :)
Thanks for the link.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users