Jump to content

Newbie question about changing from function.


iPot

Recommended Posts

Should I use AddEvent/AddEventHandler here? Or are there other options. Like in GTA SA scripting you could just type JUMP @label. then I could use JUMP @otherFunction. It's not like that but that's why I came here, to ask.

function testFunction 
    testvariable = getTestData(aDude) 
    if (testvariable == true) then 
        -- go to otherFunction 
    end 
end 
  
function otherFunction 
    -- blablabla 
end 
  
bindKey(fire,both,testFunction) 

Already thanks.

iPot

PS: Nice tabbing for a newb huh? :D

Link to comment
-- we declare a vector ( table ) 
local aTest = 
{ 
    -- we create index 'username' and 'password' and set his value 
    [ 'username' ]      = 'Jayz'; 
    [ 'password' ]      = 123456; 
} 
  
-- we declare a function 
function dTestFunction ( ) 
    -- now we call function 'getTestData' and pass aTest vector and we allocate the return value in a var 
    local vTest     = getTestData ( aTest, 'Jayz', 123456 ) 
    -- if vTest return true 
    if ( vTest ) then 
        -- call function 'otherFunction' 
        otherFunction ( ) 
    end 
end 
  
function getTestData ( aVector, user, password ) 
    -- check if passed parameter aVector is a table 
    if ( type ( aVector ) == 'table' ) then 
        -- check if table user and pass are the same as parameters passed 
        if ( aVector['username'] == tostring ( user ) and aVector['password'] == tonumber ( password ) ) then 
            return true 
        -- else do that 
        else 
            return false  
        end 
    -- if it's not a table do that 
    else 
        return false 
    end 
    -- end function declaration 
end 
  
function otherFunction ( ) 
    -- prints Welcome on the screen ( print function don't work on MTA! ) 
    -- _G['print'] ( 'Welcome' ) = print ( 'Welcome' ) 
    _G['print'] ( 'Welcome' ) 
end 
  
-- 
  
bindKey ( 'fire', 'both', dTestFunction ) 

If you don't understand something just tell me.

This answer your question, I think.

So, you can call functions by just typing:

theFunction ( parameters separated by comma ) 

If there are any errors, please tell me.

Link to comment

Thank you very much :) It works perfectly, where is the tutorial that explains the part I missed apparently?

In theory this loop should work but it doesn't. How come? I defined variable2 and thePlayer btw.

function testFunction() 
    local variable = getVar(thePlayer) 
    outputChatBox("Debug message",thePlayer) 
    if (variable == 0 ) then 
        if (variable2 == false) then 
            outputChatBox("Debug message",thePlayer) 
            takeAction() 
            testFunction2() 
        end 
    end 
end 
  
function testFunction2() 
    local variable = getVar(thePlayer) 
    if (variable ~= 0) then 
        outputChatBox("Debug message",thePlayer) 
        takeAction2() 
        testFunction() 
end 
  
function takeAction() 
        -- Do things. 
        testFunction() 
end 
  
function takeAction2() 
        -- Do different things. 
        testFunction() 
end 
  
addCommandHandler("command", testFunction) 

EDIT: I don't even get the first debug message in testFunction()

Link to comment

here:

function testFunction(thePlayer,command) --You forgot the variables 
    local variable = getVar(thePlayer) 
    outputChatBox("Debug message",thePlayer) 
    if (variable == 0 ) then 
        if (variable2 == false) then 
            outputChatBox("Debug message",thePlayer) 
            takeAction() 
            testFunction2() 
        end 
    end 
end 
  
function testFunction2() 
    local variable = getVar(thePlayer) 
    if (variable ~= 0) then 
        outputChatBox("Debug message",thePlayer) 
        takeAction2() 
        testFunction() 
    end 
end 
  
function takeAction() 
        -- Do things. 
        testFunction() 
end 
  
function takeAction2() 
        -- Do different things. 
        testFunction() 
end 
addCommandHandler("command", testFunction) 

EDIT: I found my mistake, Copy again

Edited by Guest
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...