Help turning block of code into function

I have a block of code that I am writing over and over (I actually only wrote it twice but I am going to use it a lot.) that I just can’t figure out how to put into a function correctly. the actual code is for a shop item in a clicker game and the code looks like this:

		if gui.pick_node(item_1, action.x, action.y) and points > item_1_price then --Item 1
			pps = pps + 1
			print("pps =",pps)
			msg.post("Button", pps)
			msg.post("Button", "item_1_charge")
			msg.post("Button", item_1_price)
		end

The if statement is also in a if statement checking for left click. I tried to put the block into a function all ready but it ended up confusing me because I didn’t know how to add the parameters to the block of code.

1 Like

I assume that ‘pps’ is a global variable, or at least defined at the top of the script scope. That’s why I’m not passing it as a parameter (doing so would have the variable treated as a value, not a reference, so pps would not increase as expected).

I’ve also not changed the “item_1_charge” message because I’m not sure what it does and how to re-write it so it doesn’t require a hardcoded string.

Beyond that, it’s no more difficult than:

local function function_name(item_node, action, price)

    if gui.pick_node(item_node, action.x, action.y) and points > price then
        pps = pps + 1
        print("pps =",pps)
        msg.post("Button", pps)
        msg.post("Button", "item_1_charge") -- <- what does this do? can this be done in a way that doesn't rely on a hardcoded string?
        msg.post("Button", price)
    end

end

To call the function:

function_name(item_1, action, item_1_price)

What are the specific issues you are having?

3 Likes

That looks like some good code but I have one question, what’s a local function. and also about the problem I was having I wasn’t really Shure how function act inside of functions. and about the item one charge thing that just runs some code in another game object to actual subtract points for what you buy so that means I can just put another parameter for item_x_charge. thanks this code will be helpful, when I tried making the function I just made a parameter for every variable in the function. also I need ideas for challenging myself in coding because the stuff I’m doing now is sort of easy (besides the fact that it takes me forever to debug.)

I ended up adding a couple more parameters to make the function flexible enough for my game:

function function_name(item_node, action, price, item_charge, item_type, increment_amount)
	if gui.pick_node(item_node, action.x, action.y) and points > price then
		item_type = item_type + increment_amount
		print("new value =",item_type)
		msg.post("Button", item_type)
		msg.post("Button", item_charge)
		msg.post("Button", price)
	end
end

now I’m having a problem where I cant get the increment to work correctly, you can see in the block above that item_type and increment_amount but I need the function to change the variable that it is given and not just change its parameters, I thought of having it return a value but it doesn’t know what variable to change.

Right, it does sound like you need to understand scoping in Lua. Read this section of the manual (in fact the whole page is useful).

In simple terms, the “scope” of a variable (or function) in Lua has to do with where it is available (“blocks”). A block might be a function, or an entire script.

Something with the prefix ‘local’ is available only in the block where it is defined. Without that prefix, it is global, and available everywhere. Using global variables can quickly get messy, and you risk accidentally overwriting them.

Say you define these two variables in script A:

local here = "value1"
everywhere = "value2"

In script B, you do this:

print(here)
print(everywhere)

The output will be:

nil
value2

In the example above, ‘here’ is local to script A, whereas ‘everywhere’ is a global variable, and thus available everywhere.

Another example, but this time the block will be a function rather than a script.

function init(self)

	local var1 = 1 --defined in the init() block

	local function do_stuff()

		local var2 = 1 -- defined in the do_stuff() block, a subset of the init() block

		var1 = var1 + 1
		var2 = var2 + 1

	end

	do_stuff()

	print(var1)
	print(var2)

end

var1 is defined inside the init() scope, and is therefore available inside the entirety of that scope (including inside the local function do_stuff()). On the other hand, var2 is defined only inside do_stuff().

The output:

2
nil

This is because var2 is not available outside of the do_stuff() block.

You also need to understand referencing. When you pass in variables containing values to a function, you are passing in just the value - not the variable. So if you modify the value in the function, you are just modifying the value and not the variable.

function init(self)

	local var1 = 1

	local function do_stuff(var)
		var = var + 1
		print(var)
	end

	do_stuff(var1)

	print(var1)
end

Output:

2
1

The first output is the value modified inside the function. As stated, this doesn’t change the variable, which is illustrated by the second print. Actually modifying the variable could be achieved using a return however, as you have already hinted at:

function init(self)

	local var1 = 1

	local function do_stuff(var)
		var = var + 1
		return var
	end

	var1 = do_stuff(var1)

	print(var1)
end

Output:

2

Programming in Lua is an excellent (and free) resource to learn everything about Lua.

5 Likes