Jump to content

Code to get an object based on distance


SMB92

Recommended Posts

You know it just occured to me, I can just make copies of said script and name them different (ASC-SP1, 2 etc) and place them on the objects, and place the objects strategically so that 2 the same never load simultaneously. Then there is no need for any of this

 

Thanks all for the brainstorm, all info is good.

Link to comment
Share on other sites

  • Replies 40
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

You know it just occured to me, I can just make copies of said script and name them different (ASC-SP1, 2 etc) and place them on the objects, and place the objects strategically so that 2 the same never load simultaneously. Then there is no need for any of this

 

Thanks all for the brainstorm, all info is good.

Do I unerstanbd you correct? You want to make many copies of the same script ? I`m confused on why would you do that.

Link to comment
Share on other sites

 

You know it just occured to me, I can just make copies of said script and name them different (ASC-SP1, 2 etc) and place them on the objects, and place the objects strategically so that 2 the same never load simultaneously. Then there is no need for any of this

 

Thanks all for the brainstorm, all info is good.

ÃÂ Do I unerstanbd you correct? You want to make many copies of the same scriptÃÂ ? I`mÃÂ confused on why would you do that.
So they dont interfere with each other if they load at the same time.

 

Heres a nice example of what i mean by interfere with each - make a simple script with a timer that does something like placeactoratme ontimer and place it on 2 markers you put next to each other in the same cell. Use the oncellattach event. Walk into that cell. You should have only one NPC standing there. Same shiz happens with all variables set.

 

So i make a rule, dont have object with "same" script in a 2 cell radius. The spawn code is very fast so it should be fired long before you load 2 cells even when sprinting.

 

Hopefully that makes sense. I'd only need 5 or so copies for what I'd be doing.

Link to comment
Share on other sites

 

 

You know it just occured to me, I can just make copies of said script and name them different (ASC-SP1, 2 etc) and place them on the objects, and place the objects strategically so that 2 the same never load simultaneously. Then there is no need for any of this

 

Thanks all for the brainstorm, all info is good.

ÃÃÃÃÂ Do I unerstanbd you correct? You want to make many copies of the same scriptÃÃÃÃÂ ? I`mÃÃÃÃÂ confused on why would you do that.
So they dont interfere with each other if they load at the same time.

 

Heres a nice example of what i mean by interfere with each - make a simple script with a timer that does something like placeactoratme ontimer and place it on 2 markers you put next to each other in the same cell. Use the oncellattach event. Walk into that cell. You should have only one NPC standing there. Same shiz happens with all variables set.

 

So i make a rule, dont have object with "same" script in a 2 cell radius. The spawn code is very fast so it should be fired long before you load 2 cells even when sprinting.

 

Hopefully that makes sense. I'd only need 5 or so copies for what I'd be doing.

That's not how scripts work in Papyrus. Two objects with the same script attached to them will not interfere with each other unless you explicitly code that to happen. Your "rule" isn't necessary and is complicating your mod design. Edited by Reneer
Link to comment
Share on other sites

 

 

You know it just occured to me, I can just make copies of said script and name them different (ASC-SP1, 2 etc) and place them on the objects, and place the objects strategically so that 2 the same never load simultaneously. Then there is no need for any of this

 

Thanks all for the brainstorm, all info is good.

ÃÂ Do I unerstanbd you correct? You want to make many copies of the same scriptÃÂ ? I`mÃÂ confused on why would you do that.
So they dont interfere with each other if they load at the same time.

 

Heres a nice example of what i mean by interfere with each - make a simple script with a timer that does something like placeactoratme ontimer and place it on 2 markers you put next to each other in the same cell. Use the oncellattach event. Walk into that cell. You should have only one NPC standing there. Same shiz happens with all variables set.

 

So i make a rule, dont have object with "same" script in a 2 cell radius. The spawn code is very fast so it should be fired long before you load 2 cells even when sprinting.

 

Hopefully that makes sense. I'd only need 5 or so copies for what I'd be doing.

 

