Lua chatbox

The Lua scripting language allows you to give your plugin more advanced features.
Previous topicNext topic
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Hello again may i ask if this was an issue
IMG_20200622_174421.jpg
I used long "if, elseif " conditions and it works fine, I just want to ask if it is still valid even it has error like that ↑?
You do not have the required permissions to view the files attached to this post.
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

Long? Can you show the code?
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Ølsken wrote: 22 Jun 2020, 10:46 Long? Can you show the code?
Lets go PM
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

rjroldan1 wrote: 22 Jun 2020, 11:23
Ølsken wrote: 22 Jun 2020, 10:46 Long? Can you show the code?
Lets go PM
For documentation purposes. I will write it here.
Don't worry I won't leak any info.

I see that you're using a lot of elseif.
That's going to clutter the code try something more clean ;)

This does the same thing:

Code: Select all

local number = 12
local character
local alphabet = {
  "a", "b", "c", "d", "e", "f", "g", ...
}

for i = 1, #alphabet do
  if number == i then
    character = alphabet[i]
    break
  end
end

--> local character: string = l
This does the same thing just cleaner. also avoiding stacks of elseif elseif ....

Much cleaner than:

Code: Select all

local number = 12
local character

if number == 1 then
  character = "a"
elseif number == 2 then
  character = "b"
elseif number == 3 then
  character = "c"
elseif number == 4 then
...
I think it's simple enough so I won't explain how it works ;)
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

But thats still need lots of elseif conditions


Ok I get it thanks :)
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Ohh wait @Ølsken what's the role of "local number = 12" there based on my sent script?
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

rjroldan1 wrote: 22 Jun 2020, 13:02 Ohh wait @Ølsken what's the role of "local number = 12" there based on my sent script?
City
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Anyway how can i make Building only buildable when Region name returns true?
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
ian`
Metropolitan
Reactions:
Posts: 117
Joined: 04 Apr 2020, 15:36
Location: 日本
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

Post by ian` »

I want to ask, how to call lua from a different directory?
:bc
User avatar
Lakan Haraya
Metropolitan
Reactions:
Posts: 131
Joined: 28 Mar 2019, 07:34
Location: Philippines
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

Post by Lakan Haraya »

distian wrote: 23 Jun 2020, 04:18 I want to ask, how to call lua from a different directory?
Try this...

Code: Select all

"script":"../folderName/script.lua"
// 2 dots means you're leaving the folder, then enter to a new folder by its name, and will read the file
"ᜀᜃᜓ ᜀᜅ᜔ ᜁᜌᜓᜅ᜔ ᜑᜇᜌ"
CEO of Haráya Motors

“TheoTown Philippines Council”
“Kapulungan Ng Mga Nagkakaisang Lungsod”
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

rjroldan1 wrote: 23 Jun 2020, 02:54 Anyway how can i make Building only buildable when Region name returns true?
City.getRegionName() returns a string if the city is in a region and nil if not.

Lua:

Code: Select all

if City.getRegionName() then 
  -- Passes if the city is in a region (any)
end
Now we have something to determine if the City is in a region or not.

Next let's think how we would detected if a building is built.
Let's use the event function. In order to use the event function you
need to add the script to the building in question.

JSON:

Code: Select all

{
  "id": "$example_building",
  "script": "my_script.lua",
  ...
}
Now let's add another if statement to see if the building was placed.

Lua:

Code: Select all

function script:event(x, y, level, event)
  if event == Script.EVENT_PLACED and Tile.getBuildingDraft(x, y) == [draft] then
    if not City.getRegionName() then
      Builder.remove(x, y)
    end
  end
end
This code will first check if the building that was placed was the building and if so it checks if it's not in a region. If so it will remove the building.

the not keyword is basically checks if
the value is nil, false or 0.
Same thing as:

Lua:

Code: Select all

if foo == nil or foo == false or foo == 0 then
  -- Do something
end
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Thanks
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Other questions....

Is it really posible to save the value of a variable even the player exits the game?
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3759
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Lua chatbox

Post by Lobby »

Yes, there are tables that will be saved and restored when the player comes back.


Saving Lua data persistently
So called storage tables can be used to to store data in a persistent way.

City.getStorage()
Is saved within the city file, so it's useful to save city dependent stuff in it.

TheoTown.getStorage()
A single table that is stored globally for the game. So you could put things in here that should not be city dependent.

