Lua chatbox

The Lua scripting language allows you to give your plugin more advanced features.

Moderator: Plugin Moderators

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#41

Post by rjroldan1 »

Lobby wrote:
Sun Jun 21, 2020 14:54

Code: Select all

...
local _, rank, var2

function script:update()
  _, rank = City.getRank()
end

local var = function()
  var2 = rank * 1
end
...
I you define a variable as local in a function it won't be accessible from outside of that function :teach

Oh ok i get it this is just like getting the 2nd return info. To store at second variable
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#42

Post by Bearbear76 »

Hmm a lot of misplaced locals.

Local variables are local to their block no one can access them outside.
These are the basic blocks:

Code: Select all

if condition then
  -- If block
end

for i = 1, 100 do
  -- For block
end

while condition do
  -- While block
end

function foo()
  -- Function block
end
For example:

Code: Select all

local a = 12

if condition then
  local b = 13
end

local c = 20
function foo()
  c = 50
end

--> local a: number = 12
--> nil
--> local c: number = 50
As you can see the variable b cannot be accessed because it's only visible to it's own block (if statement).
But we can see c as it was declared outside of any block, though it was re-declared inside a block it still is local to the original declaration.
ImageJust my opinion

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#43

Post by rjroldan1 »

It works well now Thanks for your help also :)

Hope this conversation would help others too :)
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#44

Post by rjroldan1 »

Hello again may i ask if this was an issue
IMG_20200622_174421.jpg
IMG_20200622_174421.jpg (10.8 KiB) Viewed 1105 times
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 ↑?

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#45

Post by Bearbear76 »

Long? Can you show the code?
ImageJust my opinion

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#46

Post by rjroldan1 »

Ølsken wrote:
Mon Jun 22, 2020 12:46
Long? Can you show the code?
Lets go PM
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#47

Post by Bearbear76 »

rjroldan1 wrote:
Mon Jun 22, 2020 13:23
Ølsken wrote:
Mon Jun 22, 2020 12: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 ;)
ImageJust my opinion

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#48

Post by rjroldan1 »

But thats still need lots of elseif conditions


Ok I get it thanks :)
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#49

Post by rjroldan1 »

Ohh wait @Ølsken what's the role of "local number = 12" there based on my sent script?
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#50

Post by Bearbear76 »

rjroldan1 wrote:
Mon Jun 22, 2020 15:02
Ohh wait @Ølsken what's the role of "local number = 12" there based on my sent script?
City
ImageJust my opinion

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#51

Post by rjroldan1 »

Anyway how can i make Building only buildable when Region name returns true?
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
ian`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#52

Post by ian` »

I want to ask, how to call lua from a different directory?
:fire

User avatar
Lakan Haraya
Metropolitan
Reactions:
Posts: 117
Joined: Thu Mar 28, 2019 9:34
Location: Rizal, Philippines
Plugins: Show
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#53

Post by Lakan Haraya »

distian wrote:
Tue Jun 23, 2020 6: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”

Pinoy? Sali na :jb:
https://discord.gg/9n7PFy

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#54

Post by Bearbear76 »

rjroldan1 wrote:
Tue Jun 23, 2020 4: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
ImageJust my opinion

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#55

Post by rjroldan1 »

Thanks
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#56

Post by rjroldan1 »

Other questions....

Is it really posible to save the value of a variable even the player exits the game?
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
Lobby
Developer
Reactions:
Posts: 3610
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

Platform

Re: Lua chatbox

#57

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
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#58

Post by rjroldan1 »

Love it :)
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
DESOLAN
Villager
Reactions:
Posts: 18
Joined: Sat Jan 25, 2020 6:19
Plugins: Show

Re: Lua chatbox

#59

Post by DESOLAN »

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)

Temgamer
Villager
Reactions:
Posts: 12
Joined: Tue May 16, 2017 20:26
Location: Saint-Petersburg, Russia
Plugins: Show

Re: Lua chatbox

#60

Post by Temgamer »

DESOLAN wrote:
Fri Jun 26, 2020 7: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
Last edited by Temgamer on Fri Jun 26, 2020 23:02, edited 1 time in total.

Post Reply Previous topicNext topic

Return to “Lua Scripting”