yes nigga's I post a lot.
anyways
So guys I have been having a lot of trouble creating custom code in GLua. Mainly because I just don't get function parameters.
Let me give you guys an example:
[code]
-- add all elements of array `a'
function add (a)
local sum = 0
for i,v in ipairs(a) do
sum = sum + v
end
return sum
end
[/code]
-- Source: [url]http://www.lua.org/pil/5.html[/url]
So, I just don't understand what the "a" is in the parenthesis is... I mean can it be anything? Like can:
[code]
function ass (ply)
print(ply.. "is gay.")
end
[/code]
Be:
[code]
function ass (myfingeredbutthole)
print(myfingeredbutthole.. "is gay.")
end
[/code]
And if so, HOW?! I mean I'm just so confused. Can someone help me?
It's the parameter name, it can be anything as long as it doesn't conflict with the language keyword names such as end, do, then.
I'm not sure how much more I could explain but uh..
say you had code like this
[CODE]
local lol = true
[/CODE]
well the variable "lol" is now assigned to true in the local scope
and so if we had a function like this
[CODE]
function IsItTrue(some_variable_name)
print(some_variable_name == true)
end
[/CODE]
and we used it like this
[CODE]
IsItTrue(lol)
[/CODE]
the true that "lol" is assigned to is passed into the function and onto the parameter variable. And so it would print "true" into console because the value that's assigned to "some_variable_name" is true and true is equal to true
EDIT: The parameter is a local variable to the function, probably a better way to word it.
[QUOTE=bigdogmat;49855425]It's the parameter name, it can be anything as long as it doesn't conflict with the language keyword names such as end, do, then.
I'm not sure how much more I could explain but uh..
say you had code like this
[CODE]
local lol = true
[/CODE]
well the variable "lol" is now assigned to true in the local scope
and so if we had a function like this
[CODE]
function IsItTrue(some_variable_name)
print(some_variable_name == true)
end
[/CODE]
and we used it like this
[CODE]
IsItTrue(lol)
[/CODE]
the true that "lol" is assigned to is passed into the function and onto the parameter variable. And so it would print "true" into console because the value that's assigned to "some_variable_name" is true and true is equal to true[/QUOTE]
I'm so confused... Can you use an example in GLua?
[editline]2nd March 2016[/editline]
Or even the following:
[code]function f(a, b) return a or b end[/code]
-- Source: [url]http://www.lua.org/pil/5.html[/url]
What does this return?
[QUOTE=Percipience;49855490
[code]function f(a, b) return a or b end[/code]
[/QUOTE]
[code]
function f(a, b) return a or b end
print (f ("hello"))
[/code]
> hello
[code]
function f(a, b) return a or b end
print (f (nil,"doe"))
[/code]
> doe (im not actually testing this cause u hurt my feelings)
What I put is "GLua", Lua and GLua are the same just a few language differences
And I edited my post but I'll say it again, think of function parameter as variables that are local to the function, as that's what they are.
An example of the function you gave would return different things depending on what you gave it..
[CODE]
f(1, 2)
[/CODE]
Would return 1 because 1 is valid
While if you did
[CODE]
f(nil, 2)
[/CODE]
Would return 2 as nil is not valid and so it goes to 2
To give examples as to what those are in the first example the function is pretty much doing this
[CODE]
return 1 or 2
[/CODE]
While in the second example the function is doing this
[CODE]
return nil or 2
[/CODE]
The parameters in your function represent whatever the function is being called with. Like this:
[code]function PrintIsTwo(a)
if a == 2 then
print("Yep, that's two!")
else
print("Nope, that's not two!"
end
end
PrintIsTwo(1)
PrintIsTwo(2)
PrintIsTwo(3)[/code]
This prints "Nope, that's not two!", then "Yep, that's two!", then "Nope, that's not two!" again.
[QUOTE=bigdogmat;49855551]-snip-[/QUOTE]
[QUOTE=TwoYearLurker;49855613]-snip-[/QUOTE]
okay, I think I have a general understanding. however, take the "Demote Upon Death" on the DarkRP Wiki for example:
[code]
PlayerDeath = function(ply, weapon, killer)
ply:teamBan()
ply:changeTeam(GAMEMODE.DefaultTeam, true)
if killer:IsPlayer() then
DarkRP.notifyAll(0, 4, "The mayor has been killed and is therefor demoted.")
else
DarkRP.notifyAll(0, 4, "The mayor has died and is therefor demoted.")
end
end
[/code]
-- Source: [url]http://wiki.darkrp.com/index.php/LUA:Demote_Upon_Death[/url]
The function isn't being called? Like how does it know "ply:teamBan()" is even a player?
or
ply:changeTeam(GAMEMODE.DefaultTeam, true)
??
[editline]2nd March 2016[/editline]
also, how does it know "killer:IsPlayer()"
is even the killer of that person? I mean aren't these parameters like localized variables?
[editline]2nd March 2016[/editline]
Anyone?
Somewhere else in the code that function would be ran. The gamemode probably calls it.
PlayerDeath(ply,weapon,killer)
the parameters will be replaced with the proper values.
ex. PlayerDeath(Player1,"smg1",MegaMech)
in ply:TeamBan() if ply is not a player it will throw an error because ply might not be nil, but ply.TeamBan() would be nil. (Trying to index nil value)
killer is a table value (I think) as well as ply. IsPlayer() probably checks the table to see if a certain key or boolean is true. Might be able to look up the source code perhaps?
Parameters work exactly as local variables, initialized with the actual arguments given in the function call. You can call a function with a number of arguments different from its number of parameters. Lua adjusts the number of arguments to the number of parameters, as it does in a multiple assignment: Extra arguments are thrown away; extra parameters get nil. For instance, if we have a function like
[code]
function f(a, b) return a or b end
[/code]
I got this from:
[url]http://www.lua.org/pil/5.html[/url]
So basically if you make a function parameter with the name "ply" you wouldn't have to call it in Garry's Mod? Since it's already called somewhere else?
[QUOTE=Percipience;49856075]So basically if you make a function parameter with the name "ply" you wouldn't have to call it in Garry's Mod? Since it's already called somewhere else?[/QUOTE]
ULX is my best example here
[lua]
function ulx.stuck(calling_ply)
ULib.tsay(calling_ply, "SAY SOMETHING I'M GIVING UP ON YOU")
end
local stuck = ulx.command( CATEGORY_NAME, "ulx stuck", ulx.stuck, "/stuck" )
stuck:defaultAccess( ULib.ACCESS_ADMIN )
stuck:help( "When you are stuck this command will respawn you")
[/lua]
When someone say's /stuck in chat. ulx runs command ulx.stuck and adds the parameter calling_ply.
The game knows *who* typed /stuck in chat. So it passes this on to ulx.stuck(calling_ply)
It works the sameway for any function with (ply) in it. I can change calling_ply to anything. It doesn't matter because all the game does it pass the Entity() into the parameter. the variable could be called "thisIsDefinitelyNotAnEntity" and it would still work, but anyone trying to read my code would hate me for it.
I could also do ply = LocalPlayer() or plyName = LocalPlayer():GetName()
(Note that if all entites are not initialized LocalPlayer will be nil)
Comprendo?
[QUOTE=MegaMech;49856085]ULX is my best example here
[lua]
function ulx.stuck(calling_ply)
ULib.tsay(calling_ply, "SAY SOMETHING I'M GIVING UP ON YOU")
end
local stuck = ulx.command( CATEGORY_NAME, "ulx stuck", ulx.stuck, "/stuck" )
stuck:defaultAccess( ULib.ACCESS_ADMIN )
stuck:help( "When you are stuck this command will respawn you")
[/lua]
When someone say's /stuck in chat. ulx runs command ulx.stuck and adds the parameter calling_ply.
The game knows *who* typed /stuck in chat. So it passes this on to ulx.stuck(calling_ply)
It works the sameway for any function with (ply) in it. I can change calling_ply to anything. It doesn't matter because all the game does it pass the Entity() into the parameter.
Comprendo?[/QUOTE]
Nope
How does it pass it to "calling_ply" I mean, there has to be something calling this function. Right?
[QUOTE=Percipience;49856103]Nope
How does it pass it to "calling_ply" I mean, there has to be something calling this function. Right?[/QUOTE]
Yes, there is back end code calling ulx.stuck. Something along the lines of this: (Except it would be waaay more complex)
[lua]
hook.run("OnChat", function(calling_ply, msg)
if msg == "/stuck" then do ulx.stuck(calling_ply) end end
end)
[/lua]
Now you'll be wondering where msg comes from. I have no idea... It's just there, God did it. I dunno. More back end code that is not displayed here.
This is why the Gmod API is soo important. Fretta for example has tons of this. A gamemode that uses Fretta has tons of predetermined function names and arguements that Fretta calls. You won't know how a function get's called unless you're looking at a document that specifically tells you when it is going to run.
[QUOTE=MegaMech;49856116]Yes, there is back end code calling ulx.stuck. Something along the lines of this: (Except it would be waaay more complex)
[lua]
hook.run("OnChat", function(msg)
if msg == "/stuck" then do ulx.stuck(calling_ply) end end
end)
[/lua][/QUOTE]
I see, thank you mate! :)
Sorry, you need to Log In to post a reply to this thread.