[Resolved]script:click() doesn't work

Plugin specific problems will be solved here.

Moderator: Plugin Moderators

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

[Resolved]script:click() doesn't work

I've been writing some simple script, which will activate, when player tap on a building on some coordinates.

Code: Select all

function script:click(10,10)
Debug.toast("click test")
end

But when I'm trying to start the game it returns this error:
Where is the problem? There's nothing said about <name> in "script:click()" documentation.
Last edited by Temgamer on Thu Jun 25, 2020 1:06, edited 1 time in total.

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

Re: script:click() doesn't work

Could you post or send the contents of Oxadg_cat.json?
...

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

Re: script:click() doesn't work

Yes, there is the building, which uses that script:

Code: Select all

{
"id":"$radioo2", "type":"decoration", "category":"$0xadg_cat",
"width":1,
"height":1,
"title":"lua test",
"text":"hope this works",
"script":"soundplay.lua"
}


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

Re: script:click() doesn't work

Needs [ at the start and ] at the end if that is the complete json.
...

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

Re: script:click() doesn't work

CommanderABab wrote:
Wed Jun 24, 2020 19:09
Needs [ at the start and ] at the end if that is the complete json.
It's a part of bigger json file, only it uses that script. Also, 6th line in .lua file is where the "script:click()" is located

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

Re: script:click() doesn't work

OK. What does the lua function look like?
...

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

Re: script:click() doesn't work

CommanderABab wrote:
Wed Jun 24, 2020 19:51
OK. What does the lua function look like?
It should show me a toast, when I'm tapping at something at x:10 and y:10

Code: Select all

function script:click(10,10)
Debug.toast("click test")
end


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

Re: script:click() doesn't work

It might be expecting the id of a building and not coordinates.
...

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

Re: script:click() doesn't work

CommanderABab wrote:
Wed Jun 24, 2020 20:34
It might be expecting the id of a building and not coordinates.
I've tried entering id of that building, also a name of it, for some reason, but it still doesn't work.
That's description of click() on the Lua wiki, in parameters there is only numbers
Also, I checked script:tap() and it returns the same error. Maybe they're unsupported yet

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

Re: script:click() doesn't work

Ah you're getting a little bit confused with how TheoTown uses functions.
You don't put values in script:click(x, y, level) instead you put in variables like x, y, level (a variable is a word that can have a mutateable (changeable) value). The general rule would be when you declare functions (using the function keyword) you use variables in the parameters (round brackets).

Code: Select all

--- Declaring a function
function sum(a, b) -- use variables in here
return a + b
end

But when calling (using) a function you use normal values or variables that contain values.

Code: Select all

local number = 12 -- declare variable

--- Calling a function
foo(50, number) -- Use values or variables that were declared

--> res0: number = 62
-- Output of foo(50, number) is 62

The same logic applies when using script:tap(tileX, tileY, x, y) (using tap as it's easier to explain).
Let's say you had this code:

Code: Select all

function script:tap(tileX, tileY, x, y)
Debug.toast("You tapped: "..x..","..y)
end

What this would do is that whenever TheoTown detects that the user tapped the screen it would call this function (hence the name "tap"). Then TheoTown gives you information about the tap. That's what's going to be inside the parameters.
Let's say if you tapped (20, 40) on the screen TheoTown will put those values inside the x, y of the parameter so that you can use it.

This is probably hard to understand by just reading. I think it would be easier to understand by experiments so try this code out and try to figure out how it works.

Code: Select all

function script:tap(tileX, tileY, x, y)
local name = "TheoTown"
Debug.toast(name.." detected that you tapped: ("..tileX..","..tileY..")")
end

Just my opinion
I'm probably recompiling my kernel... :o

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

Re: script:click() doesn't work

Ølsken wrote:
Thu Jun 25, 2020 0:28
Ah you're getting a little bit confused with how TheoTown uses functions.
You don't put values in script:click(x, y, level) instead you put in variables like x, y, level (a variable is a word that can have a mutateable (changeable) value). The general rule would be when you declare functions (using the function keyword) you use variables in the parameters (round brackets).

Code: Select all

--- Declaring a function
function sum(a, b) -- use variables in here
return a + b
end

But when calling (using) a function you use normal values or variables that contain values.

Code: Select all

local number = 12 -- declare variable

--- Calling a function
foo(50, number) -- Use values or variables that were declared

--> res0: number = 62
-- Output of foo(50, number) is 62

The same logic applies when using script:tap(tileX, tileY, x, y) (using tap as it's easier to explain).
Let's say you had this code:

Code: Select all

function script:tap(tileX, tileY, x, y)
Debug.toast("You tapped: "..x..","..y)
end

What this would do is that whenever TheoTown detects that the user tapped the screen it would call this function (hence the name "tap"). Then TheoTown gives you information about the tap. That's what's going to be inside the parameters.
Let's say if you tapped (20, 40) on the screen TheoTown will put those values inside the x, y of the parameter so that you can use it.

This is probably hard to understand by just reading. I think it would be easier to understand by experiments so try this code out and try to figure out how it works.

Code: Select all

function script:tap(tileX, tileY, x, y)
local name = "TheoTown"
Debug.toast(name.." detected that you tapped: ("..tileX..","..tileY..")")
end

Thank you very much, I forgot about that function parameters in Lua

ian
Supporter
Reactions:
Posts: 104
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Re: script:click() doesn't work

Code: Select all

script:click(x,y,level)  -- this is will detect attached building positions every where on your city.

script:click(10,10,level) -- this is will detect only on x:10 y:10 coordinate on your city, but level will detect to attached building or road position.

script:click(10,y,level) or script:click(x,10,level) -- this is will be like line, just detect 10 on x or y only, but will detect attached building positions on unfilled x or y.

script:click(x+10,y+10,level) -- this is will detect attached building positions + 10. Your building will be x:0 and y:0 then when you click x:10 and y:10, the script will be work. If your building position in your city is x:50 and y:70, so the script will be work on x:60 and y:80. Other calculation will be work, like - * or / depend on where you need coordinates you want to script will work.`
You can use lua real time editing on viewtopic.php?f=115&t=10834 to see what your script errors or when you want to change any stuff on your lua script without restart the games. It's work on desktop or mobile.