Scenario scripting best practices

I'm just getting into scripting scenarios, and I'm curious about "best practices" for scripting scenarios. One thing that comes to mind is the update() function. The online docs might lead a beginner to stuff everything in the update() function, but it feels like this would eventually lead to performance issues, since it runs 60x per second.

Has anyone developed some core libraries to help with scenario scripting, like LUA coroutines that would help keep the scenarios more performant and organized? To be fair, I haven't looked in depth at the pre-installed scenario scripts, so maybe they already have this sort of thing.

Comments

  • Hey Jonathan.

    I have not checked into the forums for some time, but I might help even though it might be too late.

    Using coroutines is not possible, but you can use some functional programming magic, that makes maintaining scenarios much easier and less error-prone.

    Lively Epsilon is what I wrote and use for my scenarios. Check its extensive documentation and see if you find it helpful. There are some very basic building blocks and some very specialized functions.

    Cron

    You will find Cron (Reference, Source, Spec) very helpful as it solves the problem of calling expensive checks too often.

    function init()
        -- player = ...
        -- station = .... 
    
        Cron.regular(function(self)
            if distance(player, station) > 20000 then
                player:addToShipLog("You did not guard the station as you were supposed to.")
                victory("Kraylor")
            end
        end, 1)
    end
    
    function update(delta)
        Cron.tick(delta)
    end
    

    Cron is very simple internally: It only checks if the time has come to run the function again (basically comparing two numbers) which makes a big difference if the operation itself is rather expensive (like checking for close enemies, etc).

    Cron is used internally in Lively Epsilon almost anywhere and you can also check the scenarios I use it in.

    As for performance: If you fear performance problems, hit F11 in Empty Epsilons Server to check how slow your script is. But I have not seen any issues there with a rather extensive scenario.

    EventHandler

    The other helpful component I use almost everywhere is the EventHandler (Reference, Source, Spec). Together with Cron it helps decoupling your code. It also helps not having to check for conditions in your scenario constantly, but have it all event based.

    Organizing

    You can use require to include other lua files. I use it heavily to organize aspects of the scenario. It works like a charm.

    Closing Words

    I hope this helps. Have fun scripting, the API is quite powerful and it is always great to see new scenarios.

Sign In or Register to comment.