Simone Rene
Simone Rene

Reputation: 25

Beginner with Lua looping function

I am new to lua, just started last night. I have written a script for my games and I found its not operated.

There are three functions in my script, they are function: function shoot(), function pickplayer() and function drinkwater().

  1. running the function shoot() non-stop for 35 seconds
  2. and then running function pickplayer() right after the function shoot() completed.
  3. Looping step 1 and 2 for 3 times
  4. then run the function drinkwater()

I have run 3 of these functions individually and it worked fine, but when i combined them together, the script just not working as I wish. As it is not able to running the function shoot() non-stop for 35 seconds before it jumped to other functions

That would be much appreciate if you guys could give me a hand and point out where the problems are, so that i can move forward.

I know it might be too dumb to ask such stupid questions, but for a non-computering person, your help would gain me huge improvement

Please see the script below

DESCRIPTION=" first script";
function shoot()
    while true do
        x, y = findImage("/mnt/sdcard/target1.bmp");
        x1, y1 = findImageFuzzy("/mnt/sdcard/b7.bmp", 80);
        if x1 ~= -1 == x ~= -1 and y1 ~= -1 == y ~= -1 then 
            touchDown(0, x, y);
            touchUp(0)          
        end
        coroutine.yield();      
    end
end


function pickplayer()
    while true do 
        if findImage("/mnt/sdcard/df.bmp") then
            touchDown(0, 355, 783)
            touchUp(0)
            mSleep(500);
            touchDown(0, 188, 203)
            touchUp(0)
            mSleep(500);
            touchDown(0, 196, 196)
            touchUp(0)
            mSleep(500);                        
        end
        mSleep(500);
        coroutine.yield();
    end         
end


function drinkwater()
    while true do
        if findImage("/mnt/sdcard/noenoughwater.bmp") then
            touchDown(0, 228, 479)
            touchUp(0)
            mSleep(2000);
            touchDown(0, 178, 223)
            touchUp(0)
            mSleep(2000);
            touchDown(0, 222, 604)
            touchUp(0)
            mSleep(2000);
            touchDown(0, 180, 218)
            touchUp(0)
            mSleep(3000);           
        end
        coroutine.yield();      
    end
end


function main()
    co1 = coroutine.create(shoot);
    co2 = coroutine.create(pickplayer);  
    co3 = coroutine.create(drinkwater); 

    while true do
        local timeToRun = 35000
        local initialTime = os.time()
        local timeElasped=os.difftime(os.time(), initialTime)
        while timeElasped < timeToRun do
            coroutine.resume(co1)
            timeElasped =os.difftime(os.time(), initialTime)
            mSleep(2000);   
            coroutine.resume(co2);
            coroutine.resume(co3);      
        end
    end     
end

Upvotes: 2

Views: 830

Answers (1)

Oliver
Oliver

Reputation: 29621

All the touchDown and ups and sleeps are noise, the issue here is "scheduling" of the coroutines. You basically have this:

function condition1() return true end -- for testing
function condition2() return true end -- for testing


function shoot()
    while true do
        -- do stuff, then:
        coroutine.yield()
    end
end


function pickplayer()
    while true do 
        if condition1() then
            -- do stuff, then:
            mSleep(1500)                       
        end
        mSleep(500)
        coroutine.yield()
    end         
end


function drinkwater()
    while true do
        if condition2() then
            -- do stuff, then:
            mSleep(9000)       
        end
        coroutine.yield() 
    end
end


function main()
    co1 = coroutine.create(shoot)
    co2 = coroutine.create(pickplayer)
    co3 = coroutine.create(drinkwater)

    while true do
        local timeToRun = 35000
        local initialTime = os.time()
        local timeElasped=os.difftime(os.time(), initialTime)
        while timeElasped < timeToRun do
            coroutine.resume(co1)
            timeElasped =os.difftime(os.time(), initialTime)
            mSleep(2000)
            coroutine.resume(co2)
            coroutine.resume(co3)  
        end
    end     
end

The above will do this:

  1. run the function shoot()
  2. and then run function pickplayer()
  3. and then run function drinkwater()
  4. Looping steps 1 to 3 forever

However you say you want to achieve this:

  1. run the function shoot() non-stop for 35 seconds
  2. and then run function pickplayer()
  3. Loop step 1 and 2 for 3 times
  4. then run the function drinkwater()

This would require your main to do the following (not that repeat-until is nicer here than while-do-end):

function main()
    co1 = coroutine.create(shoot)
    co2 = coroutine.create(pickplayer)
    co3 = coroutine.create(drinkwater)

    for i=1,3 do -- do the following 3 times:
        -- shoot for 35 seconds non-stop
        local timeToRun = 35000
        local initialTime = os.time()
        local timeElasped = 0
        repeat 
            coroutine.resume(co1)
            timeElasped = os.difftime(os.time(), initialTime)
        until timeElasped < timeToRun 

        -- mSleep(2000): don't need this

        coroutine.resume(co2) -- pickplayer
    end     
    coroutine.resume(co3)  
end

Upvotes: 1

Related Questions