When a player hovers the mouse over an object, its description will update to show the names of the objects that are beneath that object.
This intermediate guide assumes basic knowledge of lua scripting.
It can be useful to know what is beneath an object, without moving the object to check. 예를 들어, a stack of counters in a wargame, or any other stack of tiles.
This function uses the onObjectHover() function in the TTS API. When the player hovers the mouse over an object, this function updates that object’s description to list the names of the objects underneath that object. The list is added to the end of any description that is already attached to the object.
This function calls functions from the lua string library, which includes many interesting features. You can read more about them in the string library tutorial on the lua-users wiki.
This function works best if you have a way of identifying the objects in your game, such as the Tag functions in the TTS API. 또한, the objects need to have names or some other information that can be added to the description.
The Function
function onObjectHover(플레이어, obj) if obj and obj.hasTag("Hover") then local descrip = obj.getDescription() or '' local start = descrip:찾다('stacked with') if start then descrip = descrip:sub(1, start-1) end local hitObjects = { } local stackObjects = { } local pos = obj.getPosition() pos.y = pos.y + 2 hitObjects = Physics.cast({origin = pos, direction = {0,-1,0}}) for _, hitobj in ipairs(hitObjects) do if obj ~= hitobj.hit_object and hitobj.hit_object.hasMatchingTag(obj) then table.insert(stackObjects, hitobj.hit_object) end end if #stackObjects>0 then if descrip ~= '' and not descrip:endsWith('\n') then descrip = descrip..'\n' end descrip = descrip..'stacked with\n' for _, stackobj in ipairs(stackObjects) do local name = stackobj.getName() or '' descrip = descrip .. 이름 .. '\n' end end if descrip:endsWith('\n') then descrip = descrip:sub(1, -2) end obj.setDescription(descrip) end end
What the Function Does
The function:
- Checks that the mouse is currently hovering over an object (“if obj …”). Without this, the function may throw an error.
- Checks that the object has a certain tag. You should use some test here, because this function is probably useful for only some of the objects in your game.
- Gets the description of the object, or an empty string if the object does not have a description.
- Checks if the description contains the key phrase “stacked with”. You could use any phrase here, but the function works by checking for this key phrase, so the phrase has to be consistent, and has to be something that would not otherwise appear in an object’s description.
- If the description contains the key phrase “stacked with”, deletes that phrase and everything that comes after it. (That information will be added back later.)
- Gets the position of the object.
- Identifies every other object downwards from a point slightly above the object, and puts those objects in a table called hitObjects.
- Checks each object in hitObjects. If the object is not the first object and if the object has a tag that matches the first object, then the object is added to a table called stackObjects.
- Checks if anything is in the stackObjects table, 그렇다면:
adds a new line to the first object’s description (if there is already text in the description),
adds the key phrase “stacked with” to the first object’s description, 그리고
gets the name of each object in stackObjects, adds it to the first object’s description, and adds a new line. - Deletes the new line from the end of the first object’s description, if any.
- Sets the text of the first object’s description.
The String Library Functions
In brief, this is what the functions from the string library do.
start = descrip:찾다(‘stacked with’) checks whether a string variable, descrip, contains the phrase “stacked with”, and if so it returns the index of the beginning of that phrase. 이 경우, the index is the nth letter in descrip at which the “에스” ~에 “stacked with” 나타납니다. This number is assigned to the variable start.
descrip = descrip:sub(1, start-1) If start is not nil (즉., if the phrase “stacked with” does appear in descrip), then return a new string that includes everything in descrip up to but not including the index start. One is subtracted at the end so that the first letter of “stacked with” is not included. This new string is assigned to and replaces the variable descrip.
not descrip:endsWith(‘\n’) The endsWith() function is not in the lua string library but rather is part of the Moonsharp library that TTS uses. It returns true if a string ends with a specified phrase. 여기, it checks if descrip already has a new line at the end, before adding one.
descrip:sub(1, -2) The function sometimes ends with an extra new line at the end of the object’s description, which looks strange. 그래서, if descrip ends with a new line (and nothing after), descrip:sub(1, -2) returns a new string that includes everything in descrip up to but not including the last two letters in the string (즉., ‘\n’), and assigns it to the variable descrip.
Note that lua does not delete anything from a string, but rather returns a new string that does not have what you want to delete.
Physics.cast()
This function from the TTS API returns a table of all the objects that exist in a certain area or direction. It can do many things.
여기, it uses mainly the default parameters to cast a line downwards from the object over which the mouse is hovering. 이를 위해, it helps to set the origin of the line a value of 2 above the object itself. This seems to work best to capture everything in the stack.
Physics.cast() is fully described in the TTS API reference. It probably could be used to identify objects surrounding (rather than beneath) a certain object, but this Guide is limited to lines cast downward from an object.
Note that Physics.cast() will identify every object it intercepts, including the game table and other incidental objects.
이 때문에, the function checks that obj ~= hitobj.hit_object, to avoid including obj in its own description.
또한, it is necessary to include some test to include only certain objects in the stackObjects table. 여기, the object is included if it has a tag that matches the first object.
문제 해결
This function adds the names of the other objects to the first object’s description. If the objects in your game do not have names, then use some other information and replace the call to the getName() 기능.
This function leaves (즉., does not delete) the list of objects in the first object’s description.
This should not normally be a problem, because the description only appears when the mouse hovers over the object. And the first thing the function does (when the mouse hovers over an object in the relevant group) is to check if the description contains the key phrase “stacked with”. 그렇다면, the function deletes that phrase and anything that comes after it.
So if the mouse ever hovers over an object in the relevant group, and no relevant object is beneath it, then the function will reset the object’s description.
If this does become a problem, please leave a comment and perhaps the function can be corrected to avoid it.
이것이 오늘 우리가 공유하는 모든 것입니다. Tabletop Simulator 가이드. 이 가이드는 원래 작성자가 작성하고 작성했습니다. CareyMcDuff. 이 가이드를 업데이트하지 못한 경우, 다음을 수행하여 최신 업데이트를 찾을 수 있습니다. 링크.