Interesting. I do have a script that spawns an object when the player enters the workshop. It`s attached to a craftable object. And I can build 5, 10, 15 of them, they all have the same scipt, but each of them will spawn the object when I enter the workshop. They do not interfere. I don`t know what it was caused by, but there could be another reason why it did not work.

 

Reneer is right. When I was typing, I had not seen the post. But anyway, this just confirms what he said.

Edited by kitcat81
Link to comment
Share on other sites

 

 

 

You know it just occured to me, I can just make copies of said script and name them different (ASC-SP1, 2 etc) and place them on the objects, and place the objects strategically so that 2 the same never load simultaneously. Then there is no need for any of this

 

Thanks all for the brainstorm, all info is good.

ÃÃÃÃÃÃÂ Do I unerstanbd you correct? You want to make many copies of the same scriptÃÃÃÃÃÃÂ ? I`mÃÃÃÃÃÃÂ confused on why would you do that.
So they dont interfere with each other if they load at the same time.

 

Heres a nice example of what i mean by interfere with each - make a simple script with a timer that does something like placeactoratme ontimer and place it on 2 markers you put next to each other in the same cell. Use the oncellattach event. Walk into that cell. You should have only one NPC standing there. Same shiz happens with all variables set.

 

So i make a rule, dont have object with "same" script in a 2 cell radius. The spawn code is very fast so it should be fired long before you load 2 cells even when sprinting.

 

Hopefully that makes sense. I'd only need 5 or so copies for what I'd be doing.

That's not how scripts work in Papyrus. Two objects with the same script attached to them will not interfere with each other unless you explicitly code that to happen. Your "rule" isn't necessary and is complicating your mod design.
Then why do I keep seeing the contrary. Two markers with same script in cells side by side, the one that ran ever so slightly after the first seemingly changed the variables I am storing to the new ones it made. So for example 3 gunners out of 7 spawn at the first marker but then 11 forged spawn (4 at first marker). And I can see them coming from there because they are linking to different patrol markers on the forked road at Cocord past the RR and walking together.

 

Tell my why that happens because initially I was of the same thinking until this shiz keeps happening?

Link to comment
Share on other sites

Can you paste the script and explain what effect you wanted to achieve and what exactly went wrong. And what variables did you store in the script? ( variables can be lost in a cell that resets so you`d need to use oninit event to set them again) .

Link to comment
Share on other sites

