Jump to content

[Help]Tables


Perfect

Recommended Posts

Hello, Can some help me on making Tables for the following script ?

    function (attacker, weapon, bodypart, loss) 
if weapon == 2 or weapon == 3 or weapon == 4 or weapon == 5 or weapon == 6 or weapon == 7 or weapon == 8 or weapon == 9 then 
....... 
end 
end 
  

Well, I am trying to make a table for all weapons in groups of slot 0 no weapons, slot 1 melee, slot 2 handguns ....etc

You can find those weapons slots at :- https://wiki.multitheftauto.com/wiki/Weapon

I always have difficulties in making tables, can someone Please teach me some basics ? f

from creating tables to loading from tables ?

(or just make a table for above script with loading facility and i will try to understand it.)

Link to comment

You should check this Lua article out: http://lua-users.org/wiki/TablesTutorial

This is one approach to do what you are looking for:

local myTable = { [ 2 ] = true, [ 3 ] = true, [ 4 ] = true, [ 5 ] = true, [ 6 ] = true, [ 7 ] = true, [ 8 ] = true, [ 9 ] = true } 
local myValue = 1 
  
if ( myTable[ myValue ] ) then -- Check if myValue exists as a key inside the myTable table 
    print( "We found that value from the table." ) -- Print a success message if found 
else 
    print( "We could not find that value from the table." ) -- Print a fail message if not found 
end 

And another approach to that:

local function isValueInTable( theTable, theValue ) -- Initialize a local function for isValueInTable, which takes in two arguments: a table and a value 
    if ( theTable == nil ) or ( theValue == nil ) or ( type( theTable ) ~= "table" ) then return false end -- If an argument is invalid, then just return false right away 
    for _,v in pairs( theTable ) do -- Loop through the specified table and define v as the item value (we "ignore" the key by making it an underscore, but it could be anything, simply put) 
        if ( v == theValue ) then -- If the current table item's value is the same as the value I just passed in... 
            return true -- ... then just return true 
        end 
    end 
    return false -- If something went wrong or not found, then just return false 
end 
  
local myTable = { [ 2 ] = true, [ 3 ] = true, [ 4 ] = true, [ 5 ] = true, [ 6 ] = true, [ 7 ] = true, [ 8 ] = true, [ 9 ] = true } 
local myValue = 1 
  
if ( isValueInTable( myTable, myValue ) ) then -- Run through the above isValueInTable function with the above myTable table and myValue value 
    print( "We found that value from the table." ) -- Print a success message if found 
else 
    print( "We could not find that value from the table." ) -- Print a fail message if not found 
end 

The first one is perhaps simpler to understand, since you are comparing your value with the table keys. If you can find an item with that corresponding key then return true, otherwise false.

The second one is perhaps easier to write. That way you will only need to type in your values in the table without using keys. Then you just loop through the table and see if you can find any item with that specific value. Again the same method of returning true or false. The upside in using the first method is that you are able to assign them specific values, this way you can switch those table items on and off by simply changing true to false (which makes it automatically return false in the if-statement) - or if you want to assign it a string or an integer representing something useful that you will use later on in your code it will return true if it has anything else but null or true as a value. The second method isn't really ideal for such usage.

But really it's up to your needs. If you want to keep it nice and clean and something that you are easily able to modify, then perhaps the first method. Otherwise go for the second one (especially if you have a lot of values and you don't need to modify them or so).

Edited by Guest
Link to comment

The reason why I use the myValue variable is to find out if that myValue exists as a key inside myTable. The key in the tables are basically defined between the square brackets [ ], so in my example it would not find such key, because there is no [ 1 ] defined in the table.

The second one is basically a loop through the table and it checks if that specified value is the same as the one currently looping through on.

I edited my code with some comments, you should check them out.

Link to comment

so you mean i have to do this ?

myValue = 2,3,4,5,6,7,8,9 

Well, I don't understand a thing about tables except creating table and i am also not sure of it.

From my understand, i create This :-

weapons = { 2,3,4,5,6,7,8,9} 
  
function (attacker, weapon, bodypart, loss) 
if table.sort[#weapons] then 
....... 
end 
end 

Can you Please tell me this will work or not ? and why not ?

Link to comment

Aha, well, your code would not work the way you would want it to. You are sorting the table, not checking for value collision.

Like I showed in my example, it would work like this:

local myTable = { 
    [ 2 ] = true -- We create an item within the table, its key is set to 2, and its value is set to true (it can be anything, a string or an integer or whatever) 
} 
local myValue = 1 -- I want to search for an item with a key defined as 1 
  
if ( myTable[ myValue ] ) then -- Check if that key is defined in myTable 
    print( "We found that value from the table." ) -- We found that key 
else 
    print( "We could not find that value from the table." ) -- We were unable to find that key, meaning it is not defined in the table 
end 

So, in your case you would do it like this:

local weapons = { [ 2 ] = true, [ 3 ] = true, [ 4 ] = true, [ 5 ] = true, [ 6 ] = true, [ 7 ] = true, [ 8 ] = true, [ 9 ] = true } 
function yourFunctionName( attacker, weapon, bodypart, loss ) 
    if ( weapons[ weapon ] ) then 
        -- We found that weapon from the table 
    else 
        -- We did not find that weapon from the table 
    end 
end 

Link to comment

Oh yeah i got it, Thank You for your Petiance and for clearing some basic things about tables.

But still whats the difference between

weapons = {1,2,3} 
 

and

weapons={[1] = true,[2]=true,[3]=true} 
 

2) What happens if i replace 'true' with 'abcd' ,'false' ?

