Lua chatbox

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

Moderator: Plugin Moderators

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#41

Post by Hadestia »

Lobby wrote: 21 Jun 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
User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

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.
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#43

Post by Hadestia »

It works well now Thanks for your help also :)

Hope this conversation would help others too :)
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#44

Post by Hadestia »

Hello again may i ask if this was an issue
IMG_20200622_174421.jpg
IMG_20200622_174421.jpg (10.8 KiB) Viewed 5689 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: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#45

Post by Bearbear76 »

Long? Can you show the code?
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#46

Post by Hadestia »

Ølsken wrote: 22 Jun 2020, 12:46 Long? Can you show the code?
Lets go PM
User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#47

Post by Bearbear76 »

rjroldan1 wrote: 22 Jun 2020, 13:23
Ølsken wrote: 22 Jun 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 ;)
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#48

Post by Hadestia »

But thats still need lots of elseif conditions


Ok I get it thanks :)
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#49

Post by Hadestia »

Ohh wait @Ølsken what's the role of "local number = 12" there based on my sent script?
User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#50

Post by Bearbear76 »

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

Plugin Creator

Platform

Re: Lua chatbox

#51

Post by Hadestia »

Anyway how can i make Building only buildable when Region name returns true?
User avatar
ian`
Supporter
Reactions:
Posts: 117
Joined: 04 Apr 2020, 17:36
Location: Indonesien
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#52

Post by ian` »

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

Plugin Creator

Platform

Re: Lua chatbox

#53

Post by Lakan Haraya »

distian wrote: 23 Jun 2020, 06: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
User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#54

Post by Bearbear76 »

rjroldan1 wrote: 23 Jun 2020, 04: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
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#55

Post by Hadestia »

Thanks
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#56

Post by Hadestia »

Other questions....

Is it really posible to save the value of a variable even the player exits the game?
User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
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
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#58

Post by Hadestia »

Love it :)
User avatar
CERRERA
Townsman
Reactions:
Posts: 48
Joined: 25 Jan 2020, 06:19
Location: Philippines
Plugins: Showcase Store
Version: Beta
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#59

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)
User avatar
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 20:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: Lua chatbox

#60

Post by Temgamer »

DESOLAN wrote: 26 Jun 2020, 07: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 26 Jun 2020, 23:02, edited 1 time in total.
Post Reply Previous topicNext topic

Return to “Lua Scripting”