Hearbeat Woes

I have a placeable with a bit of code for its OnHearbeat handler. Just want a critter to spawn when the PC nears?

Code:

#include "nw_i0_generic"

void main()
{
    object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC);
    if ( GetIsPC(oPC) ) return;

    if ( GetDistanceToObject(OBJECT_SELF) <= 2.5 )
    {
        string sSpider = "q1_spidgiant_03";
        CreateObject(OBJECT_TYPE_CREATURE, "q1_spidgiant_03", GetLocation(oPC));
        AssignCommand(StringToObject(sSpider), DetermineCombatRound(oPC));
    }
}
Problem is, nothing happens at all?
Thank you for any help...

Shouldn’t that be -

if (!(GetIsPC(oPC)))
	return;

At present your code exits when the PC approaches the placeable. Surely you want the code to return when it is not the PC.

TR

Oh shoot. Good catch. Where is my head at…?

Edit: Amazing! I’ve got spider spawn now. Sorry about that Tarot. I should have caught that… :frowning:

Having tested it, I don’t think your AssignCommand is currently doing anything, since I don’t think StringToObject can be used to find a creature like that. I think you’re thinking of GetObjectByTag, but I wouldn’t use that here because as a heartbeat this script will end up spawning multiple creatures over the object’s lifespan. Instead, I’d remove the string and do the following:

object oSpider = CreateObject(OBJECT_TYPE_CREATURE, "q1_spidgiant_03", GetLocation(oPC));
AssignCommand(oSpider, DetermineCombatRound(oPC));

Doing this gets the creature spawned by the current instance of the script to immediately start attacking the player.

Yeah, wasn’t sure about that function.

Now the spider spawns no matter what. It doesn’t wait until the PC nears. Not sure what I’m doing wrong boys…?

I’m surprised I didn’t notice it before, but GetDistanceToObject needs to target oPC rather than OBJECT_SELF, because you’re currently measuring if the placeable is within 2.5 meters of itself rather than if the PC is.

I’ve tried using both OBJECT_SELF, and oPC. Neither are working properly. Or my coding just sucks. It just spawns no matter the distance…?

That’s odd. Without seeing your current version of the script I don’t know what could be wrong, but this is the version I have in my test module that’s currently working for me.

#include "nw_i0_generic"

void main()
{
    object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC);
    if (!GetIsPC(oPC)) return;

    if (GetDistanceToObject(oPC) <= 2.5)
    {
        object oSpider = CreateObject(OBJECT_TYPE_CREATURE, "q1_spidgiant_03", GetLocation(oPC));
        AssignCommand(oSpider, DetermineCombatRound(oPC));
    }
}

I’ll give this a go and see whats up…
Thanks for the reply.

Thanks. Working well now.

I modified it a little…
Code now:

#include "nw_i0_generic"

void main()
{
    object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC);
    if (!GetIsPC(oPC)) return;

    if (GetDistanceToObject(oPC) <= 2.5)
    {
        string sSound = "opening_01";

        location lLoc = GetLocation(GetWaypointByTag("POD_VFX"));
        ApplyEffectAtLocation(DURATION_TYPE_INSTANT, EffectVisualEffect( VFX_COM_CHUNK_STONE_MEDIUM ),  lLoc);

        object oSpider = CreateObject(OBJECT_TYPE_CREATURE, "q1_spidgiant_05", GetLocation(oPC));
        AssignCommand(oSpider, DetermineCombatRound(oPC));

        DelayCommand(1.5, AssignCommand(oPC, PlaySound(sSound)));

        DestroyObject(OBJECT_SELF, 2.5);
    }
}
1 Like