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

Functions Question

Started by johnnic, 25 June 2013 - 09:31 PM
johnnic #1
Posted 25 June 2013 - 11:31 PM
Can functions set variables to be used later, and if so how?
theoriginalbit #2
Posted 25 June 2013 - 11: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.
johnnic #3
Posted 26 June 2013 - 12:01 AM
Thanks. I am trying to make my API and was trying to implement a buttons option.
Imque #4
Posted 27 June 2013 - 08: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.
Engineer #5
Posted 27 June 2013 - 10:34 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.
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 :)/>
Imque #6
Posted 28 June 2013 - 10:23 AM
Oh right. I see what I did wrong. Thanks man.
0099 #7
Posted 30 June 2013 - 04:23 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.
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.
theoriginalbit #8
Posted 01 July 2013 - 06:51 AM
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 on 01 July 2013 - 04:53 AM