Skip to content

Treasure chest spawning in backwards?

So basically I'm trying to get chest to face away from monster.

My existing script:
void main()
{
  object oPC = GetLastKiller();
    if (!GetIsPC(oPC)) return;
    if (d100()<=10)



{

object oPC;
object oTarget;
object oSpawn;
location lTarget;
oTarget = OBJECT_SELF;

lTarget = GetLocation(oTarget);





oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "tres_anylow002", lTarget);
DelayCommand(0.5, ApplyEffectToObject(DURATION_TYPE_INSTANT, EffectVisualEffect(VFX_FNF_DISPEL), oSpawn));
FloatingTextStringOnCreature("You found loot!", oPC);
    }

      }

I'm trying to blend this with it but just can't make it happen ...
location lSpawnLoc = Location(GetArea(OBJECT_SELF), GetPosition(OBJECT_SELF), GetFacing(OBJECT_SELF)-180.0);

CreateObject(OBJECT_TYPE_PLACEABLE, "treasure_chest_resref_here", lSpawnLoc);

Comments

  • TarotRedhandTarotRedhand Member Posts: 1,481
    Try SetFacing(). From the Lexicon (check out the example at the end of the quoted text) -
    SetFacing(float)

    Sets the object to face a certain direction.

    void SetFacing(float fDirection);

    Parameters

    fDirection

    A floating point number that represents the direction to face. See description for additional details.

    Description

    Causes the caller of this function to face fDirection.

    There are some predefined constants for a direction:

    DIRECTION_EAST = 0.0
    DIRECTION_NORTH = 90.0
    DIRECTION_WEST = 180.0
    DIRECTION_SOUTH = 270.0

    Remarks

    Note that GetFacing will correctly return the facing to the nearest degree from 1.62.

    Known Bugs

    There are issues with setting the facing of sitting creatures.

    Version 1.22

    Example
    // Used in the OnSpawn of a creature to set 
    // their facing to a random direction.
    main()
    {
       // Get a random direction from 0 to 360
       float fDirection = IntToFloat(Random(361));
    
       SetFacing(fDirection);
    }
    

    TR
  • MelkiorMelkior Member Posts: 204
    edited June 2023
    Don't forget to "normalise" (American spelling: normalize) the facing. If you don't, then the value can go below zero which won't work with the facing. If I remember correctly, you can do this by adding instead of subtracting then evaluating modulo 360.
    location lSpawnLoc = Location(GetArea(OBJECT_SELF), GetPosition(OBJECT_SELF), (GetFacing(OBJECT_SELF)+180.0)%360.0);
    
    You should double-check that this works, although I believe I've got it right. The last expression should provide a "facing" which is the opposite direction to OBJECT_SELF.
    (I've never tried to use modulus arithmetic with floats before so I'm not certain it will work even though it should work in theory).
    Edit: And now I see there's a function to do exactly what I showed you.
    #include "x0_i0_position"
    location lSpawnLoc = Location(GetArea(OBJECT_SELF), GetPosition(OBJECT_SELF), GetOppositeDirection(GetFacing(OBJECT_SELF)));
    
    Post edited by Melkior on
Sign In or Register to comment.