Jump to content




Functions Question


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

#1 johnnic

  • Members
  • 50 posts
  • LocationSomewhere in ****** County, *******

Posted 25 June 2013 - 10:31 PM

Can functions set variables to be used later, and if so how?

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 June 2013 - 10:36 PM

Yes they can, just define the variable at the top of your code.
local someVar = 1 --# your variable for later

local function change( num )
  someVar = num
end

print(someVar)
change(5)
print(someVar)
change(7)
print(someVar)

Also do note, that there are some cases where it will not be able to set the variable, and that is when your name any variables in your function the same as ones outside, in this case when out use the variable it will always be the one inside the function and not modify the external one. example
local someVar = 1

local function change( someVar )
  someVar = someVar
end

print(someVar)
change(5)
print(someVar)
change(7)
print(someVar)
this will always output 1.

#3 johnnic

  • Members
  • 50 posts
  • LocationSomewhere in ****** County, *******

Posted 25 June 2013 - 11:01 PM

Thanks. I am trying to make my API and was trying to implement a buttons option.

#4 Imque

  • Members
  • 134 posts

Posted 27 June 2013 - 07:42 AM

If you need to change a specific variable in a function you can use

function change(var1, toV)

  _G[var1] = _G[toV];

end


If you call change(version, "TestProgram") it would be the same as version = "TestProgram"

I used this for setting variables a while ago.

#5 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 27 June 2013 - 09:34 AM

View PostImque, on 27 June 2013 - 07:42 AM, said:

If you need to change a specific variable in a function you can use

function change(var1, toV)

  _G[var1] = _G[toV];

end


If you call change(version, "TestProgram") it would be the same as version = "TestProgram"

I used this for setting variables a while ago.
The funny part is, that it doesnt work like that. If you store something globally, so not with the local prefix, it still wont work. Sinc _G is a table and version can be anything.
Lets assume version 1.5: (Im doing what there is code wise )
version = 1.5
local function change( x, z )
  _G[x] = _G[z]
end

change( version, "newv" ) -- Now the function runs like this: _G[1.5] = _G["newv"]

So it would only work with strings, and btw, you need to change it around:
local function change( var, toVar ) -- Both needs to be stringed
	_G[toVar] = _G[var]
	_G[var] = nil
end
--SImpler:
local function change( var, toVar ) -- int - stringed
	_G[toVar] = var
   -- Cannot set var to nil
end

Your concept was good though :)

#6 Imque

  • Members
  • 134 posts

Posted 28 June 2013 - 09:23 AM

Oh right. I see what I did wrong. Thanks man.

#7 0099

  • Members
  • 52 posts

Posted 30 June 2013 - 03:23 PM

View Posttheoriginalbit, on 25 June 2013 - 10:36 PM, said:

Yes they can, just define the variable at the top of your code.
local someVar = 1 --# your variable for later

local function change( num )
  someVar = num
end

print(someVar)
change(5)
print(someVar)
change(7)
print(someVar)

Also do note, that there are some cases where it will not be able to set the variable, and that is when your name any variables in your function the same as ones outside, in this case when out use the variable it will always be the one inside the function and not modify the external one. example
local someVar = 1

local function change( someVar )
  someVar = someVar
end

print(someVar)
change(5)
print(someVar)
change(7)
print(someVar)
this will always output 1.
And:
function generate()
  local var = 0
  local function change(new)
	var = new
  end
  local function get()
	return var
  end
  return change, get
end

f1, f2 = generate()
If you run it, Lua will create a variable without global name, you can only access it via 2 functions returned by generate(), they'll have global names "f1" and "f2".
f1(new_value) will modify it, f2() will return it. You can also store f1 as local, then you'll have a variable that everyone can get, but only you can change.

#8 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 01 July 2013 - 05:51 AM

View Post0099, on 30 June 2013 - 03:23 PM, said:

And:
function generate()
  local var = 0
  local function change(new)
	var = new
  end
  local function get()
	return var
  end
  return change, get
end

f1, f2 = generate()
If you run it, Lua will create a variable without global name, you can only access it via 2 functions returned by generate(), they'll have global names "f1" and "f2".
f1(new_value) will modify it, f2() will return it. You can also store f1 as local, then you'll have a variable that everyone can get, but only you can change.
That could work, but its a lot more cumbersome than what I've posted and requires knowledge of function pointers, plus its just means lots of functions, if you're going to take advantage of pointers just use a table.

local vars = {}

local function change(tbl, idx, new)
  tbl[idx] = new
end

vars["somevar"] = 5

Edited by theoriginalbit, 01 July 2013 - 05:53 AM.






3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users