Issue with deleting & re-using a point.

SupremeRuler

Member
Reaction score
1
For some reason, it will only create 2 footmen if I delete Point2 then reset it.
Also, I think deleting & resetting Point is creating some slight issues but not 100% sure of this.

Code:
Setting Terrain
    Events
        Time - Elapsed game time is 1.00 seconds
    Conditions
    Actions
        Set Point = (Random point in Border Left <gen>)
        Unit - Create 1 Footman for Player 1 (Red) at Point facing Default building facing degrees
        For each (Integer A) from 1 to 15, do (Actions)
            Loop - Actions
                Set Real[0] = 0.00
                Set Real[1] = 45.00
                Set Real[2] = 90.00
                Set Real[3] = 270.00
                Set Real[4] = 315.00
                Set Point2 = (Point offset by 300.00 towards Real[(Random integer number between 0 and 4)] degrees)
                Custom script:  call RemoveLocation(udg_Point)
                Set Point = Point2
                Game - Display to (All players) the text: (String(Real[(Random integer number between 0 and 4)]))
                Unit - Create 1 Footman for Player 1 (Red) at Point2 facing Default building facing degrees
                Custom script:  call RemoveLocation(udg_Point2)
 

Juggernaut

I don't know what to change it to
Reaction score
33
The problem is because you remove the variable Point inside the loop. And you also remove Point2 inside the loop. But since point2=point
and point doesn't get reset the variable gets lost.

Trigger:
  • Untitled Trigger 001
    • Events
      • Time - Elapsed game time is 2.00 seconds
    • Conditions
    • Actions
      • Set Temp_Point = (Random point in Region 000 &lt;gen&gt;)
      • Unit - Create 1 Footman for Player 1 (Red) at Temp_Point facing 0.00 degrees
      • For each (Integer A) from 1 to 10, do (Actions)
        • Loop - Actions
          • Set Point2 = (Temp_Point offset by 500.00 towards 0.00 degrees)
          • Set Temp_Point = Point2
          • Game - Display to (All players) the text: &lt;Text&gt;
          • Unit - Create 1 Footman for Player 1 (Red) at Point2 facing 0.00 degrees
      • Custom script: call RemoveLocation(udg_Temp_Point)
      • Custom script: call RemoveLocation(udg_Point2)


EDIT: Now it works correctly. Wasn't copying correctly lol
 

SupremeRuler

Member
Reaction score
1
@trena99, the problem now is that you're creating point leaks by assigning a new value to your point variables before destroying them.
 

KaerfNomekop

Swim, fishies. Swim through the veil of steel.
Reaction score
613
Trigger:
  • Untitled Trigger 001
    • Events
      • Time - Elapsed game time is 1.00 seconds
    • Conditions
    • Actions
      • Set Real[1] = 45.00
      • Set Real [2] = 90.00
      • Set Real [3] = 270.00
      • Set Real [4] = 315.00
      • Set Point = (Random point in Region 000 &lt;gen&gt;)
      • Unit - Create 1 Footman for Player 1 (Red) at Point facing Default building facing degrees
      • For each (Integer A) from 1 to 15, do (Actions)
        • Loop - Actions
          • Wait 1.00 seconds
          • Set Angle = Real[(Random integer number between 1 and 4)]
          • Set Point2 = (Point offset by 300.00 towards Angle degrees)
          • Custom script: call RemoveLocation (udg_Point)
          • Set Point = Point2
          • Unit - Create 1 Footman for Player 1 (Red) at Point facing Default building facing degrees
      • Custom script: call RemoveLocation (udg_Point)
      • Custom script: call RemoveLocation (udg_Point2)

This should be enough.

A leak only happens when you have a point which no variables refer to. Since Point and Point2 refer to the same location, clearing Point2 will also clear Point, and the location no longer exists. When you set Point2 to something else in the next run of the loop, Point still refers to the old location, and you've cleared that, so no leaks happen.
 

SupremeRuler

