Alcscript/python wierd

Help bring our custom Ages to life! Share tips and tricks, as well as code samples with other developers.

Alcscript/python wierd

Postby sculp » Fri Aug 05, 2011 2:50 pm

I have made extensive referance in the last few days to this thread: http://forum.guildofwriters.org/viewtopic.php?f=9&t=4828
My mission was essentially the same as Egon's; how to make a call to a python script from within a responder defined in Alcscript. 'notifymsg' seemed to be my answer, and given Egon's eventual success I used code derived from this thread. As is always the case with the gossamer thin web that is the Alcscript/Python interface I eventually got my code to work.. .. but not without a lot of patience .. phew :shock: .
On the way though I noticed the same behavior as reported by Egon at one point in his struggle:
When I click my button in game "id" passed to "OnNotify" is "-1"


I think I've noticed this before, without understanding what the problem is. My experience, given my current setup is that this call:
Code: Select all
actTrigger = ptAttribActivator(1, 'Activator')

##############some code#########

    def OnNotify(self, state, id, events):
        if id == actTrigger.id:

will always fail because the value reported for the ptAttribActivator is always -1
This call:
Code: Select all
actTrigger = ptAttribActivator(1, 'Activator')

##############some code#########

    def OnNotify(self, state, id, events):
        if id == actTrigger.id - 2:

works :?

I went back and checked this with some scripts I wrote a couple of years ago, which I know work fine. I inserted a debug script to report the id of the activator and always the same result: the value reported for the ptAttribActivator is always -1. Bug?

my current setup is:
Blender 2.49
Python 2.6
PyPRP 1.6
Last edited by sculp on Sat Aug 06, 2011 10:08 am, edited 1 time in total.
sculp
 
Posts: 36
Joined: Fri Nov 16, 2007 9:45 pm

Re: Alcscript/python wierd

Postby tachzusamm » Sat Aug 06, 2011 7:53 am

I don't think it's a bug; more likely you may have a typo or something else wrong in the AlcScript where you pass the Activator to python.
You will usually get a -1 as the ID then, because the activator you want to pass is undefined then. For example, when you forgot to give the logicmod the same name as in the line where you reference it.
Could you post your AlcScript?

By the way, if you really want to get an action fired by a button, I would recommend to write the Python part this way:
Code: Select all
    def OnNotify(self, state, id, events):
        if state and id == actTrigger.id:

otherwise the code gets fired when the button is clicked (mouse button pressed, state=1) PLUS when you release the mouse button (state=0).
User avatar
tachzusamm
 
Posts: 575
Joined: Thu May 29, 2008 2:03 am
Location: Germany

Re: Alcscript/python wierd

Postby sculp » Sat Aug 06, 2011 10:06 am

I don't think it's a bug; more likely you may have a typo or something else wrong in the AlcScript where you pass the Activator to python.

In fact my responder works perfectly if I accept that the value passed to Python is -1.
Here's the relevant part of the Alcscript:
Code: Select all
        actions:
          - type: pythonfile
            name: powerLever
            pythonfile:
                file: powerLever
                parameters:
                  - type: activator
                    ref: logicmod:LeverPulled
                  - type: string
                    value: Lever01
                  - type: responder
                    ref: :LeverResponder
                  - type: responder
                    ref: :RumbleResponder
                  - type: responder
                    ref: :RumbleOffResponder

and my Python:
Code: Select all
    def OnNotify(self, state, id, events):
        powerState=ageSDL['PowerLeverState'][0]
        print 'OnNotify called..'
        print 'this notification has id of: %d' % id
        if id == actTrigger.id-2:
            if state:
                print 'Lever pulled.. running lever responder'
                lever.physics.suppress(true)
                leverResp.run(self.key, events=events)

Looking at the .elf report:
Code: Select all
(08/06 17:44:16) OnNotify called..
(08/06 17:44:16) this notification has id of: -1
(08/06 17:44:16) Lever pulled.. running lever responder

As I say, I have tested this elsewhere and found the same to be true in every one I investigated. I never noticed before, because I have been in the habit of referencing the trigger with it's name as passed in the ptAttribString. If I try to identify it with it's id - which should be 1 - it fails unless I get round it with the 'actTrigger.id-2' dodge :?
sculp
 
Posts: 36
Joined: Fri Nov 16, 2007 9:45 pm

Re: Alcscript/python wierd

Postby tachzusamm » Sat Aug 06, 2011 11:14 am

Yes, I understand. But getting a -1 as the id should not happen.

Let me post an example, which I tested just some minutes ago, to make it more clear what I meant.