Sure, see below. This is the "random" type spawn (I'm making other types like Ambush that are controlled separately) The variables are only stored for the few milliseconds it takes to make the spawn, then the code is gone nothing is stored longer than that.

 

Note that I haven't written the code to grab the closest patrol marker, I was just placing a property for testing. Also, you'll note the "reroll" code, that chance number is set to 0.

 

The R1 in everything means Region 1. I have divided the map into 12 hypothetical regions. The sheer amount of globals is so the player can set them from a menu should they so wish to fine tune things (there are 3 presets and 4 difficulty levels however).

 

 

 

Scriptname asc_sp_normal_r1 extends ObjectReference

GlobalVariable Property ASC_Main_ModEnabled Auto
GlobalVariable Property ASC_Main_RandomEnabled_R1 Auto
GlobalVariable Property ASC_Main_Random_Chance_R1 Auto
GlobalVariable Property ASC_Main_Random_DisableOnBlock_R1 Auto
FormList Property ASC_ResetList_R1 Auto
ActorBase property LvlRaider Auto
ActorBase Property LvlGunner Auto
ActorBase Property LvlForged Auto
ActorBase Property LvlTriggerman Auto
GlobalVariable Property ASC_Main_Difficulty_R1 Auto
GlobalVariable Property ASC_Allowed_Raider_R1 Auto
GlobalVariable Property ASC_Allowed_RaiderBoss_R1 Auto
GlobalVariable Property ASC_Max_Allowed_Raider_R1 Auto
GlobalVariable Property ASC_Max_Allowed_RaiderBoss_R1 Auto
GlobalVariable Property ASC_Chance_Raider_R1 Auto
GlobalVariable Property ASC_Reroll_Allowed_Raider_R1 Auto
GlobalVariable Property ASC_Reroll_Chance_Raider_R1 Auto
GlobalVariable Property ASC_Chance_RaiderBoss_R1 Auto
GlobalVariable Property ASC_Allowed_Gunner_R1 Auto
GlobalVariable Property ASC_Allowed_GunnerBoss_R1 Auto
GlobalVariable Property ASC_Max_Allowed_Gunner_R1 Auto
GlobalVariable Property ASC_Max_Allowed_GunnerBoss_R1 Auto
GlobalVariable Property ASC_Chance_Gunner_R1 Auto
GlobalVariable Property ASC_Reroll_Allowed_Gunner_R1 Auto
GlobalVariable Property ASC_Reroll_Chance_Gunner_R1 Auto
GlobalVariable Property ASC_Chance_GunnerBoss_R1 Auto
GlobalVariable Property ASC_Allowed_Forged_R1 Auto
GlobalVariable Property ASC_Allowed_ForgedBoss_R1 Auto
GlobalVariable Property ASC_Max_Allowed_Forged_R1 Auto
GlobalVariable Property ASC_Max_Allowed_ForgedBoss_R1 Auto
GlobalVariable Property ASC_Chance_Forged_R1 Auto
GlobalVariable Property ASC_Reroll_Allowed_Forged_R1 Auto
GlobalVariable Property ASC_Reroll_Chance_Forged_R1 Auto
GlobalVariable Property ASC_Chance_ForgedBoss_R1 Auto
GlobalVariable Property ASC_Allowed_Tmen_R1 Auto
GlobalVariable Property ASC_Allowed_TmenBoss_R1 Auto
GlobalVariable Property ASC_Max_Allowed_Tmen_R1 Auto
GlobalVariable Property ASC_Max_Allowed_TmenBoss_R1 Auto
GlobalVariable Property ASC_Chance_Tmen_R1 Auto
GlobalVariable Property ASC_Reroll_Allowed_Tmen_R1 Auto
GlobalVariable Property ASC_Reroll_Chance_Tmen_R1 Auto
GlobalVariable Property ASC_Chance_TmenBoss_R1 Auto
GlobalVariable Property ASC_Reroll_Main_Chance_R1 Auto
GlobalVariable Property ASC_Reroll_Main_R1 Auto

Int ChanceToSpawn = 0
Int Difficulty = 0
Int WhoToSpawn = 0
Int SpawnCounter = 0
Int SpawnCounterBoss = 0
Bool SpawnSuccess = false
Bool Rerolled = false
Actor Spawn = None

Event OnCellAttach()
	if (Self.IsDisabled() == false) && (ASC_Main_ModEnabled.GetValueInt() == 1)&& (ASC_Main_RandomEnabled_R1.GetValueInt() == 1)
		BeginActivation()
	endif
EndEvent

Function BeginActivation()
	ChanceToSpawn = Utility.RandomInt(1,100)	
	if (ChanceToSpawn <= ASC_Main_Random_Chance_R1.GetValueInt())
		Spawn()
		Self.Disable()
		ASC_ResetList_R1.AddForm(Self)
		Debug.Notification("Controller Disabled and added to formlist")
		Debug.Notification("SC Activation Sent")
	elseif (ChanceToSpawn >= ASC_Main_Random_Chance_R1.GetValueInt()) && (ASC_Main_Random_DisableOnBlock_R1.GetValueInt() == 1)
		Self.Disable()
		ASC_ResetList_R1.AddForm(Self)
		Debug.Notification("SC Unsuccessful and disabled")
	else
		;Nothing
	endif
EndFunction

Function RerollOnSuccess()
	if (SpawnSuccess == true) && (Rerolled == false) && (ASC_Reroll_Main_R1.GetValueInt() == 1) && (Utility.RandomInt(1,100) <= ASC_Reroll_Main_Chance_R1.GetValueInt())
		Spawn()
		Debug.Notification("Spawning another group!")
	else
		Difficulty = 0
		WhoToSpawn = 0
		SpawnSuccess = false
		Rerolled = false
		Spawn = none
		SpawnCounter = 0
		SpawnCounterBoss = 0
		Debug.Notification("Failed to Reroll another group!")
	endif	
EndFunction

Function Spawn()
	Difficulty = ASC_Main_Difficulty_R1.GetValueInt()
	WhoToSpawn = Utility.RandomInt(1,4)
		if (WhoToSpawn == 1)  && (ASC_Allowed_Raider_R1.GetValueInt() == 1)
			SpawnRaider()
			if ASC_Allowed_RaiderBoss_R1.GetValueInt() == 1
				SpawnraiderBoss()
			endif
		elseif (WhoToSpawn == 1)  && (ASC_Allowed_Raider_R1.GetValueInt() == 0) && (ASC_Reroll_Allowed_Raider_R1.GetValueInt() == 1)
			RerollRaiderR1()
		elseif (WhoToSpawn == 2)  && (ASC_Allowed_Gunner_R1.GetValueInt() == 1)
			SpawnGunner()
				if ASC_Allowed_GunnerBoss_R1.GetValueInt() == 1
					SpawnGunnerBoss()
				endif
		elseif (WhoToSpawn == 2)  && (ASC_Allowed_Gunner_R1.GetValueInt() == 0) && (ASC_Reroll_Allowed_Gunner_R1.GetValueInt() == 1)
			RerollGunnerR1()
		elseif (WhoToSpawn == 3)  && (ASC_Allowed_Forged_R1.GetValueInt() == 1)
			SpawnForged()
				if ASC_Allowed_ForgedBoss_R1.GetValueInt() == 1
					SpawnForgedBoss()
				endif
		elseif (WhoToSpawn == 3)  && (ASC_Allowed_Forged_R1.GetValueInt() == 0) && (ASC_Reroll_Allowed_Forged_R1.GetValueInt() == 1)
			RerollForgedR1()
		elseif (WhoToSpawn == 4)  && (ASC_Allowed_Tmen_R1.GetValueInt() == 1)
			SpawnTmen()
				if ASC_Allowed_TmenBoss_R1.GetValueInt() == 1
					SpawnTmenBoss()
				endif
		elseif (WhoToSpawn == 4)  && (ASC_Allowed_Tmen_R1.GetValueInt() == 0) && (ASC_Reroll_Allowed_Tmen_R1.GetValueInt() == 1)
			RerollTmenR1()
		endif
EndFunction

Function SpawnRaider()
	SpawnCounter = ASC_Max_Allowed_Raider_R1.GetValueInt()
	Debug.Notification("Raider Spawning")
		while (SpawnCounter > 0)
			if (Utility.RandomInt(1,100) <= ASC_Chance_Raider_R1.GetValueInt())
				Spawn = Self.PlaceActorAtMe(LvlRaider, Utility.RandomInt(1,Difficulty), None)
				Spawn.SetLinkedRef(PatrolMarker)
				SpawnCounter = SpawnCounter - 1
				Spawn = None
				SpawnSuccess = True
				Debug.Notification("Raider Spawned")
			else
				SpawnCounter = SpawnCounter - 1
				Debug.Notification("Raider Spawn Blocked but still counting")
			endif
		endwhile
EndFunction
			
Function SpawnRaiderBoss()
	SpawnCounterBoss = ASC_Max_Allowed_RaiderBoss_R1.GetValueInt()
		while (SpawnCounterBoss) > 0
			if (Utility.RandomInt(1,100) <= ASC_Chance_RaiderBoss_R1.GetValueInt())
				Spawn = Self.PlaceActorAtMe(LvlRaider, Utility.RandomInt(1,Difficulty), None)
				Spawn.SetLinkedRef(PatrolMarker)
				SpawnCounterBoss = SpawnCounterBoss - 1
				Spawn = None
				Debug.Notification("Raider Boss Spawned")
			else
				SpawnCounterBoss = SpawnCounterBoss - 1
				Debug.Notification("Raider Boss Spawn Blocked but still counting")
			endif
		endwhile
EndFunction

Function RerollRaiderR1()
	if (Rerolled == false) && (Utility.RandomInt(1,100) <= ASC_Reroll_Chance_Raider_R1.GetValueInt())
		Spawn()
		Rerolled = true
		Difficulty = 0
		WhoToSpawn = 0
		Debug.Notification("Rerolling after Raiders Blocked")
	else
		Difficulty = 0
		WhoToSpawn = 0
		Debug.Notification("Reroll on blocked Raiders denied")
	endif
EndFunction

Function SpawnGunner()
	SpawnCounter = ASC_Max_Allowed_Gunner_R1.GetValueInt()
	Debug.Notification("Gunner Spawning")
		while (SpawnCounter > 0)
			if (Utility.RandomInt(1,100) <= ASC_Chance_Gunner_R1.GetValueInt())
				Spawn = Self.PlaceActorAtMe(LvlGunner, Utility.RandomInt(1,Difficulty), None)
				Spawn.SetLinkedRef(PatrolMarker)
				SpawnCounter = SpawnCounter - 1
				Spawn = None
				SpawnSuccess = True
				Debug.Notification("Gunner Spawned")
			else
				SpawnCounter = SpawnCounter - 1
				Debug.Notification("Gunner Spawn Blocked but still counting")
			endif
		endwhile
EndFunction
			
Function SpawnGunnerBoss()
	SpawnCounterBoss = ASC_Max_Allowed_GunnerBoss_R1.GetValueInt()
		while (SpawnCounterBoss > 0)
			if (Utility.RandomInt(1,100) <= ASC_Chance_GunnerBoss_R1.GetValueInt())
				Spawn = Self.PlaceActorAtMe(LvlGunner, Utility.RandomInt(1,Difficulty), None)
				Spawn.SetLinkedRef(PatrolMarker)
				SpawnCounterBoss = SpawnCounterBoss - 1
				Spawn = None
				Debug.Notification("Gunner Boss Spawned")
			else
				SpawnCounterBoss = SpawnCounterBoss - 1
				Debug.Notification("Gunner Boss Spawn Blocked but still counting")
			endif
		endwhile
EndFunction

Function RerollGunnerR1()
	if  (Rerolled == false) && (Utility.RandomInt(1,100) <= ASC_Reroll_Chance_Gunner_R1.GetValueInt())
		Spawn()
		Rerolled = true
		Difficulty = 0
		WhoToSpawn = 0
		Debug.Notification("Rerolling after Gunners Blocked")
	else
		Difficulty = 0
		WhoToSpawn = 0
		Debug.Notification("Reroll on blocked Gunners denied")
	endif
EndFunction

Function SpawnForged()
	SpawnCounter = ASC_Max_Allowed_Forged_R1.GetValueInt()
	Debug.Notification("Forged Spawning")
		while (SpawnCounter > 0)
			if (Utility.RandomInt(1,100) <= ASC_Chance_Forged_R1.GetValueInt())
				Spawn = Self.PlaceActorAtMe(LvlForged, Utility.RandomInt(1,Difficulty), None)
				Spawn.SetLinkedRef(PatrolMarker)
				SpawnCounter = SpawnCounter - 1
				Spawn = None
				SpawnSuccess = True
				Debug.Notification("Forged Spawned")
			else
				SpawnCounter = SpawnCounter - 1
				Debug.Notification("Forged Spawn Blocked but still counting")
			endif
		endwhile
EndFunction
			
Function SpawnForgedBoss()
	SpawnCounterBoss = ASC_Max_Allowed_ForgedBoss_R1.GetValueInt()
		while (SpawnCounterBoss > 0)
			if (Utility.RandomInt(1,100) <= ASC_Chance_ForgedBoss_R1.GetValueInt())
				Spawn = Self.PlaceActorAtMe(LvlForged, Utility.RandomInt(1,Difficulty), None)
				Spawn.SetLinkedRef(PatrolMarker)
				SpawnCounterBoss = SpawnCounterBoss - 1
				Spawn = None
				Debug.Notification("Forged Boss Spawned")
			else
				SpawnCounterBoss = SpawnCounterBoss - 1
				Debug.Notification("Forged Boss Spawn Blocked but still counting")
			endif
		endwhile
EndFunction

Function RerollForgedR1()
	if  (Rerolled == false) && (Utility.RandomInt(1,100) <= ASC_Reroll_Chance_Forged_R1.GetValueInt())
		Spawn()
		Rerolled = true
		Difficulty = 0
		WhoToSpawn = 0
		Debug.Notification("Rerolling after Forgeds Blocked")
	else
		Difficulty = 0
		WhoToSpawn = 0
		Debug.Notification("Reroll on blocked Forgeds denied")
	endif
EndFunction

Function SpawnTmen()
	SpawnCounter = ASC_Max_Allowed_Tmen_R1.GetValueInt()
	Debug.Notification("Tmen Spawning")
		while (SpawnCounter > 0)
			if (Utility.RandomInt(1,100) <= ASC_Chance_Tmen_R1.GetValueInt())
				Spawn = Self.PlaceActorAtMe(LvlTriggerman, Utility.RandomInt(1,Difficulty), None)
				Spawn.SetLinkedRef(PatrolMarker)
				SpawnCounter = SpawnCounter - 1
				Spawn = None
				SpawnSuccess = True
				Debug.Notification("Tmen Spawned")
			else
				SpawnCounter = SpawnCounter - 1
				Debug.Notification("Tmen Spawn Blocked but still counting")
			endif
		endwhile
EndFunction
			
Function SpawnTmenBoss()
	SpawnCounterBoss = ASC_Max_Allowed_TmenBoss_R1.GetValueInt()
		while (SpawnCounterBoss > 0)
			if (Utility.RandomInt(1,100) <= ASC_Chance_TmenBoss_R1.GetValueInt())
				Spawn = Self.PlaceActorAtMe(LvlTriggerman, Utility.RandomInt(1,Difficulty), None)
				Spawn.SetLinkedRef(PatrolMarker)
				SpawnCounterBoss = SpawnCounterBoss - 1
				Spawn = None
				Debug.Notification("Tmen Boss Spawned")
			else
				SpawnCounterBoss = SpawnCounterBoss - 1
				Debug.Notification("Tmen Boss Spawn Blocked but still counting")
			endif
		endwhile
EndFunction

Function RerollTmenR1()
	if  (Rerolled == false) && (Utility.RandomInt(1,100) <= ASC_Reroll_Chance_Tmen_R1.GetValueInt())
		Spawn()
		Rerolled = true
		Difficulty = 0
		WhoToSpawn = 0
		Debug.Notification("Rerolling after Tmens Blocked")
	else
		Difficulty = 0
		WhoToSpawn = 0
		Debug.Notification("Reroll on blocked Tmens denied")
	endif
EndFunction

 

 

 

EDIT: wow that looks so pretty in color. How can I get Notepad++ to look like that?

Link to comment
Share on other sites

Here is an example:

 

Let's say you have two actors, lets call them actor1 and actor2. Both have same script. Let's call it TestScript. Script is registered for player distance and is using busy state when player steps close enough.

 

Player steps close to actor1 and then 0.5s later steps close to actor2. Actor1 makes TestScript instance 1 and goes to busy state. Actor2 makes script instance 2 but it has NO idea that Testscript instance 1 is in busy state, so both works like they have no idea other one exists. The only way to make them affect same things is to try them both change same globalvariable or access/change property on another script, or both try change players actor value etc..

 

What I think you might want to do is: Have your normal script, then that script calls function in Quest script and that quest script is using states, busy state. So, same example, now Actor2 is calling function, but the quest script is in busy state and nothing happens.

Edited by vkz89q
Link to comment
Share on other sites

I see, but can you please clarify what was not working es expected. The only thing I want to say. I don`t why it works so and I don`t have a logical explanation now, but if you call randomint in exactly the the same moment of time, it can cause some kind of bug sometimes. I remember this from an experiment with changing settlers hairstyles when using the randomint ended up so that only the first settler got a new hairstyle and all others got the same hairstyle. To avoid this I used utilty.Wait(utility.RandomFloat(0, 5)) and then untility.RandomInt .

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.

×
×
  • Create New...