• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • It's time for the first HD Modeling Contest of 2025. Join the theme discussion for Hive's HD Modeling Contest #7! Click here to post your idea!

Removing point breaks code

Status
Not open for further replies.
Level 3
Joined
Jun 23, 2020
Messages
14
Hey, I'm having issues doing something very basic. I want to check wether a unit is moving by saving its location, then every 0.1s check if its a certain distance from that location and if so, saving the new one, and repeat. etc. Naturally this involves saving a whole bunch of points, so of course I want to remove them. But when I do, the code no longer works


point removal1.png


This code runs the way I want it, but it doesnt clean up all of the points...
But when I try to remove the tempPoint variable, no matter where i put the command, the code no longer works and the distance (real value) returned from line 2 becomes really big

Here's one example that doesnt work:
point removal2.png


I really don't get this, ive removed similar points before with no problem, can anyone help me here?

Thanks
 

Attachments

  • point removal1.png
    point removal1.png
    17.9 KB · Views: 10
  • point removal2.png
    point removal2.png
    15.6 KB · Views: 8
Last edited:
Level 24
Joined
Jun 26, 2020
Messages
1,921
I have understood that removing a null location breaks the game, so in the line:
  • Custom script: call RemoveLocation(udg_PlayerLocation[1])
You destroy a potential null location, because you don't set it before, for more safety add these lines:
  • Custom script: if udg_PlayerLocation[1] != null then
  • Custom script: call RemoveLocation(udg_PlayerLocation[1])
  • Custom script: endif
 
Level 19
Joined
Jan 1, 2018
Messages
739
Locations are reference types, not value types. What this means is that if you do "set Player_Location[1] = tempPoint", both variables point to the same location object, it does NOT make a copy. Then the next thing you do is "RemoveLocation(tempPoint)". Because they are the same location object, Player_Location[1] is now removed as well.

To properly clean up all leaks: in the trigger, you create one point, then do an if/then/else. You do not create any new points in the then block nor in the else block. So you only need to destroy one location in the then and else blocks. So in your first example you already clean up properly in the then block, no need to add a second RemoveLocation.
However, you don't clean the location in the else block, so here you should add "call RemoveLocation(udg_tempPoint)".
 
Level 3
Joined
Jun 23, 2020
Messages
14
I have understood that removing a null location breaks the game, so in the line:
  • Custom script: call RemoveLocation(udg_PlayerLocation[1])
You destroy a potential null location, because you don't set it before, for more safety add these lines:
  • Custom script: if udg_PlayerLocation[1] != null then
  • Custom script: call RemoveLocation(udg_PlayerLocation[1])
  • Custom script: endif
I do set the variable to something upon map initialization, but thats good insight anyways, thanks.

Locations are reference types, not value types. What this means is that if you do "set Player_Location[1] = tempPoint", both variables point to the same location object, it does NOT make a copy. Then the next thing you do is "RemoveLocation(tempPoint)". Because they are the same location object, Player_Location[1] is now removed as well.

To properly clean up all leaks: in the trigger, you create one point, then do an if/then/else. You do not create any new points in the then block nor in the else block. So you only need to destroy one location in the then and else blocks. So in your first example you already clean up properly in the then block, no need to add a second RemoveLocation.
However, you don't clean the location in the else block, so here you should add "call RemoveLocation(udg_tempPoint)".
Oh, interesting. so if I understand you correctly, this cleans everything up properly? Because location removal doesnt remove the "reference" to the location, but rather the location itself. So if point1 and point2 are both set to (center of (playable map area)), and i remove point1, point2 stops referring to anything aswell?
point removal3.png
 
Level 19
Joined
Jan 1, 2018
Messages
739
Oh, interesting. so if I understand you correctly, this cleans everything up properly? Because location removal doesnt remove the "reference" to the location, but rather the location itself. So if point1 and point2 are both set to (center of (playable map area)), and i remove point1, point2 stops referring to anything aswell?
Correct, but the example you gave is wrong:
When you set location variable 1 to the value of location variable 2 (in your case 'set Player_Location[1] = tempPoint'), they refer to the same location object, so you have only 1 location.
In the case of 'center of area', this is not a variable but a function. This function will create a new location every time you call it. So if you do 'set point1 = center' and 'set point2 = center', you will have 2 locations.

The trigger as it is in your image should be OK.
 
Level 3
Joined
Jun 23, 2020
Messages
14
Correct, but the example you gave is wrong:
When you set location variable 1 to the value of location variable 2 (in your case 'set Player_Location[1] = tempPoint'), they refer to the same location object, so you have only 1 location.
In the case of 'center of area', this is not a variable but a function. This function will create a new location every time you call it. So if you do 'set point1 = center' and 'set point2 = center', you will have 2 locations.


The trigger as it is in your image should be OK.
Cool, thanks!
 
Status
Not open for further replies.
Top