Note: Sorry for the long post. I thought experienced modders would be better able to help me if I showed them my thought process. I will try to highlight the important stuff for those that can't be bothered to read all of it
Hi everyone, I'm trying to write this experimental script mainly for the purpose of learning. The goal is simple: create an actor reference via script and then make him follow a simple patrol path to a marker and back using the pathToReference() function. But of course as many of you know (and as I have found out), pathToReference is a very unreliable function.
Q: Why use pathToReference when you can use a package? Packages are far more reliable.
A: Explaining this would take a wall of text so hopefully you'll just trust me when I say that pathToReference would be the best and easiest option for what I will be attempting later on IF I could get it to work reliably.
So first I came up with this simple script:
Scriptname TestPatrolScript extends ObjectReference {Creates actors that patrol routes}objectReference property spawnMarker autoobjectReference property gotoMarker autoactorBase property patrolSoldierBase autoevent onEquipped(actor akActor) if akActor == game.getPlayer() actor patrolSoldier = (spawnMarker.placeAtMe(patrolSoldierBase, 1, true)) as actor utility.wait(2) patrolSoldier.pathToReference(gotoMarker, 0.5) utility.wait(5) patrolSoldier.pathToReference(spawnMarker, 0.5) endIfendEvent
The problem was that once pathToReference was interrupted, the actor would not resume his path (unlike packages). So for example if I tried to talk to him while he was on his way, he would stop, and not move again. So I looked up the function in the wiki and it turns out the function returns "true" if the path was successfully completed and "false" if it failed for some reason. I thought, "great, I can find out when he is interrupted because pathToReference would return a "false" boolean. Then I can force him to keep going with another pathToReference command." Unfortunately it turns out that for some reason the function always returns "true" irrespective of whether or not the actor successfully completed the path. So that seemed to be not an option.
So I decided to try the slightly more complex script below:
Scriptname TestPatrolScript extends ObjectReference {Creates actors that patrol routes}objectReference property spawnMarker autoobjectReference property gotoMarker autoactorBase property patrolSoldierBase autofunction myPatrolFunc(actor soldier, objectReference startMarker, objectReference endMarker) bool p1c = false bool p2c = true while true if p2c soldier.pathToReference(endMarker, 0.5) utility.wait(5) if ((soldier as objectReference).getDistance(endMarker)) < 100 p1c = true else p1c = false endIf endIf if p1c soldier.pathToReference(startMarker, 0.5) utility.wait(5) if ((soldier as objectReference).getDistance(startMarker)) < 100 p2c = true else p2c = false endIf endIf utility.wait(2) endWhileendFunctionevent onEquipped(actor akActor) if akActor == game.getPlayer() actor patrolSoldier = (spawnMarker.placeAtMe(patrolSoldierBase, 1, true)) as actor utility.wait(2) myPatrolFunc(patrolSoldier, spawnMarker, gotoMarker) endIfendEvent
The idea was that with this while-loop I could give him a pathToReference command over and over to make sure that when he's interrupted, he will keep going. Unfortunately this only partially worked. So sometimes when I interrupted him, he would resume his path, and sometimes he wouldn't. After doing some debugging I realized that the problem is pathToReference not ending properly. pathToRef is a latent function, and sometimes it does not end when the actor is interrupted. So the actor is interrupted, he stands idle in one spot, but the pathToReference() function does not "realize" that he has stopped. Therefore the function continues to halt the rest of my code waiting for the actor to reach his destination - which of course will never happen.
So I think what I need to do is to somehow figure out when he is not moving ---> means that he is interrupted ---> then somehow force the pathToReference() function to end if it hasn't ended already. Or perhaps there is a better, more efficient way of doing this? Any help would be greatly appreciated.