Skip to content

CreateObject crashes game

So to explain the situation: I have a 'fun' value that is a random number between 0 and 9. When the module is loaded for the first time this number is generated and doesn't change for that playthrough. I'm trying to make certain random events happen based on that number.

Right now I'm trying to spawn a merchant who sells scrolls. The idea is that when the PC enters the area for the first time it checks if the creature has already spawned and the fun value is the correct amount (right now I'm just looking for higher than 0). The script runs in the OnEnter for the area. As soon as the player enters the area though the game crashes (becomes non-responsive). As far as I can tell something in CreateObject is not functioning.

Where is the problem here?
int nFun = GetLocalInt(GetModule(), "fun");

void main()
{
        location lLoc = GetLocation(GetWaypointByTag("NW_WAYPOINT001"));

        object oNearestObject = GetNearestObjectToLocation(OBJECT_TYPE_CREATURE, lLoc);

        if(nFun > 1 && GetTag(oNearestObject) != "npc_scrollmer001")
        {
            int nObjectType = OBJECT_TYPE_CREATURE;

            string sTemplate = "npc_scrollmer001";

            int bUseAppearAnimation = FALSE;

            CreateObject(nObjectType, sTemplate, lLoc, bUseAppearAnimation);
        }
}

Comments

  • TerrorbleTerrorble Member Posts: 179
    I'm not sure why it would crash. Does it crash without the script in the AreaOnEnter handle?

    If it only crashes with this script, then I'd try giving your waypoint a unique tag and confirming that the tag of the scroll merchant and it's resref are both npc_scrollmer001. (Though that shouldn't be a reason for it to crash. If those were wrong it just wouldn't spawn the merchant.)

    Is there something wrong with the NPC you made? What if you just place the merchant in the area in the toolset and enter the area? Does their store open when you talk to them?


    Also, the script can be shortened a bit:
    void main()
    {
        if( GetLocalInt(GetModule(),"fun") > 0 && !GetIsObjectValid(GetObjectByTag("npc_scrollmer001")) )
        {
            //The name of the waypoint you are searching for has the default waypoint tag.  I'd give it a unique tag.
            location lLoc = GetLocation(GetWaypointByTag("NW_WAYPOINT001"));
    
            CreateObject(OBJECT_TYPE_CREATURE,"npc_scrollmer001",lLoc,FALSE);
        }
    }
    
  • Dragonfolk2000Dragonfolk2000 Member Posts: 388
    I've switched it out for your code and it still crashes upon entering the area. To answer your questions in order:

    - It doesn't crash when it isn't in the AreaOnEnter handle.
    - I gave the waypoint a unique tag and the tag and resref are correct for the scroll merchant.
    - I've made a new NPC from scratch. The first one was copied from an existing character so I thought there may have been a problem with that. It still crashes on load.
  • ProlericProleric Member Posts: 1,316
    Try checking that the entering object is a PC.

    Also, I'd put conditions like nFun > 1 in parentheses (nFun > 1) when using operators like && so that the order of evaluation is unambiguous.

    The script is creating a merchant, who fires OnEnter, perhaps creating another merchant... ad infinitum until the module crashes or TMI kicks in.
  • Dragonfolk2000Dragonfolk2000 Member Posts: 388
    edited February 2022
    Proleric wrote: »
    Try checking that the entering object is a PC.

    Also, I'd put conditions like nFun > 1 in parentheses (nFun > 1) when using operators like && so that the order of evaluation is unambiguous.

    The script is creating a merchant, who fires OnEnter, perhaps creating another merchant... ad infinitum until the module crashes or TMI kicks in.

    It looks like you had it right. The on area enter seemed to have been firing for every creature that entered, including the newly spawned merchant. Now it spawns a merchant every time you enter the area but I think I can sort it out from here. In case anyone needs it here is the current state of the code (with the merchant that spawns every time someone enters the map).
    void main()
    {
        if((GetEnteringObject() == GetFirstPC()) && (GetLocalInt(GetModule(),"fun") > 0) && (!GetIsObjectValid(GetObjectByTag("scrollmerchant"))) )
        {
            //The name of the waypoint you are searching for has the default waypoint tag.  I'd give it a unique tag.
            location lLoc = GetLocation(GetWaypointByTag("wp_merchantspot"));
    
            CreateObject(OBJECT_TYPE_CREATURE,"scrollmerchant",lLoc,FALSE);
        }
    }
    

    Update: The 'spawn another one every time you enter' was a result of the incorrect tag in the if statement.
  • MelkiorMelkior Member Posts: 204
    First thing to do is "if(!GetIsPC(GetEnteringObject())) return;" That'll stop any triggering from NPCs, created or otherwise, entering the area. That line or something similar is pretty much standard practice for me unless I intend for the script to trigger for a NPC.
Sign In or Register to comment.