Lua chatbox

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

Moderator: Plugin Moderators

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

Re: Lua chatbox

#61

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
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#62

Post by Bearbear76 »

Temgamer wrote:
Fri Jun 26, 2020 22: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
スクリーンショット (6).png (79.21 KiB) Viewed 1129 times
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!
ImageJust my opinion

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

Re: Lua chatbox

#63

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

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

#64

Post by Bearbear76 »

Temgamer wrote:
Sat Jun 27, 2020 0:53
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
I don't think there are any arguments. What do you want to do?
ImageJust my opinion

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

Re: Lua chatbox

#65

Post by Temgamer »

Bearbear76 wrote:
Sat Jun 27, 2020 6:36
Temgamer wrote:
Sat Jun 27, 2020 0:53
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
I don't think there are any arguments. What do you want to do?
I just wanted to make a command, which would set radius and zone around some building by taking radius and zone ID as parameters

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

#66

Post by Bearbear76 »

Temgamer wrote:
Sat Jun 27, 2020 8:08
Bearbear76 wrote:
Sat Jun 27, 2020 6:36
Temgamer wrote:
Sat Jun 27, 2020 0:53
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
I don't think there are any arguments. What do you want to do?
I just wanted to make a command, which would set radius and zone around some building by taking radius and zone ID as parameters
Tried something like that but it doesn't work. That's why I'm trying to create a terminal in TheoTown which gives you more powerful commands but for personal reasons I can't work on it for a while :/
ImageJust my opinion

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

#67

Post by ian` »

I just wanted to make a command, which would set radius and zone around some building by taking radius and zone ID as parameters
Try to just change the condition variables with command and call that variables in outside of register command function.

Code: Select all

local c1 = 0

function script:update()
TheoTown.registerCommand(name,function(name,arg)
if c1 == 0 then 
c1 = 1
Debug.toast('On')
elseif c1 == 1 then
c1 = 0
Debug.toast('Off')
end
end)
end

function script:click(x,y,level)
if c1 == 1 then
Your stuff ...
end
end
:fire

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

#68

Post by ian` »

Lobby wrote:
Tue Jun 23, 2020 17:24
Why in online i can't save the data to storage and load it back when restart the games?

And what is possible to save data on the city and city visitors load that data what we save before?
:fire

User avatar
Löbby
enjenir
Reactions:
Posts: 2982
Joined: Sun Jul 23, 2017 12:45
Location: Easter Island
Plugins: Show

Re: Lua chatbox

#69

Post by Löbby »

All scripts or fun transitions are disabled for cities in online mode unless you are the city author and have plugin manifest for your script.

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

#70

Post by ian` »

is there a function to separate thousands or millions with ","? like 1,000,000
:fire

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

#71

Post by Bearbear76 »

distian wrote:
Mon Jun 29, 2020 13:16
is there a function to separate thousands or millions with ","? like 1,000,000
Might be a better way but this is what I came up with.

Code: Select all

local function seperate(value)
  local result = ""
  value = tostring(value):reverse()

  for set in value:gmatch("..?.?") do
    result = result..set..","
  end
  result = result:sub(1, -2):reverse()
  return result
end
ImageJust my opinion

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

Platform

Re: Lua chatbox

#72

Post by Lobby »

Found another solution that also uses gsub here:

Code: Select all

function comma_value(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
  end
  return formatted
end

print(comma_value(1234567))
In other languages you'd be able to use the string format function to do that for you, but Lua is different :jb:
=^._.^= ∫

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

#73

Post by ian` »

Thanks.
Lobby wrote:
Mon Jun 29, 2020 16:35
Found another solution that also uses gsub here:

Code: Select all

function comma_value(amount)
  local formatted = amount
  while true do  
    formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
    if (k==0) then
      break
    end
  end
  return formatted
end

print(comma_value(1234567))
In other languages you'd be able to use the string format function to do that for you, but Lua is different :jb:
I tried it before, but didn't work. now it works. lol
:fire

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

Platform

Re: Lua chatbox

#74

Post by Lobby »

Code: Select all    Reset

-- Bearbear local function seperate(value) local result = "" value = tostring(value):reverse() for set in value:gmatch("..?.?") do result = result..set.."," end result = result:sub(1, -2):reverse() return result end -- Internet function comma_value(amount) local formatted = amount while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if (k==0) then break end end return formatted end -- Test print('The bear way: '..seperate(1234567)) print('The internet way: '..comma_value(1234567))
Interactive Lua editor
Run
=^._.^= ∫

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

#75

Post by Bearbear76 »

Ah I should of made it with %d.

Code: Select all    Reset

local function seperate(value) local result = "" value = tostring(value):reverse() for set in value:gmatch("%d%d?%d?") do result = result..set.."," end result = result:sub(1, -2):reverse() return result end print(seperate(1234567890))
Interactive Lua editor
Run
Fixed the code to work because Lobby wants to click green buttons
ImageJust my opinion

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

#76

Post by ian` »

Is there road with road decoration have specific conditions? Because when i'm place road decoration on road, the script doesn't work. Even when i use Tile.isRoadDeco(x,y,level).
:fire

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

Platform

Re: Lua chatbox

#77

Post by Lobby »

Did you use Builder.isRoadDecoBuildable to check if it is buildable in the first place? Road decorations can define requirements like flags and alignment for the road they can be placed on.
=^._.^= ∫

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

#78

Post by ian` »

Still doesn't work. I try to use script:overlay(), that's work, but the script keep updating, not update only once.
:fire

User avatar
CommanderABab
AB
Reactions:
Posts: 8962
Joined: Tue Jun 07, 2016 21:12
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#79

Post by CommanderABab »

distian wrote:
Wed Jul 01, 2020 14:33
Still doesn't work. I try to use script:overlay(), that's work, but the script keep updating, not update only once.
Seems to work for me. Your crush gives s random answer from the array. Right?
Image

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

#80

Post by ian` »

CommanderABab wrote:
Wed Jul 01, 2020 18:12
Seems to work for me. Your crush gives s random answer from the array. Right?
not about signature. lol

Anyway, that is sometime give you an answer not from the array. :bt
:fire

Post Reply Previous topicNext topic

Return to “Lua Scripting”