Frictional Games Forum (read-only)
[SCRIPT] Spawning Item Randomly - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: [SCRIPT] Spawning Item Randomly (/thread-54393.html)



Spawning Item Randomly - Organic Shelter - 04-01-2018

How do i make an item spawns randomly in multiple places? Like a key that generate in one of five chest or something.


RE: Item Spawns Randomly - Romulator - 04-01-2018

Create a few ScriptAreas where your item is to spawn, giving them a name with an appended number. In your example, you state 5, we'll go with SpawnArea_1, SpawnArea_2, etc, up to SpawnArea_5. Place your key somewhere in the map, anywhere will do, so long as it is placed. I'll call it Chest_Key in this example.

To spawn it, we'll generate a Random Integer in code using RandInt(), which we will place in OnStart(). Then we can simply place the entity at the relevant ScriptArea, using the below code.

Code:
PlaceEntityAtEntity(string &in asName, string &in asTargetEntity, string &in asTargetBodyName, bool abUseRotation);

asName - Name of the entity to place.
asTargetEntity - Name of the other entity to place the first entity at.
asTargetBodyName - Name of the body of the entity to place the first entity at. If empty the first body is used (might be buggy, recommended to name a body anyway).
abUseRotation - Whether the entity should be rotated like the target entity.

PHP Code:
void OnStart()
{
     
SetLocalVarInt("RandArea"RandInt(15));
     
PlaceEntityAtEntity("Chest_Key""SpawnArea_"+GetLocalVarInt("RandArea"), ""true);
     
AddDebugMessage("Key spawned inside chest "+GetLocalVarInt("RandArea"), false);


The final value on the PlaceEntityAtEntity, true, means you can rotate the ScriptArea a little horizontally and it will rotate the key relative to that direction.


RE: Item Spawns Randomly - Organic Shelter - 04-05-2018

(04-01-2018, 04:04 PM)Romulator Wrote: Create a few ScriptAreas where your item is to spawn, giving them a name with an appended number. In your example, you state 5, we'll go with SpawnArea_1, SpawnArea_2, etc, up to SpawnArea_5. Place your key somewhere in the map, anywhere will do, so long as it is placed. I'll call it Chest_Key in this example.

To spawn it, we'll generate a Random Integer in code using RandInt(), which we will place in OnStart(). Then we can simply place the entity at the relevant ScriptArea, using the below code.

Code:
PlaceEntityAtEntity(string &in asName, string &in asTargetEntity, string &in asTargetBodyName, bool abUseRotation);

asName - Name of the entity to place.
asTargetEntity - Name of the other entity to place the first entity at.
asTargetBodyName - Name of the body of the entity to place the first entity at. If empty the first body is used (might be buggy, recommended to name a body anyway).
abUseRotation - Whether the entity should be rotated like the target entity.

PHP Code:
void OnStart()
{
     
SetLocalVarInt("RandArea"RandInt(15));
     
PlaceEntityAtEntity("Chest_Key""SpawnArea_"+GetLocalVarInt("RandArea"), ""true);
     
AddDebugMessage("Key spawned inside chest "+GetLocalVarInt("RandArea"), false);


The final value on the PlaceEntityAtEntity, true, means you can rotate the ScriptArea a little horizontally and it will rotate the key relative to that direction.

I've tried it and the Amnesia crash. Should the ScriptArea small or big?


RE: Item Spawns Randomly - Romulator - 04-05-2018

Amnesia is crashing? Does it give you an error? Can you post the hpl.log file and your script?


RE: Item Spawns Randomly - Mudbill - 04-05-2018

Just remember that if you already have a function in your script named OnStart, you need to merge it with this one. You can't have 2 functions with the same name.


RE: Item Spawns Randomly - Organic Shelter - 04-06-2018

I've tried it again and this time it works! Thanks for your help. And now what to add if i want to make the item to spawn a monster?


RE: Spawning Item Randomly - Mudbill - 04-06-2018

How do you mean? You use the item somewhere and a monster spawns?
For that you'd want a AddUseItemCallback and simply SetEntityActive on the enemy.


RE: Spawning Item Randomly - Organic Shelter - 04-08-2018

(04-06-2018, 10:12 PM)Mudbill Wrote: How do you mean? You use the item somewhere and a monster spawns?
For that you'd want a AddUseItemCallback and simply SetEntityActive on the enemy.

And how do i use that?


RE: Spawning Item Randomly - Mudbill - 04-08-2018

Within your OnStart function, add the AddUseItemCallback, which will trigger an event when you use a certain item on a certain object.

PHP Code:
AddUseItemCallback("internal_name""item_name""entity_to_use_item_on""YourCustomFunction"true); 

Here the arguments mean:
1. Internal name for this callback.
2. The name of the inventory item you want to link.
3. The name of the entity in your level you want to link.
4. The custom function name (which must match the name below).
5. Whether this event can trigger only once.

Then you need the callback function with your custom function name (outside the OnStart function), like so:

PHP Code:
void YourCustomFunction(string &in asItemstring &in asEntity)
{
    
//Code in here will trigger when you use the item on the entity.
    
SetEntityActive("enemy_name"true); 
    
//This will enable the entity with the given name, for example an enemy. 
    //Make sure the enemy is set to "Inactive" in the level.