Difference between revisions of "IsInRadius (CMS)"
Line 6: | Line 6: | ||
|} | |} | ||
− | This small function allows you to quickly query if the mouse cursor is currently inside of a specified radius based on current position of a character, or center of | + | This not so small function allows you to quickly query if the mouse cursor is currently inside of a specified radius based on current position of a character, or the center coordinate of a playing animation or the static image sprite or animation belonging to a scene object. |
== Instructions == | == Instructions == | ||
1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/> | 1. Add the [[#Main_Script|main script]] to the Visionaire Studio Script Editor & set the script as a definition script.<br/> | ||
− | 2a. | + | 2a. Usage Example #1: check if cursor is inside of a 50px radius from current characters position via '''if lua result''' action part. |
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
− | isInRadius( | + | return isInRadius(game.CurrentCharacter, 50) |
</syntaxhighlight> | </syntaxhighlight> | ||
− | 2b. | + | 2b. Usage Example #2: check if cursor is inside of a 50px radius from the center of the image/animation sprite belonging to the specified scene object via '''if lua result''' action part. |
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
− | isInRadius(Objects["object_name"], | + | return isInRadius(game.CurrentScene.Objects["object_name"], 50) |
</syntaxhighlight> | </syntaxhighlight> | ||
− | 3 | + | 2c. Usage Example #3: check if cursor is inside of a 50px radius from the center of the specified animation via '''if lua result''' action part. |
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
− | + | return isInRadius(ActiveAnimations["animation_name"], 50) | |
− | -- | + | </syntaxhighlight> |
+ | 2d. Usage Example #4: check if cursor is inside of a 50px radius from the the specified characters position inside of a regular if query. | ||
+ | <syntaxhighlight lang="lua"> | ||
+ | if isInRadius(game.CurrentCharacter, 50) then | ||
+ | -- true, add some actions here | ||
else | else | ||
− | -- | + | -- false, add some actions here |
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | |||
== Main Script == | == Main Script == | ||
− | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
− | local x, y -- empty variables which the | + | local x, y -- empty variables which will be used to store data in later on |
+ | local radius = 20 -- specify the radius in pixels (from characters position in scene) | ||
local pos = {} -- empty table which we will store the mouse cursor positions in | local pos = {} -- empty table which we will store the mouse cursor positions in | ||
− | + | ||
function isInRadius(obj, rad) | function isInRadius(obj, rad) | ||
− | + | pos.x = game.ScrollPosition.x + getCursorPos().x; pos.y = game.ScrollPosition.y + getCursorPos().y -- store current cursor position in the scene | |
− | + | if obj:getId().tableId == eObjects then if not obj.Animation:isEmpty() then obj = ActiveAnimations[obj.Animation:getName()] end end -- check if object & contains animation | |
− | + | -- + character position & radius + -- | |
− | + | if obj:getId().tableId == eCharacters then -- check if character | |
− | x = obj.Position.x; y = obj.Position.y | + | x = obj.Position.x; y = obj.Position.y -- store characters position |
− | rad = math.floor(obj.Size * | + | rad = math.floor(obj.Size * radius) / 100 -- dynamically adjust radius size based on current scale value of character |
− | + | -- + object sprite position & radius + -- | |
− | x = obj. | + | elseif obj:getId().tableId == eObjects then |
− | y = obj. | + | x = obj.Sprite.Sprite:getPosition().x + (obj.Sprite.Sprite:getSize().x / 2) -- calculate center coordinate belonging to object sprite (x) |
− | rad = math.floor(obj.Scale * rad) | + | y = obj.Sprite.Sprite:getPosition().y + (obj.Sprite.Sprite:getSize().y / 2) -- calculate center coordinate belonging to object sprite (y) |
− | + | rad = math.floor(obj.Scale * radius) -- dynamically adjust radius size based on current scale value of object | |
− | + | -- + animation sprite position & radius + -- | |
− | + | elseif obj:getId().tableId == eActiveAnimations then | |
− | + | x = obj.CurrentPosition.x + (getAnimationSize(obj).x / 2) -- calculate center coordinate belonging to the currently displayed sprite of the animation (x) | |
− | + | y = obj.CurrentPosition.y + (getAnimationSize(obj).y / 2) -- calculate center coordinate belonging to the currently displayed sprite of the animation (y) | |
− | + | rad = rad = math.floor(obj.Scale * radius) -- dynamically adjust radius size based on current scale value of animation | |
+ | end | ||
+ | -- + calculate distance from circle center + -- | ||
+ | if x > pos.x then x = math.floor(x - pos.x) else x = math.floor(pos.x - x) end -- fallback to invert position calculation in case x is greater than pos.x | ||
+ | if y > pos.y then y = math.floor(y - pos.y) else y = math.floor(pos.y - y) end -- fallback to invert position calculation in case y is greater than pos.y | ||
+ | if math.floor( (x ^ 2) + (y ^ 2) ) <= (rad ^ 2) then return true else return false end -- check if in radius & return true or false | ||
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
== Syntax Breakdown == | == Syntax Breakdown == | ||
Line 59: | Line 72: | ||
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left;width:80%" | Description | ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left;width:80%" | Description | ||
|- | |- | ||
− | | obj || link || This should be a link to a scene object or a character. ''The function automatically detects if obj type is a character or object.'' | + | | obj || link || This should be a link to a scene object, an active animation (currently playing), or a character. ''The function automatically detects if obj type is a character, active animation, or object.'' |
|- | |- | ||
− | | rad || integer || This should be | + | | rad || integer || This should be number value containing the radius value of the circle. ''Radius = circle diameter ÷ 2.'' |
− | |} | + | |}{{toc}} |
Revision as of 22:29, 23 August 2022
Name | Type | By |
---|---|---|
isInRadius(obj, rad) | Definition | AFRLme |
This not so small function allows you to quickly query if the mouse cursor is currently inside of a specified radius based on current position of a character, or the center coordinate of a playing animation or the static image sprite or animation belonging to a scene object.
Instructions
1. Add the main script to the Visionaire Studio Script Editor & set the script as a definition script.
2a. Usage Example #1: check if cursor is inside of a 50px radius from current characters position via if lua result action part.
return isInRadius(game.CurrentCharacter, 50)
2b. Usage Example #2: check if cursor is inside of a 50px radius from the center of the image/animation sprite belonging to the specified scene object via if lua result action part.
return isInRadius(game.CurrentScene.Objects["object_name"], 50)
2c. Usage Example #3: check if cursor is inside of a 50px radius from the center of the specified animation via if lua result action part.
return isInRadius(ActiveAnimations["animation_name"], 50)
2d. Usage Example #4: check if cursor is inside of a 50px radius from the the specified characters position inside of a regular if query.
if isInRadius(game.CurrentCharacter, 50) then
-- true, add some actions here
else
-- false, add some actions here
end
Main Script
local x, y -- empty variables which will be used to store data in later on
local radius = 20 -- specify the radius in pixels (from characters position in scene)
local pos = {} -- empty table which we will store the mouse cursor positions in
function isInRadius(obj, rad)
pos.x = game.ScrollPosition.x + getCursorPos().x; pos.y = game.ScrollPosition.y + getCursorPos().y -- store current cursor position in the scene
if obj:getId().tableId == eObjects then if not obj.Animation:isEmpty() then obj = ActiveAnimations[obj.Animation:getName()] end end -- check if object & contains animation
-- + character position & radius + --
if obj:getId().tableId == eCharacters then -- check if character
x = obj.Position.x; y = obj.Position.y -- store characters position
rad = math.floor(obj.Size * radius) / 100 -- dynamically adjust radius size based on current scale value of character
-- + object sprite position & radius + --
elseif obj:getId().tableId == eObjects then
x = obj.Sprite.Sprite:getPosition().x + (obj.Sprite.Sprite:getSize().x / 2) -- calculate center coordinate belonging to object sprite (x)
y = obj.Sprite.Sprite:getPosition().y + (obj.Sprite.Sprite:getSize().y / 2) -- calculate center coordinate belonging to object sprite (y)
rad = math.floor(obj.Scale * radius) -- dynamically adjust radius size based on current scale value of object
-- + animation sprite position & radius + --
elseif obj:getId().tableId == eActiveAnimations then
x = obj.CurrentPosition.x + (getAnimationSize(obj).x / 2) -- calculate center coordinate belonging to the currently displayed sprite of the animation (x)
y = obj.CurrentPosition.y + (getAnimationSize(obj).y / 2) -- calculate center coordinate belonging to the currently displayed sprite of the animation (y)
rad = rad = math.floor(obj.Scale * radius) -- dynamically adjust radius size based on current scale value of animation
end
-- + calculate distance from circle center + --
if x > pos.x then x = math.floor(x - pos.x) else x = math.floor(pos.x - x) end -- fallback to invert position calculation in case x is greater than pos.x
if y > pos.y then y = math.floor(y - pos.y) else y = math.floor(pos.y - y) end -- fallback to invert position calculation in case y is greater than pos.y
if math.floor( (x ^ 2) + (y ^ 2) ) <= (rad ^ 2) then return true else return false end -- check if in radius & return true or false
end
Syntax Breakdown
Name | Type | Description |
---|---|---|
obj | link | This should be a link to a scene object, an active animation (currently playing), or a character. The function automatically detects if obj type is a character, active animation, or object. |
rad | integer | This should be number value containing the radius value of the circle. Radius = circle diameter ÷ 2. |