Forum

> > CS2D > Scripts > Get 1 var on return
Forums overviewCS2D overview Scripts overviewLog in to reply

English Get 1 var on return

5 replies
To the start Previous 1 Next To the start

old Get 1 var on return

sixpack
User Off Offline

Quote
Got this func:
1
2
3
4
5
6
7
8
9
10
function itemcount(id, itemid)
	local amount, items = 0, {}
	for k, v in ipairs(PLAYERS[id].Inventory) do
		if v == itemid then
			amount = amount + 1
			table.insert(items, k)
		end
	end
	return amount, items
end
and I want to get the "amount" result when I call this like:
1
variable1 = itemcount(id, 1)
How to do this?

P.S. Can someone explain the difference of:
1
2
variable = 1
local variable = 1

old Re: Get 1 var on return

Kirschkaffee
User Off Offline

Quote
DaKnOb has written
variable = 1
local variable = 1


a local variable cant be called outside the function it is declared in. so you can declare local variables of the same name in different functions and they all will have a different value.

For the other stuff i cant help you, i dont know about LUA.

old Re: Get 1 var on return

sixpack
User Off Offline

Quote
So there's no point using it if you got small amount of RAM since it will use more RAM right?

old Re: Get 1 var on return

Lee
Moderator Off Offline

Quote
From my experiences with online forums and searching for programming related material, what always makes me bang my head in disbelief is how often people would ask a question, and having finally found the answer, would just reply back saying "finally figured it out" rather than giving out the actual solution...

For the posterity who may stumble across this post looking for answers.

1
function f() return 1, 2 end

How do you grab the first and the second return values?

1. Pattern matching
a, _ = f() -- assigns a = 1
_, b = f() -- assigns b = 2


2. Even simpler - The function call creates two items on the stack, a = f() pops the first item off, so the following should also suffice

a = f() -- assigns a = 1, 2 is temporarily created as discarded at the start of the next line


3. Using select

a = select(1, f()) -- a = 1
b = select(2, f()) -- b = 2
c = select(3, 1,3,5) -- c = 5


4. Grouping using tables
list = {f()} -- list = {1,2}
a = list[1] -- a = 1
b = list[2] -- b = 2

There are also situations where you don't know how many items are being returned or passed into a function. In this case, select and table grouping are specifically helpful.


On local versus global.

When assigning global variables, you use more ram both in the short run and in the long run. This is because local variables are bound only to the current "function level" (function stack in other terminology) so they disintegrate with the function as long as its reference isn't returned back or a higher level stack references it as an upvalue.

Global variables are not garbage collected until the entire program terminates. The various states of the global variable are overwritten as well so they are not immediately garbage collected either (userdata from native functions requires this functionality). At the same time, global assignment also writes to the _G table, incurring additional overhead.

When do you use local variables?

The best practice is to use them as often as applicable. If you litter the top level (the global scope) with "temporary" variables, you're not only bloating your program, you're also making it exceptionally easy to introduce unwanted bugs into your program.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview