Hi there, I am attempting to make a Text Input spawner, which will allow you to spawn NPC's by just typing their name.
I have it working just fine spawning a single NPC but I would like the user to be able to input a number of how many NPC's they would like to spawn, for example to spawn a single headcrab it would look like headcrab(). But to spawn say 10 headcrabs the user would input headcrab(10).
This is all the relevant code I have at the moment which all works.
[lua]function PANEL:GetEntryValue()
return self.TextEntry:GetValue()
end
function PANEL:SetEntryValue(Text)
self.TextEntry:SetValue(Text)
end
function PANEL:Submit()
local Text = self:GetEntryValue();
if Text == "headcrab()" then
RunConsoleCommand("NPC_create NPC_headcrab");
elseif Text && #Text > 0 then
RunConsoleCommand("say", "invalid NPC")
end
end
[/lua]
Could anyone help me or give me tips on how I would get just the number between the brackets. As in my example headcrab(10), just get the number 10 from that line of text.
I hope this all makes sense and thank you in advance!
Patterns..
[lua]local npc_name, npc_count = text:match( "([%w_]+)%((%d+)%)" );[/lua]
Here's a wiki article on them if you want to know more about them..
[url]http://wiki.garrysmod.com/?title=Patterns[/url]
You might do this:
[lua]function PANEL:Submit()
local Text = self:GetEntryValue()
local num1 = string.find(Text, "%(")
local num2 = string.find(Text, "%)")
local numToSpawn = string.sub(Text, num1, num2)
if Text == "headcrab()" then
for i = 1, numToSpawn do
RunConsoleCommand("NPC_create NPC_headcrab")
end
elseif Text && #Text > 0 then
RunConsoleCommand("say", "invalid NPC")
end
end[/lua]
I'm not sure if this would work.
[QUOTE=yakahughes;16099594]You might do this:
[lua]function PANEL:Submit()
local Text = self:GetEntryValue()
local num1 = string.find(Text, "(") --If this is not working, change "(" to "%(" and do the same to the other one. Something about escape characters and what the guy above me said. Idk.
local num2 = string.find(Text, ")")
local numToSpawn = string.sub(Text, num1, num2)
if Text == "headcrab()" then
for i = 1, numToSpawn do
RunConsoleCommand("NPC_create NPC_headcrab")
end
elseif Text && #Text > 0 then
RunConsoleCommand("say", "invalid NPC")
end
end[/lua]
I'm not sure if this would work.[/QUOTE]
Why do this when you can do what nevec said?
It's almost the same.
The difference is that yours doesn't work. You either need to remove the ( ) and its contents or use a pattern for the if. Mine gets the npc name and count in a single line.
[QUOTE=Nevec;16100021]You either need to remove the ( ) and its contents[/QUOTE]
What? Do you mean that numToSpawn or whatever is (1) and not just 1 or something?
If that's what you mean, all you have to do is
local numToSpawn = string.sub(Text, num1 + 1, num2 - 1)
[lua]if Text == "headcrab()" then[/lua]
It obviously won't be equal to that if it ends with, say, (23).
[QUOTE=Nevec;16100893][lua]if Text == "headcrab()" then[/lua]
It obviously won't be equal to that if it ends with, say, (23).[/QUOTE]
Oh, duh. And if he's making this, why does it only work for headcrabs?
Thank you very much for all the replies! Really helpful :D
sorry I have not checked back since, been quite busy. I shall implement a solution using one of the ways tomorrow and see if it works fine, and check back if anybody still cares :p
And indeed I will just not be making it for headcrabs.
[QUOTE=Lime_Donkey;16103179]Thank you very much for all the replies! Really helpful :D
sorry I have not checked back since, been quite busy. I shall implement a solution using one of the ways tomorrow and see if it works fine, and check back if anybody still cares :p
And indeed I will just not be making it for headcrabs.[/QUOTE]
Then you'll have to make it where the word doesn't have to be headcrab(), for reasons that Nevec said.