• Working with tables
    15 replies, posted
Hey, I've never realized how powerful tables were and their uses were so useful. Anyways, i'm making a script for DarkRP (Sadly) and im trying to get a job's (or team's) playermodel. I have used the Player/getJobTable() but i cant work out how to get the mode value out of it.. p.s. PrintTable prints; [code] max = 0 salary = 45 color: r = 20 b = 20 a = 255 g = 150 hasLicense = false model: 1 = models/player/Group01/Female_01.mdl 2 = models/player/Group01/Female_02.mdl 3 = models/player/Group01/Female_03.mdl 4 = models/player/Group01/Female_04.mdl 5 = models/player/Group01/Female_06.mdl 6 = models/player/group01/male_01.mdl 7 = models/player/Group01/Male_02.mdl 8 = models/player/Group01/male_03.mdl 9 = models/player/Group01/Male_04.mdl 10 = models/player/Group01/Male_05.mdl 11 = models/player/Group01/Male_06.mdl 12 = models/player/Group01/Male_07.mdl 13 = models/player/Group01/Male_08.mdl 14 = models/player/Group01/Male_09.mdl team = 1 command = citizen name = Citizen admin = 0 vote = false candemote = false weapons: description = The Citizen is the most basic level of socie old besides being a hobo. You have no specific role in city life. [/code]
[code] local teams = team.GetAllTeams() local tmodels = {} for k, v in pairs( teams ) do if ( v.Name == "Citizen" ) then tmodels = v.model end end [/code]
[QUOTE=nettsam;46665586][code] local teams = team.GetAllTeams() local tmodels = {} for k, v in pairs( teams ) do if ( v.Name == "Citizen" ) then tmodels = v.model end end [/code][/QUOTE] how does that work with player/getJobTable() .. and arn't playermodels not assigned to the team? (im confused..)
[lua] function team.GetModel(t) return (type(rp.Teams[t].model) == 'table' and rp.Teams[t].model[math.random(1, #rp.Teams)] or rp.Teams[t].model) end [/lua] Direct copy paste from my own server. Replace rp.Teams with whatever table darkrp uses these days.
[QUOTE=StonedPenguin;46665661][lua] function team.GetModel(t) return (type(rp.Teams[t].model) == 'table' and rp.Teams[t].model[math.random(1, #rp.Teams)] or rp.Teams[t].model) end [/lua] Direct copy paste from my own server. Replace rp.Teams with whatever table darkrp uses these days.[/QUOTE] how will I use this exactly? like what do I do with it?
Stoned, giving him the answer without any explanation when he's curious as to how to do it won't help at all. To get the value from a table you can do something like this... [lua]local exampletable = { Models = { Example1.mdl; Example2.mdl; Example3.mdl; Example4.mdl; }; Names = { "ExampleName"; }; };[/lua] Now if you wanted to get the value of models you could do this... [lua]exampletable.Models[1][/lua] That's the first value of the Models table, it's grabbing the first value from the Models. If you wanted to get from the names, you could do [lua]exampletable.Names[1][/lua] That would get you the first value of the names table. Now if you wanted to get all the models, you could just do [lua]local teammodels = exampletable.Models; -- This is a table now[/lua] Just so you can then use said table later.
[QUOTE=Nookyava;46665714]Stoned, giving him the answer without any explanation when he's curious as to how to do it won't help at all. To get the value from a table you can do something like this... [lua]local exampletable = { Models = { Example1.mdl; Example2.mdl; Example3.mdl; Example4.mdl; }; Names = { "ExampleName"; }; };[/lua] Now if you wanted to get the value of models you could do this... [lua]exampletable.Models[1][/lua] That's the first value of the Models table, it's grabbing the first value from the Models. If you wanted to get from the names, you could do [lua]exampletable.Names[1][/lua] That would get you the first value of the names table. Now if you wanted to get all the models, you could just do [lua]local teammodels = exampletable.Models; -- This is a table now[/lua] Just so you can then use said table later.[/QUOTE] Thank you so much! have my babies
Sorry, can't afford child support. If you want them to pay me though...
Actully, there's a problem.. If the team only has one playermodel, it doesn't work, but lets say it had 3 playermodels, it will work..
You could either change each team to use a table, even if they only have 1 model. Or do a check on the variable to determine whether it is a table or not, and use an if/else statement accordingly.
Any other ideas?
can you post your code i don't understand what you are trying to do for the most part
Well, ive got jobs with multiple playermodels working, but if its a team/job with one playermodel, it wont work. Im wondering how would i extract a string out of a table normally.
Print the job with just one player model, it might not be saved in the same format.
Lua is built around tables. Some of this may be repeated but some of it may be new: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/tables/quick_intro_to_tables.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/classes/class_tutorial.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/strings/string_concatenation.lua.html[/url] [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/ternary_operations.lua.html[/url] Now, if you want to see whether or not something is a table, you can use istable( )... Using your table as an example: [code]local _p = LocalPlayer( ); -- or regular player... if ( !IsValid( _p ) ) then return; end // This is where your table comes in... local _data = _p:getJobTable( ); // Since print-table showed us a key "model" we can access it many different ways, as shown in the first html doc listed above. Additionally, your print-table shows numerical keys 1-x ( Lua starts with 1 instead of 0 for numerical keys ) but this will not always be the case so you want some method to differentiate between string vs table and then grab the first model, or a random one... local _model = _data.model; // Define this so it exists in this scope level if we want to use it for a return outside the if below... local _selected = nil; // If it is nil... if ( !_model ) then return; end if ( istable( _model ) ) then // Ok, we have a table. Tables can be numerically keyed or string keyed, but with DarkRP it should be numerical so if we wanted to count the numerical entries we could use #_model, or we could use table.Count( _model ) which will count everything regardless of whether it is numerically keyed or string keyed... // If we only want to return the first value we could simply return _model[ 1 ];, or set _selected to that value and return it outside the if statement... else return _model; end // This would be if you're a "one return" person. -- return _selected;[/code] In short.. istable, isstring, etc works great. You can also use type( data ) which returns a string of what the data-type is including "table"... There is also TypeID( data ) which returns an enumeration such as TYPE_NUMBER / TYPE_TABLE / TYPE_STRING, etc.. Hopefully this helps.
Thanks, with a few tweaking it worked!
Sorry, you need to Log In to post a reply to this thread.