Picking apples (and respawning them)

Hi im trying to make a module. I want the character to be able to do some agriculture but it seems that i cant make an apple respawn (with a delay) when being picked up.

The script:

/*

#include “nw_i0_2q4luskan”

void main()
{
object oSpawn;
object oSelf = OBJECT_SELF;

// Get the creature who triggered this event.
object oPC = GetLastUsedBy();

// Destroy an object (not fully effective until this script ends).
DestroyObject(oSelf);

// Give "apple" to the PC.
CreateItemOnObject("apple", oPC);

// Spawn "appleonground".
DelayCommand(4.0, CreateObjectVoid(OBJECT_TYPE_CREATURE, "appleonground", GetLocation(oSelf)));

}

Your apple that the PC uses is a placeable right? Here in this line, you are using OBJECT_TYPE_CREATURE, when you should be using OBJECT_TYPE_PLACEABLE instead. See if fixing that makes it spawn now.

Sorry it was the old script that didnt work. remade it in the script wizard but still dont work.

Script:
/*

void main()
{
object oSpawn;
object oSelf = OBJECT_SELF;

// Get the creature who triggered this event.
object oPC = GetLastUsedBy();

// Destroy an object (not fully effective until this script ends).
DestroyObject(oSelf);

// Give "apple" to the PC.
CreateItemOnObject("apple", oPC);

// Spawn "appleonground".
oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "appleonground", GetLocation(oSelf));

}

Hmm… why not try delaying the DestroyObject function, a bit? It could be interfering with the CreateObject function. (Yes, I know it says destroy is not fully effectiveness until this script ends, but calling it too early can result in problems sometimes)

The script dies, before the new Item is created, because you destroy it or the “GetLocation(oSelf)” in the spawn-Command fails for the same reason.

You need something to else to execute the spawn, not the object itself.

Okay. Do the following.

  1. Make the “apple” placeable (usable, non-static,no plot)
    //Put this script OnUsed
    void main()
    {
    object oPC = GetLastUsedBy();

if (!GetIsPC(oPC)) return;

SetLocalInt(oPC, “ApplePicked”, 1);
CreateItemOnObject(“apple_item”, oPC); // “apple_item” is the item which appears in PC’s inventory
DestroyObject(OBJECT_SELF,0.5);
}

Near the apple, put a creature say an “farmer” and his scripts tab:

//Goes on creature’s OnHeartbeat. Fires when not fighting or talking.
void main()
{
location lTarget;
object oSpawn;
object oTarget;
object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC, OBJECT_SELF, 1, CREATURE_TYPE_PERCEPTION, PERCEPTION_SEEN);

if (IsInConversation(OBJECT_SELF) || GetIsInCombat()) return;

if (GetLocalInt(oPC, “ApplePicked”)== 1)
{
oTarget = GetWaypointByTag(“wp_apple_location”);

lTarget = GetLocation(oTarget);

oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, “apple”, lTarget);
SetLocalInt(oPC, “ApplePicked”, 0);
}
}
This is for one single apple. I’ve tested it and it works fine.

Of course you can check for multiple apples in the same script.
Or to spice it up a little, you can randomize the apple placeable’s spawn location at various locations (waypoints)

@Ransom If you look very closely at your script you will see that it will not compile because your quotation marks have been automatically converted to the 66/99 style. You need to place code in code blocks to avoid this. 2 other advantages of doing that are that your layout will be preserved and it makes copying of your code easier. In a code block, hovering your mouse pointer anywhere over the code makes an icon appear in the top-right corner of the visible code. Clicking that icon automatically copies all the code in the block to the clipboard. See this pinned thread for details on this and several other points about these forums.

As an example here is your code from above with the layout (hopefully) corrected and the quotation marks fixed.

//Put this script OnUsed
void main()
{
    object oPC = GetLastUsedBy();
    
    if(!GetIsPC(oPC)) 
        return;

    SetLocalInt(oPC, "ApplePicked", 1);
    CreateItemOnObject("apple_item", oPC); // "apple_item" is the item which appears in PC’s inventory
    DestroyObject(OBJECT_SELF, 0.5);
}

Near the apple, put a creature say an "farmer" and his scripts tab:

//Goes on creature’s OnHeartbeat. Fires when not fighting or talking.
void main()
{
    location lTarget;
    object oSpawn;
    object oTarget;
    object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC, OBJECT_SELF, 1, CREATURE_TYPE_PERCEPTION, PERCEPTION_SEEN);

    if(IsInConversation(OBJECT_SELF) || GetIsInCombat()) 
        return;

    if(GetLocalInt(oPC, "ApplePicked") == 1) 
    {
        oTarget = GetWaypointByTag("wp_apple_location");

        lTarget = GetLocation(oTarget);

        oSpawn = CreateObject(OBJECT_TYPE_PLACEABLE, "apple", lTarget);
        SetLocalInt(oPC, "ApplePicked", 0);
    }
}

TR

Sorry, this morning I hadn’t the means to create a script, Now I have an alternative approach:

// This goes into the On-Use script of an individual placeable (in my case a book)
// The placeable is flagged as "usable" and has an unique blueprint-ID "kaninchenbaer"
// This script must be attached on the blueprint ("On-Use", see above).

void main()
{
  object oPC = GetLastUsedBy();
  object oArea = GetArea (oPC);

  SetLocalLocation (oArea, "loc_Kaninchenbaer", GetLocation (OBJECT_SELF));
  SignalEvent (oArea, EventUserDefined(4711));
  CreateItemOnObject ("nw_it_book009", oPC);
  DestroyObject (OBJECT_SELF);
}

// This goes into the userdefined event of the area, where the action takes place.

void CreatePLC (location l)
{
  CreateObject (OBJECT_TYPE_PLACEABLE, "kaninchenbaer", l);
}

void main()
{
  int evnt =  GetUserDefinedEventNumber();
  if (evnt == 4711)
    {
      location l = GetLocalLocation (OBJECT_SELF, "loc_Kaninchenbaer");
      DelayCommand (5.0, CreatePLC(l));
    }
}