Difference between revisions of "AlignObj (CMS)"

From The Official Visionaire Studio: Adventure Game Engine Wiki
Line 7: Line 7:
  
 
This small function allows you to quickly align a character to an object.
 
This small function allows you to quickly align a character to an object.
 +
<hr>
 
{| class="toccolours mw-collapsible mw-collapsed ts"
 
{| class="toccolours mw-collapsible mw-collapsed ts"
 
|-
 
|-
Line 14: Line 15:
 
|-
 
|-
 
| There is no simple method for obtaining the top (y) pixel coordinate of the currently active character animation.
 
| There is no simple method for obtaining the top (y) pixel coordinate of the currently active character animation.
|-
 
| &nbsp;
 
 
|-
 
|-
 
| Alignment to the object is based on the object interaction position; I may change this to object offset position in the future.
 
| Alignment to the object is based on the object interaction position; I may change this to object offset position in the future.
Line 24: Line 23:
 
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 an object...
 
2a. To align current character to an object...
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
alignObj("", "object_name")
+
alignObj(nil, "Rock")
 
</syntaxhighlight>
 
</syntaxhighlight>
 
2b. To align a specific character to an object...
 
2b. To align a specific character to an object...
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
alignObj("character_name", "object_name")
+
alignObj("Tom", "Rock")
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
== Main Script ==
 
== Main Script ==
<syntaxhighlight>
+
<syntaxhighlight lang="lua">
 
function alignObj(c, t)
 
function alignObj(c, t)
  if c == "" then c = game:getLink(VGameCurrentCharacter) else c = getObject("Characters[" .. c .. "]") end
+
  if c == nil then c = game.CurrentCharacter else c = Characters[c] end -- store character or current character
  t = getObject("Game.GameCurrentScene.SceneObjects[" .. t .. "]")
+
  t = game.CurrentScene.Objects[t] -- store target object
 
  -- + --
 
  -- + --
  local p1 = c:getPoint(VCharacterPosition)
+
  local p1 = c.Position -- store character position
  local p2 = t:getPoint(VObjectPosition)
+
  local p2 = t.Position -- store object position
 
  -- + --
 
  -- + --
 
  local ax, ay
 
  local ax, ay
  ay = p1.y - p2.y
+
ax = p2.x - p1.x -- calculate x destination coordinate
ax = p2.x - p1.x
+
  ay = p1.y - p2.y -- calculate y destination coordinate
 
  -- + --
 
  -- + --
  local angle = math.deg( math.atan2(ay, ax) )
+
  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:setValue(VCharacterDirection, angle)
+
  c.Direction = angle -- update specified characters direction
 
end
 
end
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
 
== Syntax Breakdown ==
 
== Syntax Breakdown ==
Line 57: Line 58:
 
! 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; if string is empty it will default to current character.
+
| 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 object you want to align to.
 
| t || "string" || This should be a "string" value containing the name of the object you want to align to.
 
|}
 
|}

Revision as of 00:21, 21 August 2022

Name Type By
alignObj("c", "t") Definition AFRLme

This small function allows you to quickly align a character to an object.


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.
Alignment to the object is based on the object interaction position; I may change this to object offset position in the future.


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 an object...

alignObj(nil, "Rock")

2b. To align a specific character to an object...

alignObj("Tom", "Rock")


Main Script

function alignObj(c, t)
 if c == nil then c = game.CurrentCharacter else c = Characters[c] end -- store character or current character
 t = game.CurrentScene.Objects[t] -- store target object
 -- + --
 local p1 = c.Position -- store character position
 local p2 = t.Position -- store object 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" This should be a "string" value containing the name of the object you want to align to.