Scripting

Client script

The client script should be in project_folder/Scripts with the file name Hello.lua.

Client.width: Gets the width of the screen.

Client.height: Gets the vertical size of the screen.

Client.ShowAlert(text, callback): Prompts a notification message.

Client.ShowYesNoAlert(text, callback): Prompts a Yes/No message.

Client.ShowBuyItem(id, amount, callback): Prompts a message to purchase a specific item.

Client.Quit(): Closes the client.

ScriptEventPublisher Client.GetTopic(topic): Registers a specific topic to exchange messages with the server.

Client.FireEvent (topic, args ...): Sends a message to the server with a specific topic.

 

Client.RunLater(callback, t): Executes the callback function after t seconds.

Camera: The object that controls the camera
Point: Class containing x, y coordinates
Rect: Class containing a rectangle with x, y, width, height
Image: Class to output an image
Text: Class to output text
Slider: Class to output the controller
Button: Class to output buttons
Panel: Class to output the panel
ScrollPanel: Class to output a scrollable panel
GridPanel: Class to output a panel that is arranged as a grid


Server Script

The server script should be in project_folder/ServerScripts with the file name Hello.lua.

Server.onJoinPlayer(unit): A new user has connected.
Server.onLeavePlayer(unit): A certain user has left the game.
Server.SendCenterLabel(text): Displays the middle label.
Server.SendSay(text, color): Displays a message in the chat window.

ScriptEventPublisher Server.GetTopic(topic): Registers a specific topic to send messages to the client.
Server.FireEvent(topic, args…): Sends a message to the client with a specific topic.
Server.players: Gets a list of users.
Server.fields: Gets the entire map list.
Server.CreateEventUnit(name, imageID): Creates an event unit.
Server.GetField(dataID): Gets a map with a specific ID.
ScriptUnit: An object such as a monster or player.

Server.playerLeavePartyCallback(scriptRoomPlayer, scriptParty): Called when the player leaves the party.

Server.playerJoinPartyCallback(scriptRoomPlayer, scriptParty): Called when the player enters the party.

Server.onAddItem(scriptUnit, titem): Called when the user acquires an item.

Server.onRemoveItem (scriptUnit, titem): Called when the user's item is removed and used.

unit.type=Unit type (0=Player, 1=Event, 2=Monster)

e.g., Server Script – Provides item and displays a message on level up once a certain level has been reached

File: Scripts/Servers/Hello.lua

1 2 3 4 5 6 7 8 9 10 11 function onUnitLevelUp(target, level) if target.level == 50 then --If target reaches level 50, the whole server is notified 1 item is given. Server.SendCenterLabel(target.name .. 'is ' .. level .. ' has reached!') target.AddItem(1) elseif target.level == 55 then --If they have reached level 55, they will receive 5 skills and 5000 game money. target.AddSkill(5) target.AddGameMoney(5000) end end Server.onUnitLevelUp.Add(onUnitLevelUp)

e.g., Server script - When a user is killed in PVP, displays a server-wide message and gives a reward

File: Scripts/Servers/Hello.lua

1 2 3 4 5 6 7 8 9 10 function onUnitDead(target, attacker) --target is dead, attacker is an attacker if (target.type==0 and attacker.type==0) then --in the case of a PlayerUnit Server.SendCenterLabel(target.name..' objecet \n'..attacker.name..'kill') attacker.AddGameMoney(100) --Give the attacker 100 gold. if rand(1, 100) <= 50 then attacker.AddItem(100) --50% chance to give the attacker 1 item. end end end Server.onUnitDead.Add(onUnitDead) --Add the onUnitDead function to Server.onUnitDead.

How to use onJoinField – Displays name when entering a specific map, sends server-wide message

File: Scripts/Servers/Hello.lua

