quaquel / pyNetLogo

BSD 3-Clause "New" or "Revised" License
81 stars 22 forks source link

Use netlogo.report to return value from reporter Distance in netlogo. #75

Closed PK4teens closed 10 months ago

PK4teens commented 1 year ago

Hi, I'm using pynetlogo to load the Airplane landing 3D model. In this process, I want to use netlogo.report to call the netlogo reporter 'distance', which could reports the distance from this agent to the given turtle or patch. I manage to use this reporter to return the distance between self airplane and opponent airplane. Code as follows:

    netlogo.command('ask an-airplane 1 [set roll {}]'.format(roll1))
    dist0=netlogo.report('distance an-airplane 1')
    dist1=netlogo.report('distance an-airplane 0')
    print(dist0,dist1)

It reports error:

Traceback (most recent call last): File "E:\pythonProject1\pythonProject2\lib\site-packages\pynetlogo\core.py", line 300, in report result = self.link.report(netlogo_reporter) org.nlogo.core.CompilerException: You can't use DISTANCE in an observer context, because DISTANCE is turtle/patch-only. at position 49 in

It seems that distance is an turtle/patch-only reporter. When I type it in netlogo command center, the object should be turtle-set but not the observer. So i manage to use netlogo.command to achieve it. Code as below:

``dist0=netlogo.command('ask an-airplane 0 [show distance an-airplane 1]')
dist1=netlogo.command('ask an-airplane 1 [show distance an-airplane 0]')
print(dist0,dist1)``

It really works, but the distance data is typed in netlogo's command center, not python's results bar.In python, it types none in the bar: None None None None None None None None None None So i would like to know how to load netlogo.report in turtle-only state by pynetlogo ,or to convey the data by pynetlogo.command to python? Thanks you. Cache_5da5f30adcd111f4

quaquel commented 1 year ago

commands don't return anything in the java API for netlogo. So that is why you see the None. There is a pacth_report method in pynetlogo that might be used for what you try to do. But I am not sure.

PK4teens commented 1 year ago

Really thanks for your reply. I have solved the issue. I assigned a reporter in netlogo named dist to report the distance between two agents in observer mode. Then use netlogo.report to pass the distance to python.