Good evening,
I am currently editing a Scoarboard in Lua and there are loaded by a GroupManager of the groups without formatting. These must now be formatted.
Example:
Current Rank: "superadmin"
Goal: "Super Admin"
func = function(ply) return ply:GetUserGroup() end
This funktion returns the groupname "superadmin" but now i don't have no idea
Since it's not possible to map from lower case words to upper case without knowing where to break words, you can create a table with all the possible combinations that you want to convert. For example:
local convtable = {
superadmin = "Super Admin",
somethingelse = "Something Else",
}
func = function(ply)
local group = ply:GetUserGroup()
return convtable[group] or group
end
This will return the converted text or the original string if it's not present in the mapping.
Related
tablelogin = {0 = "test1",1 = "test2",2 = "test3",3 = "test4"}
for pp=0,#table do
if takeinternalogin == (tablelogin[pp]) then
LogPrint("Login found")
else
LogPrint("failed login not found")
end
end
takeinternalogin is an internal function of my program that takes the person's login.
In this script I'm taking the person's login and comparing whether or not the login is in the table.
It works, but after the else if the person's login is not in the table, it returns the message "failed login not found" 4 times, that is, it returns the number of times it checks the table.
I don't understand. How can I make the message execute only 1 time?
first of all table is Lua's table library. You should not use it as a variable name. Unless you've added numeric fields to that table or replaced it with something else #table should be 0 and hence your loop should not do anything.
But as you say your loop runs 4 times I suppose you modified table.
You say internallogin is a function so you can never enter the if block as you compare a function value cannot equal a string value: takeinternalogin == (tablelogin[pp] is always false! takeinternallogin would have to return a string.
Why you're using #table here in the first place is unclear to me.
You are currently printing the error message every time it iterates over the table and the current value does not match.
local arr = {[0] = "test1", [1] = "test2", [2] = "test3", [3] = "test4"}
function findLogin(input)
for i,v in pairs(tablelogin) do
if v == input then
LogPrint("Login found")
return i
end
end
LogPrint("failed login not found")
end
login = findLogin(takeinternalogin)
Using return within a loop makes it break out of the loop and in this case never reach the line where it prints the error.
I have table like a:
{ "video", video_search ( message ) }.
How can i make check if input string = "video" then execute video_search command?
The correct answer to your question would be
if input_string == "video" then
video_search()
end
but going by the title and a fair bit of intuition, I assume you really want to ask
how do I associate functions with strings and, given a string, call the associated function?
To which the answer is a different one: first, restructure your table so it looks like this
local whatever = {
["video"] = video_search;
["audio"] = audio_search;
-- whatever else you have...
}
then you can just call the function like this:
local input = "video"
local message = "whatever a message is in your program"
whatever[input](message)
I am having trouble with my tables, I am making a text adventure in lua
local locxy = {}
locxy[1] = {}
locxy[1][1] = {}
locxy[1][1]["locdesc"] = "dungeon cell"
locxy[1][1]["items"] = {"nothing"}
locxy[1][1]["monsters"] = {monster1}
The [1][1] refers to x,y coordinates and using a move command I can successfully move into different rooms and receive the description of said room.
Items and monsters are nested tables since multiple items can be held there (each with their own properties).
The problem I am having is getting the items/monsters part to work. I have a separate table such as:
local monsters = {}
monsters["rat"] = {}
monsters["rat"]["Name"] = "a rat"
monsters["rat"]["Health"] = 5
monsters["rat"]["Attack"] = 1
I am using a table like this to create outlines for various enemy types. The monster1 is a variable I can insert into the location table to call upon one of these outlines, however I don't know how to reference it.
print("You are in ", locxy[x][y]["locdesc"]) -- this works
print("You can see a ", locxy[x][y]["monsters]["Name"],".") - does not work
So I would like to know how I can get that to work, I may need a different approach which is fine since I am learning. But I would also specifically like to know how to / if it possible to use a variable within a table entry that points to data in a separate table.
Thanks for any help that can be offered!
This line
locxy[x][y]["monsters]["Name"]
says
look in the locxy table for the x field
then look in the y field of that value
look in the "monsters"` field of that value
then look in the "Name" field of that value
The problem is that the table you get back from locxy[x][y]["monsters"] doesn't have a "Name" field. It has some number of entries in numerical indices.
locxy[x][y]["monsters][1]["Name"] will get you the name of the first monster in that table but you will need to loop over the monsters table to get all of them.
Style notes:
Instead of:
tab = {}
tab[1] = {}
tab[1][1] = {}
you can just use:
tab = {
[1] = {
{}
}
}
and instead of:
monsters = {}
monsters["rat"] = {}
monsters["rat"]["Name"] = "foo"
you can just use:
monsters = {
rat = {
Name = "foo"
}
}
Or ["rat"] and ["Name"] if you want to be explicit in your keys.
Similarly instead of monsters["rat"]["Name"] you can use monsters.rat.Name.
I was wondering whether it is possible to display tables in the console. Something like:
player[1] = {}
player[1].Name = { "Comp_uter15776", "maciozo" }
InputConsole("msg Player names are: " .. player[1].Name)
However, this is obviously wrong as I receive the error about it not being able to concatenate a table value. Is there a workaround for this?
Much thanks in advance!
To turn an array-like table into a string, use table.concat:
InputConsole("msg Player names are: " .. table.concat(player[1].Name, " "))
The second argument is the string placed between each element; it defaults to "".
to make life easier on yourself for this... i'd recommend naming elements in the inner tables as well. this makes the code above easier to read when you need to get at specific values in a table that are meaningful for some purpose.
-- this will return a new instance of a 'player' table each time you call it.
-- if you need to add or remove attributes, you only need to do it in one place.
function getPlayerTable()
return {FirstName = "", LastName = ""}
end
local players = {}
local player = getPlayerTable()
player.FirstName = "Comp_uter15776"
player.LastName = "maciozo"
table.insert(players, player)
... more code to add players ...
local specific_player = players[1]
local specific_playerName = specific_player.FirstName..
" ".. specific_player.LastName
InputConsole("msg Some message ".. specific_playerName)
Just picking upon Lua and trying to figure out how to construct tables.
I have done a search and found information on table.insert but all the examples I have found seem to assume I only want numeric indices while what I want to do is add key pairs.
So, I wonder if this is valid?
my_table = {}
my_table.insert(key = "Table Key", val = "Table Value")
This would be done in a loop and I need to be able to access the contents later in:
for k, v in pairs(my_table) do
...
end
Thanks
There are essentially two ways to create tables and fill them with data.
First is to create and fill the table at once using a table constructor. This is done like follows:
tab = {
keyone = "first value", -- this will be available as tab.keyone or tab["keyone"]
["keytwo"] = "second value", -- this uses the full syntax
}
When you do not know what values you want there beforehand, you can first create the table using {} and then fill it using the [] operator:
tab = {}
tab["somekey"] = "some value" -- these two lines ...
tab.somekey = "some value" -- ... are equivalent
Note that you can use the second (dot) syntax sugar only if the key is a string respecting the "identifier" rules - i.e. starts with a letter or underscore and contains only letters, numbers and underscore.
P.S.: Of course you can combine the two ways: create a table with the table constructor and then fill the rest using the [] operator:
tab = { type = 'list' }
tab.key1 = 'value one'
tab['key2'] = 'value two'
Appears this should be the answer:
my_table = {}
Key = "Table Key"
-- my_table.Key = "Table Value"
my_table[Key] = "Table Value"
Did the job for me.