Jump to content
Heavy Traffic ×

Trying to Make New Modder's Resource Script To mass replace Static objects


pyrotx

Recommended Posts

I am currently working a mod where I needed to make a new dynamic script to replace all object references of a certain base object type. For example to replace all TreePine01 within a radius with TreeAspen01 trees at the same 3d coordinates and 3d Angle. Or to replace all stone floor tiles in a radius with wood floor tiles.

 

I made the script and it does compile with no errors, but when a tried to test it in game it didn't do anything. I was hoping some might be able to help me figure out why. The script below was attached to a ordinary dwemer button, I specifically attached it to the Object Reference, not the base Object, When pressing the button it didn't do anything.

 

I will post the the script below.

 

Did I make the function wrong, like not pass arguments through it?

Or maybe was I using the wrong event types?

Did I make a simple logic error?

Is it the button activator that doesn't fit the event type or script, like maybe should I use a trigger box instead?

 

If anyone could help me figure out what is wrong that would be a great help. And hopefully once I get this working I can upload it as a much needed modder's resource to nexusmods.

 

 

 

 

Scriptname ReplaceAllRefsOfTypeScript extends ObjectReference ; Script to replace all Static items of a specific type with another.
; For Example to replace all wood floors in a house with stone floors.

;==============================================================================
; PROPERTIES
;======================================================================
ObjectReference Property SearchRef Auto
{The Object Reference to be the center of the search radius.}
Static Property RefType Auto
{The base static object to be replaced}
Static Property RefReplacement Auto
{The new base static object to replace the previous one.}
Float Property LoopSize = 100.0 Auto
{How long the while loop for scanning multiple object will be. (Default 100)}
Float Property SearchRadius = 2048.0 Auto
{Radius size to scan for objects to replace. (Default 2048)}

;==============================================================================
; VARIABLES
;======================================================================
float PosX
float PosY
float PosZ

float aX
float aY
float aZ

ObjectReference RefToReplace
ObjectReference NewRef

;==============================================================================
; EVENTS
;======================================================================
Event OnInit()
RefToReplace = Game.FindRandomReferenceOfTypeFromRef(RefType, SearchRef, SearchRadius)
NewRef = RefToReplace.PlaceAtMe(RefReplacement)
EndEvent

Event OnActivate(ObjectReference akActionRef)
ReplaceRef()
EndEvent

;==============================================================================
; FUNCTIONS
;======================================================================
ObjectReference Function ReplaceRef()
int i = 0

While(i < LoopSize) ; This while loop first scans for the object to replace and gets it's 3d Position and Angles in xyz coords.
RefToReplace ; Then creates a new replacement object at the original and sets the new object's position and angle
; to match the original's. After that disables and deletes the original object.
PosX = RefToReplace.GetPositionX()
posY = RefToReplace.GetPositionY()
PosZ = RefToReplace.GetPositionZ()

aX = RefToReplace.GetAngleX()
aY = RefToReplace.GetAngleY()
aZ = RefToReplace.GetAngleZ()

NewRef

NewRef.SetPosition(posX, posY, PosZ)
NewRef.SetAngle(aX, aY, aZ)

RefToReplace.Disable()
RefToReplace.Delete()

i += i
endWhile
EndFunction

Link to comment
Share on other sites

OnInit only runs when the object is first loaded. If you want it to run when you press the button, use OnActivate instead. Also you custom function is set to return an ObjectReference but does not actually return anything. If you do not need it to return something, drop the ObjectReference from the beginning of the function definition.

Link to comment
Share on other sites

Thank you so much IsharaMeradin for the very quick advise. I tried what you said and got the script working for the most part with just some minor tweaks left. Thank you again, i'm glad finally one of my posts over the years actually got a reply.

 

Now the only problem seams to be that the script doesn't use the while loop in the function. Instead I have to click on the activator button for each object to be replaced.

But at least it's working, so back to compiler it is then.

 

Also Is it possible to use a while loop outside of a function, because my compiler said it wasn't, or did I just misread it?

If anyone does happen to know why the while loop isn't working I would be grateful for any final bits of help.

Edited by pyrotx
Link to comment
Share on other sites

If your new version of the script is anything like the one previously posted, you are defining RefToReplace outside of the while loop. This means that it will use that single found reference and try to replace it with the NewRef on each loop. Define RefToReplace inside the while loop and it will get a new reference each time (at least it will have a better shot at it).

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...