3) What really table.sort do ? how to use it ?(As i saw that thing in many functions)

Link to comment

The difference is that the code I gave you sets 2, 3, 4, etc. as keys.

If I made it like you showed, it would not work because I am comparing keys, not values.

local myTable = { 
    [ 1 ] -- This is a key 
    = true -- This is a value 
} 
  
-- Can we find an item with key 1 or not 
if ( myTable[ 1 ] ) then  
    -- ... 
end 

So, to do something like that you have to use a function that instead of checking keys, it checks for values. The second example of mine above shows the function and how it works (isValueInTable).

And yes, you can put it to whatever you want, however, you should not put it to false or nil. Otherwise it will return else in that if-statement, because it expects a true/accessible value. If you wanted to disable weapon slot 5, then switch [ 5 ] = true to [ 5 ] = false.

Table.sort sorts the table values alphabetically by default, so if you had values: 9, 2, 5, 7, 3 - it would return them as 2, 3, 5, 7, 9. You can also do this in reverse or do more HC stuff by adding your own custom function as the third argument.

Link to comment

Hurray, i got that table.sort and '1 = true' thing but still why i have to use

myTable = {[1] = true} 
 

If i use

myTable= {1,2,3,4} 
 

then i call it as myTable[3] it should return '3'. Am I Correct ?

And if I use,

myTable={[1]=true} 
 

then when i call myTable[1], It should return 'true' then it will be something like

function someThing() 
if true then -- as myTable[1] value is true. 
.... 
end  
end 
 

It is so confusing!

Link to comment

And if I use,

myTable={[1]=true} 

then when i call myTable[1], It should return 'true' then it will be something like

function someThing() 
if true then -- as myTable[1] value is true. 
.... 
end  
end 

It is so confusing!

What is confusing about it? myTable[1] will return the value of myTable at index 1. If it is 'true', then it will return true. But tables can be changed easly!

For example, if you don't want to allow players to use the same skinf you could use something like this:

local skins = {} 
  
function setSkin ( player, skinID ) 
    if skins [ skinID ] then 
        outputChatBox ( "This skin is already in use!" ) 
    else 
        skins [ getElementModel ( player ) ] = false -- mark old skin as unused 
        skins [ skinID ] = true -- mark new skin as used 
        setElementModel ( player, skinID ) 
    end 
end 
-- note: this is just an example, not a really useful and perfect script 
  

And you can extend it by adding options to it by using tables. A hardcoded "if true then" can't be changed by a script but a table value can be (or even manually by editing the file), so this is the difference between "if true then" and "if myTable[1] then"

local skins = {} 
local options = { 
    [ "skinChange" ] = false 
} 
  
function setSkin ( player, skinID ) 
    if options.skinChange then -- or options["skinChange"], but using . (dot) is a bit more efficient 
        if skins [ skinID ] then 
            outputChatBox ( "This skin is already in use!", player ) 
        else 
            skins [ getElementModel ( player ) ] = false -- mark old skin as unused 
            skins [ skinID ] = true -- mark new skin as used 
            setElementModel ( player, skinID ) 
        end 
    else 
        outputChatBox ( "Sorry, changing skin is not allowed!", player ) 
    end 
end 
-- note: this is just an example, not a really useful and perfect script 

Link to comment

:?

I think you have increased my confusing by not directly answering my question.

Well, I was asking if i create myTable={[1]=true} and then make a following script.

function someThing()

if myTable[1] then

...

end

end

It will replace myTable[1] with true (As assigned value) and all numbers will be allowed (not only 1) as there is 'if true then'

did you understand ?

Link to comment

To your earlier post: yes, you got the idea.

The reason for doing the square bracket thing is if you have multiple different numbers like 2, 5, 8, if you now call myTable[ 5 ], it will return nil, because there is no key for that. So you'd always have to know the key beforehand you do your stuff, that's when it's useful to assign values as keys instead of the standard opposite.

If you don't assign a key for each value, the keys would be automatically assigned from 1 and upwards. So that's why it's not practical to use it to detect if a weapon ID is found in your table since it would now return invalid data each time relative to the table's default indexing from 1 and upwards.

Edited by Guest
Link to comment

I think understand something from that but don't know for sure.

That is enough for now, i will practice it and make my understanding more clear.

Thanks guys for helping and teaching me basics of tables.

Special thanks to myonlake for spending(hope it isnt wasted) his time for me. :D

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...