Multiple object creation / variable reassignment inside a loop
Aornar
Member Posts: 4
Cannot get this to work for the life of me. I have a number of waypoints which all use the same tag. The idea is upon using a lever, the script cycles through these waypoints by getting each one (its location), spawning an object at that location then deleting the original waypoint. On the next iteration of the loop, it should find the -next- waypoint with that ID, repeating the process until all the waypoints have been deleted and the objects have spawned.
I currently have the following snippet:
==========================
object wp;
location loc;
int i;
for (i = 0; i < 10; i++){
wp = GetWaypointByTag("wp_flamesappear");
loc = GetLocation(wp);
DestroyObject(wp);
//wp = GetWaypointByTag("nonsense_ref");
CreateObjectVoid(OBJECT_TYPE_PLACEABLE, "some_template", loc);
}
============================
Unfortunately, the original declaration wp=GetWaypointByTag seems to cache the first waypoint object, even when it is later deleted. I have tried manually unassigning the pointer, but this doesn't seem to work. Triggering the OnUsed of the controlling object (lever, in this case) DOES retrieve the new object, but this means that I have to click the lever separately for each instance of wp_flamesappear I have.
Has anyone come across this issue before? Is there a workaround or am I missing some vital part?
Thanks.
I currently have the following snippet:
==========================
object wp;
location loc;
int i;
for (i = 0; i < 10; i++){
wp = GetWaypointByTag("wp_flamesappear");
loc = GetLocation(wp);
DestroyObject(wp);
//wp = GetWaypointByTag("nonsense_ref");
CreateObjectVoid(OBJECT_TYPE_PLACEABLE, "some_template", loc);
}
============================
Unfortunately, the original declaration wp=GetWaypointByTag seems to cache the first waypoint object, even when it is later deleted. I have tried manually unassigning the pointer, but this doesn't seem to work. Triggering the OnUsed of the controlling object (lever, in this case) DOES retrieve the new object, but this means that I have to click the lever separately for each instance of wp_flamesappear I have.
Has anyone come across this issue before? Is there a workaround or am I missing some vital part?
Thanks.
0
Comments
Adding a SetTag function on the object now allows the loop to cycle through all the objects correctly.
Have not tested the script, but seems logical.
For example, if you want this function run when a PC enters an area, you could make an OnEnter script for the area that checks if the entering object is a player, and in that script SignalEvent(EventUserDefined(object oControlObject, int nEventNumber)).
Then the control object's UserDefined event script would run the object creation function that you want to implement.
Having seen that you want this effect achieved by using a lever, then it's even easier than I described. If your code snippet is in the lever's OnUse event, just change it to:
Therefore, on the second and every subsequent run through the for loop, the first item still exists and is still valid to be found.