Usage
Since your plugin has to share the storage tables with all other exisiting plugins you may want some sort of private table within it. You can do that by using the helper function Util.optStorage() which manages creation, insertion and retrival of a table using a given key.

Code: Select all

-- Let's use a unique, own key for the table that we will create within a storage table
local key = script:getDraft():getId()

-- Let's get our personal storage table within the city storage table (will be created if it doesn't exist)
local table = Util.optStorage(City.getStorage(), key)

-- Let's put something into it; this should still be there after saving the city, ending the game, and then coming back
table.x = 42
There are no explicit save functions for the storage tables since they will be saved together with other data automatically.

Limitations
You cannot save functions in a storage table persistently.
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Love it :)
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
CERRERA
Townsman
Reactions:
Posts: 48
Joined: 25 Jan 2020, 04:19
Location: Philippines
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

Post by CERRERA »

May i ask?

How can i build a zones with a radius of 5 from the example building (as the center of the zone radius)
Everything you can imagine is real
T
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 18:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: Lua chatbox

Post by Temgamer »

DESOLAN wrote: 26 Jun 2020, 05:25 May i ask?

How can i build a zones with a radius of 5 from the example building (as the center of the zone radius)
I've wrote sort of function for that, it requires x and y of your building, ID of zone and radius. I tested it several times with different radiuses and it works great

Code: Select all

function buildZoneC(zeroX, zeroY, zoneId, radius)
    zeroX = zeroX - radius
    zeroY = zeroY - radius
    local offset = 0
    Builder.buildZone(Draft.getDraft(zoneId), zeroX + offset, zeroY)
    for i=1, (radius * 2 + 1)^2 - 1 do
    if i % (radius * 2 + 1) == 0 then
      zeroY = zeroY + 1
      offset = 0
    else
      offset = offset + 1
    end
    Builder.buildZone(Draft.getDraft(zoneId), zeroX + offset, zeroY)
    end
end
Code underneath runs it, when player builds a building with <id>:

Code: Select all

function script:event(zeroX, zeroY, level, event)
  if event == Script.EVENT_PLACED and Tile.getBuildingDraft(zeroX, zeroY):getId() == "<id> then
    buildZoneC(zeroX, zeroY, "$zonecommercial", 2)
  end
end
Result of build:
Ahotee_20-06-26_23.12.47.png
It also places zone under that building, btw
You do not have the required permissions to view the files attached to this post.
Last edited by Temgamer on 26 Jun 2020, 21:02, edited 1 time in total.
T
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 18:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: Lua chatbox

Post by Temgamer »

How to set a console command with TheoTow.registerCommand()? I've tried putting it in script:init() and it doesn't work
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

Temgamer wrote: 26 Jun 2020, 20:49 How to set a console command with TheoTow.registerCommand()? I've tried putting it in script:init() and it doesn't work
Use TheoTown.registerCommand(string, function)
If you return a value from the function that would be displayed on the console.

Code: Select all

TheoTown.registerCommand("hello!", function()
  local number = 11 + 1
  return "Hello, the magic number is: "..number
end)

-- output: Hello, the magic number is: 12 [string]
You could also achieve the same thing with:

Code: Select all

local function hello()
  local number = 11 + 1
  return "Hello, the magic number is: "..number
end

TheoTown.registerCommand("hello!", hello())

-- output: Hello, the magic number is 12 [string]
スクリーンショット (6).png
If you want to enable it and disable it add a if statement or something to check if
a condition is true, if so run the command.

Code: Select all

local password = "something"

TheoTown.registerCommand("hello!", function()
  if password == "olsken" then
    local number = 11 + 1
    return "Hello, the magic number is: "..number
  end
end)

--- (console) "hello" > [nil]
--- password = olsken [string] 
--- (console) "hello" > Hello, the magic number is: 12 [string]
*Note*
Command names don't override existing command names.
For example, the command above is "hello!" but if I change it to "hello" you would get
a different result as TheoTown already has the prebuilt command "hello" would would say "42"

:jb: bear out!
You do not have the required permissions to view the files attached to this post.
Just my opinion
retired
T
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 18:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: Lua chatbox

Post by Temgamer »

Also, is it possible to make console command require arguments? I tried TheoTown.registerCommand("testt", function(arg)) but this seems doesn't work how I expected
Previous topicNext topic