Member
Reaction score
1
Thanks Kaerf, it works! But I don't understand why removing the variable Point would also remove Point2 because even though they may be the same location, they are still 2 different variables and I am removing the variable. Also, what's with the wait?
(Also, did they remove reps from this forum? I can't seem to be able to rep you.)
 

Imp Midna

Active Member
Reaction score
52
they are still 2 different variables and I am removing the variable.

Exactly that is the Point: There are NOT 2 different variables. Thats the Prolem about high-level languages such as Java (or this high-high-level scripting syntax). Anyways, im gonna explain 2 you, WHY they are the same variable.

The thing is that most languages only know call-by-value functions. Usually, thats not that supid. Just imagine this function, which takes an integer variable as a parameter and returns (yourVar + 5).

JASS:
function with the name &quot;integerPlus5&quot; having the parameter &quot;integer i&quot; {
 
set i = i + 5
 
return i;
 
}

Imagine you would have a variable, lets call it "Counter", currently holding a "1". Now you want to know what 1+5 is (for some weird reason). So, what you would do is call integerPlus5 using your variable Counter, and it would return a 6.

Trigger:
  • Set someVar = integerPlus5(Counter)


Now, the thing is, what happens to your variable "Counter"? There are two possible things that could happen to it:

If you call the function, your variable "Counter" holding a 1 is duplicated, and the clone of it is passed to the function. The parameter "i" you use in the function and the variable "Counter" are now two different variables, and thus your original Variable "Counter" is not affected by the call. After the execution of the function, "Counter" would still hold a "1". This method of handling parameters is called "call by value".

The other method would be, that the parameter "i" is the exact same variable as "Counter" and thus, every change you make with "i" would also affect "Counter". This would mean, that the code-line "set i = i + 5" would also affect your Counter. Your Variable Counter would then, in the end, hold the value "6", since it was set to Counter(1) + 5 in the function. This is called "call by reference".

Summed up:
Call by reference - a function can directly modify your variables.
Call by value - a function gets a copy of your variables instead.

Now, its pretty uncommon to get your parameters screwed up whenever you call a function. Imagine you call the warcraft function Display to (All players) the text: myVar_string, and your variable myVar_string would be dead after calling it. It would be so annoying. To get around that, the Display to (All players) the text-function would have to create a copy of the string you gave it, but hey, thats exactly what call-by-value does.

Now, always sending a COPY of a parameter can become extremly unhandy sometimes. Imagine you have a variable holding your map. This Variable would contain every possible information about your map, such as title, every trigger, every single unit placed on the map etc etc. Maybe you have some custom modells and the map is 1MB big. Now making a copy of that variable for every function call is extremly... stupid.

To get around this, so called "Pointers" where created. A Pointer is like a number that tells you "the variable you are searching for is there *points at the variable". (Year, the "point" is meant literallyly if that is a word, but note the conherense to the name "Pointer"). In Warcraft, the Pointers are called "Handles" if you ever heard of them. (They renamed it, because a "Handle" is not directly pointing at the variable, but still identifis it. Its like "Pull me through that pit and the amount of mud im my mouth will tell you where the variable is. I know my metaphors are awsome.)

Back to the example with your map-variable: you would then create a Pointer pointing at it and give the pointer to the function. Thus, only your small Pointer would be copied and not the whole map. The Problem (Or the good thing, depending on what you want to have) is, of corse, that no copy of the map was created, and the function now accesses your map variable directly, just as it would have done using call-by-reference. Like, you made the Pointer pointing to your map variable, and a exact copy of that Pointer will still point at your map variable. Thus, the function is now capable of modifying your map variable.

Now here comes the point: as i allready told, everything in warcraft which is labeled as a "handle" is sutch a pointer (more or less). Units, Players, Special Effects, Sounds, Doodads, Ubersplats and also your Locations are all in fact Pointer and not the actual location.
In general, its better this way. Imagine everytime you call anything using a unit variable there is suddenly a second unit poping up right inside the one you used as a parameter. You dont want that unit to be there.

Unfortunately, some smart people though the concept of Pointers to be way too complicated. Thus, they simply hid the Pointers from you. The Problem that you have right now is then of corse that you dont even know what a Pointer is (well, now you do) and thus cant possibly know what i just wrote. The same thing also cloaks the creation of such variables and the fact that you have to destroy them again, which is why so many wc3-mapmakers have problems to understand, seek and destroy the causes of memory leaks.

Summing up that means, everytime you write
Trigger:
  • Set Point = Location at x with the offset y

the "Location at x with the offset y" function creates a location and gives you a Pointer to it. This Pointer is written into your Variable Point. And if you then write
Trigger:
  • Set Point2 = Point

You have a second Pointer called "Point2" which now points to the same Location that "Point" points at. (Wow, all those Points and Pointers are so confunsing xD)
 

KaerfNomekop

Swim, fishies. Swim through the veil of steel.
Reaction score
613
Removing Point removes Point2 as well, because they both refer to the same position on the map. When you clear Point, that position is wiped from memory, so Point2 no longer refers to anything. That's why I removed the second clearing action from the loop, and added two more at the end.

The wait was just to see if each one was working.

Reps now come in the form of "Like"s at the bottom right of each post. It's much faster this way.

EDIT: Imp Mdina's awesomely long ninja. I feel dumb now.
 

Juggernaut

I don't know what to change it to
Reaction score
33
So lets say. you get a variable point. And then in a loop you set the variable to different values,
such in the example offset by xx.

You don't have to clear the variables inside the loop do you?
You can just delete it in the end?
e.g
"set x = position of y
-for a 1 to 10 do
-set x = x offset by 100
-<Triggers>
call removelocation x"
{the bolded part is outside of the loop}
or you have to remove the variable inside the loop?!
Does this leak?
 

KaerfNomekop

Swim, fishies. Swim through the veil of steel.
Reaction score
613
@trena99: Yes, it does leak. Because you're changing the value of your variable, there are no more references to the previous "x", so a leak happens. The location that "x" pointed to still exists in memory, but you can't reach it with a variable, so you can't use it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top