Difference between revisions of "AlignChar (CMS)"
From The Official Visionaire Studio: Adventure Game Engine Wiki
(Created page with "{| class="wikitable" style="width:100%" |- ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By |- | alignChar("c", "t") || Defin...") |
m |
||
| (12 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | {| class=" | + | {| class="ts" style="width:100%" |
|- | |- | ||
! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By | ! style="text-align:left" | Name !! style="text-align:left" | Type !! style="text-align:left" | By | ||
| Line 7: | Line 7: | ||
This small function allows you to quickly align a character to another character. | This small function allows you to quickly align a character to another character. | ||
| + | <hr> | ||
| + | {| class="toccolours mw-collapsible mw-collapsed ts" | ||
| + | |- | ||
| + | ! Additional Info | ||
| + | |- | ||
| + | | Alignment is based on character center which is usually set near the feet. | ||
| + | |- | ||
| + | | There is no simple method for obtaining the top (y) pixel coordinate of the currently active character animation. | ||
| + | |} | ||
| + | |||
== 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. To align current character to another character... | 2a. To align current character to another character... | ||
| − | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
| − | alignChar( | + | alignChar(nil, "Tom") |
</syntaxhighlight> | </syntaxhighlight> | ||
2b. To align a specific character to another character... | 2b. To align a specific character to another character... | ||
| − | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
| − | + | alignChar("Tom", "Barry") | |
</syntaxhighlight> | </syntaxhighlight> | ||
2c. To align a specific character to current character... | 2c. To align a specific character to current character... | ||
| − | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
| − | + | alignChar("Tom") | |
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
== Main Script == | == Main Script == | ||
| − | <syntaxhighlight> | + | <syntaxhighlight lang="lua"> |
function alignChar(c, t) | function alignChar(c, t) | ||
| − | if c == | + | if c == nil then c = game.CurrentCharacter else c = Characters[c] end -- store character or current character |
| − | if t == | + | if t == nil then t = game.CurrentCharacter else t = Characters[t] end -- store character or current character |
-- + -- | -- + -- | ||
| − | local p1 = c | + | local p1 = c.Position -- store characters position |
| − | local p2 = t | + | local p2 = t.Position -- store target characters position |
-- + -- | -- + -- | ||
local ax, ay | local ax, ay | ||
| − | ay = p1.y - p2.y | + | ax = p2.x - p1.x -- calculate x destination coordinate |
| − | + | ay = p1.y - p2.y -- calculate y destination coordinate | |
-- + -- | -- + -- | ||
| − | local angle = math.deg( math. | + | local angle = math.deg( math.atan(ay, ax) ) -- calculate angle |
| − | if angle < 0 then angle = 360 + angle end | + | if angle < 0 then angle = 360 + angle end -- some fallback? I don't remember why I wrote it |
-- + -- | -- + -- | ||
| − | c | + | c.Direction = angle -- update specified characters direction |
end | end | ||
</syntaxhighlight> | </syntaxhighlight> | ||
| + | |||
== Syntax Breakdown == | == Syntax Breakdown == | ||
| − | {| class=" | + | {| class="ts" style="width:100%" |
|- | |- | ||
! 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 | ||
|- | |- | ||
| − | | c || "string" || This should be a "string" value containing the name of the character you want to affect | + | | c || "string" or nil || This should be a "string" value containing the name of the character you want to affect, or nil if you want to link to the current character. |
|- | |- | ||
| − | | t || "string" || This should be a "string" value containing the name of the character you want to align to | + | | t || "string" or nil || This should be a "string" value containing the name of the character you want to align to, or nil if you want to link to the current character. |
| − | |} | + | |}{{toc}} |
Latest revision as of 19:49, 23 August 2022
| Name | Type | By |
|---|---|---|
| alignChar("c", "t") | Definition | AFRLme |
This small function allows you to quickly align a character to another character.
| Additional Info |
|---|
| Alignment is based on character center which is usually set near the feet. |
| There is no simple method for obtaining the top (y) pixel coordinate of the currently active character animation. |
Instructions
1. Add the main script to the Visionaire Studio Script Editor & set the script as a definition script.
2a. To align current character to another character...
alignChar(nil, "Tom")
2b. To align a specific character to another character...
alignChar("Tom", "Barry")
2c. To align a specific character to current character...
alignChar("Tom")
Main Script
function alignChar(c, t)
if c == nil then c = game.CurrentCharacter else c = Characters[c] end -- store character or current character
if t == nil then t = game.CurrentCharacter else t = Characters[t] end -- store character or current character
-- + --
local p1 = c.Position -- store characters position
local p2 = t.Position -- store target characters position
-- + --
local ax, ay
ax = p2.x - p1.x -- calculate x destination coordinate
ay = p1.y - p2.y -- calculate y destination coordinate
-- + --
local angle = math.deg( math.atan(ay, ax) ) -- calculate angle
if angle < 0 then angle = 360 + angle end -- some fallback? I don't remember why I wrote it
-- + --
c.Direction = angle -- update specified characters direction
end
Syntax Breakdown
| Name | Type | Description |
|---|---|---|
| c | "string" or nil | This should be a "string" value containing the name of the character you want to affect, or nil if you want to link to the current character. |
| t | "string" or nil | This should be a "string" value containing the name of the character you want to align to, or nil if you want to link to the current character. |