Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script Help Spawning Item Randomly
Organic Shelter Offline
Junior Member

Posts: 22
Threads: 5
Joined: Mar 2018
Reputation: 0
#1
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.
(This post was last modified: 04-06-2018, 01:55 PM by Organic Shelter.)
04-01-2018, 08:22 AM
Find
Romulator Offline
Not Tech Support ;-)

Posts: 3,628
Threads: 63
Joined: Jan 2013
Reputation: 195
#2
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.

PHP Code: (Select All)
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.

Discord: Romulator#0001
[Image: 3f6f01a904.png]
04-01-2018, 04:04 PM
Find
Organic Shelter Offline
Junior Member

Posts: 22
Threads: 5
Joined: Mar 2018
Reputation: 0
#3
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.

PHP Code: (Select All)
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?
04-05-2018, 12:33 PM
Find
Romulator Offline
Not Tech Support ;-)

Posts: 3,628
Threads: 63
Joined: Jan 2013
Reputation: 195
#4
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]
04-05-2018, 01:00 PM
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#5
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
Find
Organic Shelter Offline
Junior Member

Posts: 22
Threads: 5
Joined: Mar 2018
Reputation: 0
#6
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
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#7
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
Find
Organic Shelter Offline
Junior Member

Posts: 22
Threads: 5
Joined: Mar 2018
Reputation: 0
#8
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
Find
Mudbill Offline
Muderator

Posts: 3,881
Threads: 59
Joined: Apr 2013
Reputation: 179
#9
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.

PHP Code: (Select All)
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: (Select All)
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.


(This post was last modified: 04-08-2018, 04:58 PM by Mudbill.)
04-08-2018, 04:57 PM
Find




Users browsing this thread: 1 Guest(s)