Snipppets of the Python code:
Code: Select all
actTestButton = ptAttribActivator(1, 'Act: ClickTest')

class aniClickTest(ptModifier,):
   
    def OnNotify(self, state, id, events):
        print ('---%s:OnNotify()' % self.__class__.__name__)
        print ('   state=%d, id=%d' % (state,id))
       
        if (state == 0):
            return
       
        if (id == actTestButton.id):
            print 'aniClickTest.OnNotify: Button Clicked'



Now, this AlcScript gives me a correct ID of 1:
Code: Select all
Test_Button:
    logic:
        modifiers:
          - name: TestTheButton      # gives the logicmod a name
            cursor: poised
            flags:
              - localelement
            activators:
              - type: objectinvolume
                remote: TestBtnClickRgn
                triggers:
                  - any
            conditions:
              - type: activator
                activators:
                  - type: picking
              - type: objectinbox
                satisfied: true
            actions:
              - type: pythonfile
                ref: :AniClickTest_PFM

        actions:
          - type: pythonfile
            name: AniClickTest_PFM
            pythonfile:
                file: aniClickTest
                parameters:
                  - type: activator
                    ref: logicmod:TestTheButton   # logicmod name


Code: Select all
(08/06 19:59:07) ---aniClickTest:OnNotify()
(08/06 19:59:07)    state=1, id=1
(08/06 19:59:07) aniClickTest.OnNotify: Button Clicked
(08/06 19:59:08) ---aniClickTest:OnNotify()
(08/06 19:59:08)    state=0, id=1



while this one gives a wrong ID of -1 because the modifier name does not match:
Code: Select all
Test_Button:
    logic:
        modifiers:
          - name: Test_Button      # (name not matching the one in activator parameter)
            cursor: poised
            flags:
              - localelement
            activators:
              - type: objectinvolume
                remote: TestBtnClickRgn
                triggers:
                  - any
            conditions:
              - type: activator
                activators:
                  - type: picking
              - type: objectinbox
                satisfied: true
            actions:
              - type: pythonfile
                ref: :AniClickTest_PFM

        actions:
          - type: pythonfile
            name: AniClickTest_PFM
            pythonfile:
                file: aniClickTest
                parameters:
                  - type: activator
                    ref: logicmod:TestTheButton   # logicmod name


Code: Select all
(08/06 20:04:39) ---aniClickTest:OnNotify()
(08/06 20:04:39)    state=1, id=-1
(08/06 20:04:39) ---aniClickTest:OnNotify()
(08/06 20:04:39)    state=0, id=-1


I hope this explains it. ;)
User avatar
tachzusamm
 
Posts: 575
Joined: Thu May 29, 2008 2:03 am
Location: Germany

Re: Alcscript/python wierd

Postby sculp » Sat Aug 06, 2011 12:02 pm

Well thanks tachzusamm for the elegant explanation. I know this is how it's supposed to be, just that somehow it's not working right here. Here's the complete Alcscript:
Code: Select all
Lever01:
    animations:
      - name: LeverPullAnim
        autostart: 0
        loop: 0     
    logic:
        modifiers:
          - name: LeverPulled
          - cursor: poised
            flags:
              - localelement
            activators:
              - type: objectinvolume
                remote: RgnPowerLever
                triggers:
                  - any
            conditions:
              - type: activator
                activators:
                  - type: picking
              - type: objectinbox
                satisfied: true
            actions:
              - type: pythonfile
                ref: :powerLever
        actions:
          - type: pythonfile
            name: powerLever
            pythonfile:
                file: powerLever
                parameters:
                  - type: activator
                    ref: logicmod:LeverPulled
                  - type: string
                    value: Lever01
                  - type: responder
                    ref: :LeverResponder
                  - type: responder
                    ref: :RumbleResponder
                  - type: responder
                    ref: :RumbleOffResponder

As you can see, unless I'm missing something blinding, it looks ok
sculp
 
Posts: 36
Joined: Fri Nov 16, 2007 9:45 pm

Re: Alcscript/python wierd

Postby tachzusamm » Sat Aug 06, 2011 12:25 pm

You could try:
Code: Select all
          - name: LeverPulled
            cursor: poised



instead of:
Code: Select all
          - name: LeverPulled
          - cursor: poised
User avatar
tachzusamm
 
Posts: 575
Joined: Thu May 29, 2008 2:03 am
Location: Germany

Re: Alcscript/python wierd

Postby sculp » Sat Aug 06, 2011 1:30 pm

That did it! :o thanks t
sculp
 
Posts: 36
Joined: Fri Nov 16, 2007 9:45 pm


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests

cron