MaxStudio Posted July 9, 2009 Share Posted July 9, 2009 I want to know, is it safe to reuse handles within a procedure? My scripts usually generate a lot of objects. Once I am done creating and defining the object attributes can I reassign the handle to a new object? The really want to limit the number of handles I define at the beginning of my script. good idea/bad idea? thanks! Quote Link to comment
MullinRJ Posted July 9, 2009 Share Posted July 9, 2009 Yes, when you are done with an object, the handle (variable) can be reused. Many of my scripts have only one handle variable and I access every object on a layer with it, using FOR or WHILE loops. Raymond Quote Link to comment
Miguel Barrera Posted July 9, 2009 Share Posted July 9, 2009 The only problem with reusing the same variables for different data is that you may loose track of their meaning. I encapsulate variables for temporary use in procedures and functions to limit memory usage and use global variables only for data that will be needed throughout the program. Quote Link to comment
Guest Lyndsey Posted July 9, 2009 Share Posted July 9, 2009 Yes, it is far easier to maintain a script if the variable name for a given handle is associated only with one object. If your function is getting too large, you may want to consider refactoring (breaking up in an intelligent way) the large function to call smaller functions. Quote Link to comment
MaxStudio Posted July 14, 2009 Author Share Posted July 14, 2009 Hey thanks everyone. Do global variables take up more memory? If so why? Quote Link to comment
JBenghiat Posted July 14, 2009 Share Posted July 14, 2009 Explicitly, no. I think the caution is declaring more variables then you need. For example, say you have a script that draws a triangle with three lines. After drawing each line, you want to set it to a color, so you need the handle to the line. You could declare a variable for each side: side1 :HANDLE; side2 :HANDLE; side3 :HANDLE; If you need to come back to side 1 after dealing with side 2 then this is the way to go. You could also declare a single variable: lastSide: HANDLE; If you draw the side, set the color, and never need to do anything else with it, just re-assign lastSide to the last drawn line, and you use 1/3 the memory. To be the most efficient, use the handle in a sub-procedure: PROCEDURE ColorThisSide(theSide:HANDLE); Then within the main script, call ColorThisSide(LastNewObj); Here the handle is created only why the side is being assigned its color, then released at the end of the procedure. What you do depends on when and how often the script needs to access the same object. Extrapolate this example to all selected objects, and the affect on memory becomes significant. -Josh Quote Link to comment
Miguel Barrera Posted July 14, 2009 Share Posted July 14, 2009 Global variables do not take more memory than local variables but because they exist until the program ends, the number of global variables may affect the amount of memory available. For example, if you are looking for an object with a defined attribute and you are going to modify it later in the program, it is a good candidate for a global handle to the object. All the other variables needed to search for the object can then be defined in a function or procedure and they will only exist while the function executes. PROCEDURE Example; VAR {these global variables exist until the program ends} objHandle: HANDLE; objAttr: INTEGER; FUNCTION SearchForObj(objAttr: INTEGER): HANDLE; VAR {these variables only exist while SearchForObj executes} curAttr: INTEGER; searching: BOOLEAN; curObj: HANDLE; BEGIN curObj:= FInLayer; searching:= TRUE; WHILE (curObj <> NIL) & searching DO BEGIN curAttr:= GetLS(curObj); IF curAttr = objAttr THEN BEGIN searching:= FALSE; SearchForObj:= curObj; END ELSE curObj:= NextObj(curObj); END; END; BEGIN ..... objAttr:= 2; objHandle:= SearchForObj(objAttr); ..... END; Run(Example); Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.