Lua: setpos loop & sleep()

User avatar
RabbiBob
Member
 
Posts: 335
Joined: Sat Jan 28, 2012 22:40

Lua: setpos loop & sleep()

by RabbiBob » Thu Aug 30, 2012 10:18

For a project I am attempting to force the player to teleport through a set of map coordinates and I was using a for loop to accomplish going through the range. I'd like to pause between hops to allow for the game engine to render the area if it is undiscovered, but I'm running into an issue that I'm not sure if it's teleport or the lack of a "sleep" function.

When I run the below script, the minetest.chat appears in the hud and increments, showing the loop is going through the sequence but the player never moves until it reaches the last coords by way of the loop. I've tried an extended counting for loop after setpos to slow it down, but I'm not sure that is having the effect I want either (does doing this cause the engine to be tied up in a way that even though I'm injecting 5 seconds of pause, nothing else occurs?). The CPU activity goes through the roof when I do a triple nested for (x,y,z) so I know that it is computing.

Bottom line: the for loop is looping, but the player doesn't seem to be moving until the loop exits. Enabling the position (F5) confirms that while the loop is reporting xpos increasing, the player coords remain static until the final.

What I'm looking for:

  • Can someone give me a working example of sleep
  • Does setpos need special care in some way


Thanks,
Bob

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
print '[MAPTELE]LOADED'

minetest.register_chatcommand('MAPTELE',{
    description = 'Begin Teleport Routine',
    privs = {privs=false},
    func = function(name, params)
        local player = minetest.env:get_player_by_name(name)
        zpos=8000
        ypos=20
        for xpos = -2000, 2000, 50 do
                player:setpos({x=xpos,y=ypos,z=zpos})
                minetest.chat_send_player(name, 'Teleported to '..xpos..','..ypos..','..zpos..'')
                    --sleep(500)
        end
    end
})
 

celeron55
Member
 
Posts: 430
Joined: Tue Apr 19, 2011 10:10

by celeron55 » Thu Aug 30, 2012 12:24

You can't sleep that way in callback-driven systems like Minetest's API. The server is "stopped" always when called code is running. To put it simply, the called code is just part of a big loop in the server that handles many things.

Instead of what you have now, you can for example make a chain of minetest.after() callbacks, storing state in the parameter.
 

User avatar
Echo
Member
 
Posts: 121
Joined: Tue Jul 31, 2012 08:11

by Echo » Thu Aug 30, 2012 13:04

Maybe it's possible to modify your loop in a state-machine.
Depending on the players position, the next position is calculated. The next time your function is called, the new position leads to another and so on.
 


Return to WIP Mods

Who is online

Users browsing this forum: No registered users and 9 guests

cron