Jump to content

Lua Parameters


tommysticks

Recommended Posts

If this thread is too far gone, I understand.

 

I still try to figure out lua from time to time, and for the most part am able to get shit to do what I want, although pretty inefficiently, but I don't understand parameters whatsoever.

 

Small example, this code works perfectly as it is:

addStuff = function(room, containerType, container)     container:AddItem('Base.Pistol'); --this worksendEvents.OnFillContainer.Add(addStuff);

 

...but remove the parameters and it breaks the internet:

addStuff = function()     container:AddItem('Base.Pistol'); --this no longer worksendEvents.OnFillContainer.Add(addStuff); 

 

From what I've read parameters are a sort of local variable to the function... but how the shit does adding those 3 parameters make a difference in the function when none of them are defined?? I assume it is because they have been defined in other functions, but this doesn't help me in understanding how they work.

 

Like I said, if explaining this to me would be like explaining calculus to a cactus I understand if no attempt is made.

Link to comment
Share on other sites

In the second example, you've never defined container.

 

Events.OnFillContainer seems to pass the room, containerType, and container variables to your function.

 

It can't pass those three arguments if you never wrote the function to accept the three arguments in the first place.

 

For example:

 

function multiplyThings(multiplyByArgument)	return numberArgument * multiplyByArgument -- since you never defined numberArgument, this calls nil.endprint(tostring(multiplyThings(5, 1))) -- the function doesn't have a second argument. it won't recognize the 1 as the second argument, because there was no second argument.

 

Won't work.

 

Where as this:

 

function multiplyThings(multiplyByArgument, numberArgument)	return numberArgument * multiplyByArgument -- because you defined the two argument variables, this works.endmultiplyThings(5, 1) -- multiplyByArgument, the first argument, is passed 5. it now has the value of 5. the numberArgument is passed 1, and it has the value of 1.

 

Will work.

Link to comment
Share on other sites

-stuff

Ok, I feel like this makes sense. It seems easy to understand when the parameters are numbers... but to verify:

function mathThings(numberOne, numberTwo)	return numberTwo - numberOneendmathThings(5, 1) 

...would return -4? But when the parameters are strings, this still confuses me.

 

I guess I don't even understand it enough to ask the right questions... but it seems there are functions with parameters that aren't defined anywhere and then the parameters aren't referenced anywhere in the function. Like this from Robomat's Lua tutorial on pzmods.net:

function UIDestroyItems.onDestroyItem(_items, _player, _item)    _item:getContainer():Remove(_item);    _player:Say("I love pz-mods.net");end 

For instance... how does the game know to make your specific player say "I love pzmods.net" only from the parameter _player? I know this is not exactly an example of what I was talking about above, but it is similar. I assume the order of the parameters in the function matters? Like it would have a different result or an error if it was instead:

function UIDestroyItems.onDestroyItem(_player, _item, _items)

?

Link to comment
Share on other sites

Passing a number to a function is still a number. Btw, to convert a string to a number (ex. "5" to 5) use tonumber(stringhere)

 

 

Whatever is calling UIDestroyItems (most likely an inventory event) is returning the IsoGameCharacter Java object, just like getPlayer() does.

 

Because it passed IsoGameCharacter to _player, calling _player will return IsoGameCharacter.

 

So

 

getPlayer():Say("Test")

 

would be the same as

 

_player:Say("Test")

 

in this case.

 

And yes, the order in which you pass the arguments does matter. Think of them as slots.

function thingy(slot1, slot2, slot3)  return slot1 + slot2 + slot3)endthingy(1,2,3) -- assigns 1 to slot1, 2 to slot2, and 3 to slot3.
Link to comment
Share on other sites

I guess I don't even understand it enough to ask the right questions... but it seems there are functions with parameters that aren't defined anywhere and then the parameters aren't referenced anywhere in the function. Like this from Robomat's Lua tutorial on pzmods.net:

function UIDestroyItems.onDestroyItem(_items, _player, _item)    _item:getContainer():Remove(_item);    _player:Say("I love pz-mods.net");end 

For instance... how does the game know to make your specific player say "I love pzmods.net" only from the parameter _player? I know this is not exactly an example of what I was talking about above, but it is similar. I assume the order of the parameters in the function matters? Like it would have a different result or an error if it was instead:

function UIDestroyItems.onDestroyItem(_player, _item, _items)

?

 

So when you do something like:

function YourFunction(argument1, argument2, ...)    ...endEvents.OnSomeEvent.Add(YourFunction);

on the java side of things YourFunction gets added to a list of functions to call when the event OnSomeEvent is triggered.

The triggering is done in java or lua where appropriate with something like triggerEvent("OnSomeEvent", parameter1, parameter2, ...)

 

Thus the event parameters are constant in number and type as defined by the triggering function triggerEvent().

To know what arguments you need to put in your event handler function YourFunction() you have to look at other lua examples where that event was used or find where someone else has done the legwork and documented it for you (or decompile java and find the triggering function usage.)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...