Get agent at the head of conveyor - correct use of ConveyorPath.getAgent()?

Viewed 63

In my model, there is a palletizing area where a palletizer agent moves to a location adjacent to an accumulating conveyor, picks up the last box on that conveyor, and transfers it to a pallet.

I am trying to find the best way to refer to that last box on the conveyor and store it in a variable.

I am aware of ConveyorPath.getAgent(), documented here but it does not seem to function properly, throwing the following error:

Code : v_box = v_conveyorPath.getAgent(0);    
Error : Description: Type mismatch: cannot convert from Agent to Box.

I have gone into my conveyor markups and changed the "Material item type" fields to "Box", to no avail.

What am I doing wrong here? How can I get the ConveyorPath to return a Box type instead of an Agent type?

Thank you.

EDIT: After running tests, I can confirm that the agents present on my conveyor are of Class Box.

traceln(conveyor.getAgent(0).getClass());
Output : class modelname.Box

And yet I still cannot store a reference to that Box inside of a variable.

1 Answers

You can simply cast the agent you are getting as a box

Box myBox = (Box)conveyor.getAgent(0)

Bare in mind that this type of casting will cause an error if you have anything that is not a box on the conveyor

Read more here https://www.w3schools.com/java/java_type_casting.asp

but if you specify in your conveyor object that the objects you have are boxes, or in this example below Cartons,

enter image description here

Then the get function will return an agent of that type specified, thus no casting neccesary.

enter image description here

The latter option is the safest and possibly more correct way of doing it in most models

Related