Jump to content
Developer Wiki and Function Reference Links ×

Reusing Handles?


Recommended Posts

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!

Link to comment
Guest Lyndsey

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.

Link to comment

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

Link to comment

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);

Link to comment

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...