1 2 3 4 5 6 7 8 9 10 11 12 13 14 function onJoinField ()      function onJoinField_1 (Field,unit)          unit.SendCenterLabel (Field.name) -- displays the field name for a short time.          if # Field.playerUnits> 10 then -- where # is the length of the table.              Server.SendSay (Field.name .. "has more than 11 users !!")          end      end local map = Server.GetField(1) if map ~= nil then      Server.GetField (1) .onJoinField.Add (onJoinField_1) -- Invokes the onJoinField_1 function when entering field 1. end end Server.RunLater (onJoinField, 1) -- Executes the onJoinField function in 1 second. - The reason to run in 1 second is to prevent referencing nil because the map may not have been generated after the server is running.

onLeaveField - Handles when leaving a specific map

Factor map - ScriptField
Factor unit - ScriptUnit

File: Scripted on the map. e.g., Decrease the value of a global variable by 1 when leaving the map.

1 2 3 4 5 6 function onLeaveField(map,unit) print('Leave: ' .. map.name .. ' ' ..unit.name) Server.SetWorldVar(10, Server.GetWorldVar(10)-1) end Server.GetField(5).onLeaveField.Add(onLeaveField)

e.g., Item use (onUseItem), purchase (onBuyGameMoneyItem),

Events that occur during sales (onSellGameMoneyItem)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Server.onUseItem.Add( function(unit,item) print(unit.name) print(item.dataID) end) Server.onBuyGameMoneyItem.Add( function(unit,itemID,count) print(string.format("%s ,%s ,%s",unit.name,itemID,count)) end) Server.onSellGameMoneyItem.Add( function(unit,itemID,count) print(string.format("%s,%s,%s",unit.name,itemID,count)) end)

e.g., Item options, remove or change

Please test by deleting the comment (-) applied to the code. The script below ran as an event-script on touch.

1 2 3 4 5 6 7 -Get information about the first item in the inventory. local item = unit.player.GetItems () [1] local option1 = item.options [1] --Utility.SetItemOption (option1, option1.type, option1.statID, option1.value + 1) Utility.AddItemOption (unit.player.GetItems () [1], 1,3,5) --unit.player.RemoveItemOption (item, 1) unit.player.SendItemUpdated(item)

e.g., Event that occurs when entering a party (playerJoinPartyCallback)

1 2 3 4 5 6 7 Server.playerJoinPartyCallback = function(scriptRoomPlayer,scriptParty) print(scriptRoomPlayer.unit.name) print(scriptParty.maxPlayer) -- return must be true to create a party. You can specify return true, false depending on the condition. return true end

e.g., Event that occurs when leaving a party (playerLeavePartyCallback)

1 2 3 4 5 Server.playerLeavePartyCallback = function(scriptRoomPlayer,scriptParty) print(scriptRoomPlayer.unit.name) print(scriptParty.maxPlayer) end

e.g., Event on item acquisition (onAddItem)

1 2 3 4 5 Server.onAddItem.Add( function(scriptUnit,titem) print(scriptUnit.name) print(titem.dataID) end)

e.g., Event on item removal (onRemoveItem)

1 2 3 4 5 6 Server.onRemoveItem.Add( function(scriptUnit,titem) print(scriptUnit.name) print(titem.id) print(titem.dataID) end)

e.g., Event that occurs when dealing damage (damageCallback)

1 2 3 4 5 6 7 8 9 -- No damage display when damage is 0 -- a,b ScriptUnit Server.damageCallback = function(a, b, damage, skillDataID, critical, visible) damage = a.atk - b.def if damage <= 0 then visible = false end return damage, critical, visible; end

e.g., Event that occurs when a transaction is completed(onTradeDone)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 function Trade(sender,receiver,senderItems,receiverItems) print("sender: ") print(sender.unit.name) print("receiver: ") print(receiver.unit.name) print("senderItems: ") print(senderItems) for i=1 , #senderItems do print(i..": ") print(Server.GetItem(senderItems[i].dataID).name) end print("receiverItems: ") print(receiverItems) for j=1 , #receiverItems do print(j..": ") print(Server.GetItem(receiverItems[j].dataID).name) end end Server.onTradeDone.Add(Trade)

 

SUPERCAT Inc. All Rights Reserved.