Here is my command:
[CODE]if (string.sub(text, 1, 7) == "/gamble") then[/CODE]
Now what I want, is like a custom variable, like its /gamble then a number.
eg.
/gamble 21
But now I dunno how to add a number only variable to a command. Can someone help? I dont need anything but a variable that the player sais, what im thinking of is like
[CODE]if (string.sub(text, 1, 7) == "/gamble" .. amount) then[/CODE]
then make amount a custom player variable, but that is COMPLETELY wrong and I know it.
If you don't specify a third argument for string.sub, it will get everything else that is after the second argument.
[lua]local text = "/gamble 500"
local amount = tonumber(text:sub(9))
if (amount and amount > 0) then
print(amount)
else
print("you didn't specify an amount or the amount you specified is invalid")
end[/lua]
[B]Result[/B]
> 500
[URL="http://www.lua.org/cgi-bin/demo"]Check it out.[/URL] This is just an example, of course. You need to adapt the code to your situation just a bit. This was just to show you how you could drop the third argument for your benefit.
or you can use [URL="http://wiki.garrysmod.com/page/string/Explode"]http://wiki.garrysmod.com/page/string/Explode[/URL]
Think about this logically. The variable 'text' is a string containing everything the player has said. So if the player says '/gamble' then string.sub is getting the first 7 characters of the players chat text. Then you're checking if the first 7 chars are equal to '/gamble' if so then you execute your code. In order to parse arguments passed from the players chat text you will need to either split the string with [url=http://wiki.garrysmod.com/page/string/Split]string.split[/url] or you can check if the first 7 are equal to '/gamble' and then use string.sub to get everything after the 8th character which will hopefully be a number. Then use tonumber() to convert it. Make sure you understand the code you're using before you try and implement new things.
I think string.Split and string.Explode should only really be used for multiple matches. Having a table created every single time someone uses the command just seems clunky to me. Both of them call string.sub anyways, and if you use string.sub, it's only 1 line so I don't really see the benefit of using string.Explode or string.Split other than human readability. There's 1 argument for the command. If there was more I'd understand.
string.sub gets the job done perfectly, only needs 1 line, and doesn't create tables. Why create a table if you're only going to put 1 thing in it and then discard the table...?
[QUOTE=rotor2;46184279]I think string.Split and string.Explode should only really be used for multiple matches. Having a table created every single time someone uses the command just seems clunky to me. Both of them call string.sub anyways, and if you use string.sub, it's only 1 line so I don't really see the benefit of using string.Explode or string.Split other than human readability.[/QUOTE]
I think you're underestimating the efficiency of languages and overestimating the effects of creating tables.
Creating a table when a function is run will barely impact performance, and this method can be used regardless of the amount of variables you want/need.
[QUOTE=Internet1001;46184339]I think you're underestimating the efficiency of languages and overestimating the effects of creating tables.
Creating a table when a function is run will barely impact performance, and this method can be used regardless of the amount of variables you want/need.[/QUOTE]
I never said it can't be used regardless of the amount of variables you need. In fact, I said it could. It's not that tables are super expensive and should always be optimized no matter what. It's that you shouldn't be creating things just because you want to.
It's similar to this (yes, you mentioned it).
[lua]function example()
local tbl = {[1] = "example"}
return tbl[1]
end[/lua]
Why do that when you could do this?
[lua]local tbl = {[1] = "example"}
function example()
return tbl[1]
end[/lua]
There's no reason at all to create a table every single time. You say that it will barely impact performance at all. What if you have a large server with multiple players gambling multiple times?
[QUOTE=rotor2;46184395]I never said it can't be used regardless of the amount of variables you need. In fact, I said it could. It's not that tables are super expensive and should always be optimized no matter what. It's that you shouldn't be creating things just because you want to.
It's similar to this.
[lua]function example()
local tbl = {[1] = "example"}
return tbl[1]
end[/lua]
Why do that when you could do this?
[lua]local tbl = {[1] = "example"}
function example()
return tbl[1]
end[/lua]
There's no reason at all to create a table every single time. You say that it will barely impact performance at all. What if you have a large server with multiple players gambling multiple times?[/QUOTE]
That's true but your example applies more to constant data that doesn't change. And since what the player is saying changes every time its perfectly fine to split into a table. And if you do it this way it's much easier to implement more arguments. With string.sub you can't really implement more than one argument easily as you dont always know how many characters each argument will be.
In your example the table is storing constant, fixed data.
It's clear that in this case the values in the table will change since the player's message will change so a new table must be made every time.
[QUOTE=mcd1992;46184468]That's true but your example applies more to constant data that doesn't change. And since what the player is saying changes every time its perfectly fine to split into a table. And if you do it this way it's much easier to implement more arguments. With string.sub you can't really implement more than one argument easily as you dont always know how many characters each argument will be.[/QUOTE]
With multiple arguments, I don't see how you could go without Split and Explode, but the point I'm trying to make is if the command is /gamble, we know that it is 1 argument.
I will agree to disagree.
[QUOTE=rotor2;46184395]You say that it will barely impact performance at all. What if you have a large server with multiple players gambling multiple times?[/QUOTE]
Do you realize that creating even [b]one million[/b] tables takes very little time?
Benchmark:
[lua]
local t1 = SysTime()
for i = 1, 10^6 do
local garbage = {}
end
local t2 = SysTime()
print("dt = " .. (t2 - t1))
[/lua]
Output:
[lua]
dt = 0.0010326951701813
[/lua]
Sorry, you need to Log In to post a reply to this thread.