Lua chatbox
-
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
Hello again may i ask if this was an issue
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.
-
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
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
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
...
Just my opinion
retired
retired
-
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
Ohh wait @Ølsken what's the role of "local number = 12" there based on my sent script?
-
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
Cityrjroldan1 wrote: 22 Jun 2020, 13:02 Ohh wait @Ølsken what's the role of "local number = 12" there based on my sent script?
Just my opinion
retired
retired
-
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
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”
CEO of Haráya Motors
“TheoTown Philippines Council”
“Kapulungan Ng Mga Nagkakaisang Lungsod”
-
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
City.getRegionName() returns a string if the city is in a region and nil if not.rjroldan1 wrote: 23 Jun 2020, 02:54 Anyway how can i make Building only buildable when Region name returns true?
Lua:
Code: Select all
if City.getRegionName() then
-- Passes if the city is in a region (any)
end
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",
...
}
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
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
retired
-
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
Other questions....
Is it really posible to save the value of a variable even the player exits the game?
Is it really posible to save the value of a variable even the player exits the game?
-
Lobby
- Inhabitant of a Supercluster
- Reactions:
- Posts: 3761
- Joined: 26 Oct 2008, 10:34
- Plugins: Showcase Store
Re: Lua chatbox
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.
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.
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
Limitations
You cannot save functions in a storage table persistently.
-
CERRERA
- Townsman
- Reactions:
- Posts: 48
- Joined: 25 Jan 2020, 04:19
- Location: Philippines
- Plugins: Showcase Store
- Version: Beta
-
Plugin Creator
Platform
Re: Lua chatbox
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)
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
-
Temgamer
- Villager
- Reactions:
- Posts: 12
- Joined: 16 May 2017, 18:26
- Location: Saint-Petersburg, Russia
- Plugins: Showcase Store
Re: Lua chatbox
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 greatDESOLAN 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)
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: 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
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.
-
Temgamer
- Villager
- Reactions:
- Posts: 12
- Joined: 16 May 2017, 18:26
- Location: Saint-Petersburg, Russia
- Plugins: Showcase Store
Re: Lua chatbox
How to set a console command with TheoTow.registerCommand()? I've tried putting it in script:init() and it doesn't work
-
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
Use TheoTown.registerCommand(string, function)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
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]
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]
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]
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"
You do not have the required permissions to view the files attached to this post.
Just my opinion
retired
retired
-
Temgamer
- Villager
- Reactions:
- Posts: 12
- Joined: 16 May 2017, 18:26
- Location: Saint-Petersburg, Russia
- Plugins: Showcase Store
Re: Lua chatbox
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