Jump to content

How to check if argument is empty?


Recommended Posts

  • Moderators
arg1, arg2, ...: Each word after command name in the original command is passed here in a seperate variable. If there is no value for an argument, its variable will contain nil. You can deal with a variable number of arguments using the vararg expression, as shown in Server Example 2 below.

https://wiki.multitheftauto.com/wiki/AddCommandHandler

 

if argument then -- check
  
end

 

@Hugo_Almeidowski

 

  • Like 1
Link to comment
5 minutes ago, IIYAMA said:

arg1, arg2, ...: Each word after command name in the original command is passed here in a seperate variable. If there is no value for an argument, its variable will contain nil. You can deal with a variable number of arguments using the vararg expression, as shown in Server Example 2 below.

https://wiki.multitheftauto.com/wiki/AddCommandHandler

 


if argument then -- check
  
end

 

@Hugo_Almeidowski

 

So, if argument = nil then

 

end

 

Right?

Thanks!

Link to comment
45 minutes ago, Hugo_Almeidowski said:

So, if argument = nil then

 

end

 

Right?

Thanks!

if argument then
  
end

represents

if argument ~= nil then
  
end

 

If you'd like to check if it's nil:

if not argument then
  -- Do something
end

-- or

if argument == nil then
  -- Do something
end

 

EDIT: It doesn't matter which of the two methods you choose. As far as I know, the common code style uses

if not element then
  -- Do something
end
-- etc.

 

Edited by Sorata_Kanda
  • Like 1
Link to comment
8 minutes ago, Sorata_Kanda said:

if argument then
  
end

represents


if argument ~= nil then
  
end

 

If you'd like to check if it's nil:


if not argument then
  -- Do something
end

-- or

if argument == nil then
  -- Do something
end

 

EDIT: It doesn't matter which of the two methods you choose. As far as I know, the common code style uses


if not element then
  -- Do something
end
-- etc.

 

But for what I've been coding it seems that

if argument then



end

means that if argument == true (is valid) then

so what if I want to check if the argument is valid?

Link to comment
9 minutes ago, Hugo_Almeidowski said:

But for what I've been coding it seems that


if argument then



end

means that if argument == true (is valid) then

so what if I want to check if the argument is valid? 

It means that argument has a value assigned and that the value is different from false.

What you are looking for is:

if not argument or type(argument)~= "number" then
  argument = getPlayerDimension(player)
end

 

Edited by Zorgman
  • Like 1
Link to comment
6 minutes ago, Zorgman said:

It means that argument has a value assigned and that the value is different from false.

What you are looking for is:


if not argument or type(argument)~= "number" then
  argument = getPlayerDimension(player)
end

 

I've done it like this

if argument and argument2 and argument3 then
  
else if argument and argument2 then
    
else if argument then
      
else
      
end

 

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...