Re: Lua chatbox
Posted: 26 Jun 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
A feature rich city builder game for Android, iOS and PC
https://forum.theotown.com/
Use TheoTown.registerCommand(string, function)
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]
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]
I just wanted to make a command, which would set radius and zone around some building by taking radius and zone ID as parametersBearbear76 wrote: ↑27 Jun 2020, 06:36I don't think there are any arguments. What do you want to do?
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 :/Temgamer wrote: ↑27 Jun 2020, 08:08I just wanted to make a command, which would set radius and zone around some building by taking radius and zone ID as parametersBearbear76 wrote: ↑27 Jun 2020, 06:36I don't think there are any arguments. What do you want to do?
Try to just change the condition variables with command and call that variables in outside of register command function.I just wanted to make a command, which would set radius and zone around some building by taking radius and zone ID as parameters
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
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
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))
I tried it before, but didn't work. now it works. lolLobby wrote: ↑29 Jun 2020, 16:35 Found another solution that also uses gsub here:In other languages you'd be able to use the string format function to do that for you, but Lua is differentCode: 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))
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))
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))
not about signature. lolCommanderABab wrote: ↑01 Jul 2020, 18:12 Seems to work for me. Your crush gives s random answer from the array. Right?