Announcement

Collapse
No announcement yet.

Script Docu v1

Collapse
This is a sticky topic.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script Docu v1

    Avalonia Online & Co is finally gaining scripting power. Right now it's very limited, but with the time it should be very powerful :-)

    Client-side scripting: not yet supported

    Server-side scripting of NPCs:

    The server engine is mostly written in Javascript. However these server engine scripts have unlimited access to everything, including files and database. To make it possible to embed scripts in maps and guarantee a stable server, the scripts of NPCs must be sandboxed and limited in what they can do and how much resources they take.

    The goal is to enable those NPC scripts to do everything that the engine scripts can do, but in a secure way and with an easy-to-use API. This is work in progress and the script engine might look very different in the future, but we have to start somewhere and progress. Ideas and feature requests are very welcome!

    Script events:

    When certain things happen, such as the player colliding (touching) with the NPC, then the script of the NPC is started and can react to the collision event. The current event name is specified as a global boolean variable, which you can check like "if (playersays)"

    Following events exist right now:
    playertouchsme - the player collides with the NPC
    playersays - a player is in a distance of around 300 pixels or less, and is chatting
    mousedown - a player taps or clicks on the NPC

    Objects and global variables

    Following objects and variables exist. These are read-only. You can additionally set attributes of the objects and call functions.
    player:
    player.id: integer, the unique player id
    player.name: string
    player.chat: string
    player.onlinetime: integer, seconds of playing the game
    player.kills, player.streak, player.mobkills, player.sparwins: integer
    player.adminlevel: integer
    player.hp, player.maxhp: integer, hitpoints and max hitpoints
    player.weapon, player.armor, player.head, player.hat: string, equipped weapon and look

    You can add custom variables to the player object (player.myvar = 123). These are kept until the player logouts. If you want to do permanent modifications to the player, then you have to use functions.

    Functions:

    say(text:string [,delay:number]) - shows text, by default for 5 seconds but you can optionally specify the number of seconds
    setimg(image:string) - changes the image of the NPC, must include the file extensions and exist in the npcs/ folder of the Files project
    setani(ani:string) - sets the animation (.bani file) of the NPC, without file extension

    player.hasitem(itemid:string): boolean - tells you if the player has a certain item
    player.additem(itemid:string): boolean - the item must be configured with "addbyscript":true and "addinmaps":["mapofnpc"]
    player.setani(ani:string) - shows an animation until the player moves again, filename without file extension
    player.showurl(url:string) - opens the URL in a new window, currently only works on mobile because of popup blocker
    player.showmessage(message:string) - shows a message at the top of the screen
    player.setmap(map:string, template:string, x:number, y:number) - moves the player, do ("outside", "unknown", 0, 0) to move out of a house
    player.unstick() - unsticks the player

    Example:

    This adds a firework once you say three times "yes". The firework1 item must have the attributes "addbyscript":true and "addinmaps":["currentmapname"].

    PHP Code:
    if (playertouchsme) {
        
    say("Say 'yes' three times if you want to get a firework!");
    }

    if (
    playersays && player.chat == "yes") {
        
    player.saycounter = (player.saycounterplayer.saycounter+1);
        if (
    player.saycounter 3)
            
    say("Say 'yes' " + (3-player.saycounter) + " more time(s)!");
        else {
            
    player.saycounter 0;
            if (
    player.hasitem("firework1"))
                
    say("You already have that!");
            else if (
    player.additem("firework1"))
                
    say("There it is!");
            else
                
    say("There was a problem adding the item!");
        }


    Screenshot of an NPC in tiled (map editor):

    Click image for larger version  Name:	scripting.png Views:	1 Size:	112.5 KB ID:	3372
    Last edited by Stefan; 06-03-2018, 05:29 PM.
    Also known as Daddy and bomber

  • #2
    Cool Script

    Comment


    • #3
      Very sweet
      Looking forward to watch the baby grow up

      Comment


      • #4
        Very very nice. I can't wait to see the progress on this over the years! Thank you. =]
        Corleone Online Manager/Owner

        Comment


        • #5
          Good to see.

          Comment


          • #6
            Added a screenshot of an NPC in tiled If you have any ideas, functions or functionality you need for your projects then let me know. Will probably soon add a way to call external URLs (Webhooks).
            Also known as Daddy and bomber

            Comment


            • #7
              Is this compiled or ran as raw Javascript?

              Comment


              • #8
                Running with the same v8 engine, basically just the "global" (environment) object is different. Script execution is limited to 10 milliseconds at this time.
                Also known as Daddy and bomber

                Comment


                • #9
                  you can script in tiled? what?

                  Comment


                  • #10
                    Originally posted by Ximithie View Post
                    you can script in tiled? what?
                    You can make NPC-objects in Tiled and add custom parameters to it. That's all it is.

                    Comment


                    • #11
                      Added some more functions :-)
                      Also known as Daddy and bomber

                      Comment


                      • #12
                        Awesome . 😮

                        Comment

                        Working...
                        X