| 
		
	
		| Organic Shelter   Junior Member
 
 Posts: 22
 Threads: 5
 Joined: Mar 2018
 Reputation: 
0
 | 
			| Spawning Item Randomly 
 
				How do i make an item spawns randomly in multiple places? Like a key that generate in one of five chest or something.
			 |  |  
	| 04-01-2018, 08:22 AM |  |  
	
		| Romulator   Not Tech Support ;-)
 
 Posts: 3,628
 Threads: 63
 Joined: Jan 2013
 Reputation: 
195
 | 
			| RE: Item Spawns Randomly 
 
				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.
 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.
void OnStart(){
 SetLocalVarInt("RandArea", RandInt(1, 5));
 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.
			
 Discord: Romulator#0001
![[Image: 3f6f01a904.png]](https://puu.sh/zOxJg/3f6f01a904.png) |  |  
	| 04-01-2018, 04:04 PM |  |  
	
		| Organic Shelter   Junior Member
 
 Posts: 22
 Threads: 5
 Joined: Mar 2018
 Reputation: 
0
 | 
			| RE: Item Spawns Randomly 
 
				 (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.
 
 
 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.
 void OnStart(){
 SetLocalVarInt("RandArea", RandInt(1, 5));
 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?
			 |  |  
	| 04-05-2018, 12:33 PM |  |  
	
		| Romulator   Not Tech Support ;-)
 
 Posts: 3,628
 Threads: 63
 Joined: Jan 2013
 Reputation: 
195
 | 
			| RE: Item Spawns Randomly 
 
				Amnesia is crashing? Does it give you an error? Can you post the hpl.log file and your script?
			 
 Discord: Romulator#0001
![[Image: 3f6f01a904.png]](https://puu.sh/zOxJg/3f6f01a904.png) |  |  
	| 04-05-2018, 01:00 PM |  |  
	
		| Mudbill   Muderator
 
 Posts: 3,881
 Threads: 59
 Joined: Apr 2013
 Reputation: 
179
 | 
			| RE: Item Spawns Randomly 
 
				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.
			 
 |  |  
	| 04-05-2018, 09:18 PM |  |  
	
		| Organic Shelter   Junior Member
 
 Posts: 22
 Threads: 5
 Joined: Mar 2018
 Reputation: 
0
 | 
			| RE: Item Spawns Randomly 
 
				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?
			 |  |  
	| 04-06-2018, 12:50 PM |  |  
	
		| Mudbill   Muderator
 
 Posts: 3,881
 Threads: 59
 Joined: Apr 2013
 Reputation: 
179
 | 
			| RE: Spawning Item Randomly 
 
				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.
 
 |  |  
	| 04-06-2018, 10:12 PM |  |  
	
		| Organic Shelter   Junior Member
 
 Posts: 22
 Threads: 5
 Joined: Mar 2018
 Reputation: 
0
 | 
			| RE: Spawning Item Randomly 
 
				 (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?
			 |  |  
	| 04-08-2018, 05:03 AM |  |  
	
		| Mudbill   Muderator
 
 Posts: 3,881
 Threads: 59
 Joined: Apr 2013
 Reputation: 
179
 | 
			| RE: Spawning Item Randomly 
 
				Within your OnStart function, add the AddUseItemCallback, which will trigger an event when you use a certain item on a certain object. 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:
 void YourCustomFunction(string &in asItem, string &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.
 }
 
 
				
(This post was last modified: 04-08-2018, 04:58 PM by Mudbill.)
 |  |  
	| 04-08-2018, 04:57 PM |  |  |