Weird issue making use of Behaviour Scripts

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: Klaus, FourthWorld, heatherlaine, robinmiller, kevinmiller

Post Reply
whitebrick
Posts: 18
Joined: Wed Jul 02, 2014 12:36 pm

Weird issue making use of Behaviour Scripts

Post by whitebrick » Sat Feb 08, 2020 9:02 am

I really like the concept of Behaviour scripts - being able to have a bunch of routines shared that other UI elements can make use of. For the most part it's working a treat.

I'm getting weird results with script locals though and was wondering if anyone had come across the same issue?

I thought I might try a work around and bake the variable I want into a property on the UI button

Hence I placed this code into the behaviour script :

Code: Select all

put the long name of me into button_name
set the registered_linenumber of button button_name to linenumber
It correctly bakes the child UI button long name into button_name but setting the button property registered_linenumber on the child doesn't work! For some reason it writes linenumber to the key, which means I can't set the property later.

It'd be better to have this data stored in a script local but the behaviour seems to forget the script local and I can't work out why. I could just give up and use some globals I guess. But anyone got some tips or can point me in the write direction for learning more how script locals function in behaviour scripts?

cheers

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7307
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Weird issue making use of Behaviour Scripts

Post by FourthWorld » Sat Feb 08, 2020 10:49 am

"the long name" includes the object type, and the script using it also includes the object type, giving you "...button button "ObjName" of...".

Just delete the object type specifier in the line that uses the long name and you should be fine.

Tip: using the long ID for absolute references will at some point prove a worthwhile habit.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

SparkOut
Posts: 2067
Joined: Sun Sep 23, 2007 4:58 pm

Re: Weird issue making use of Behaviour Scripts

Post by SparkOut » Sat Feb 08, 2020 11:04 am

What Richard said. But also, where is the value being assigned to the variable linenumber?
In a behavior script all variables are instanced, as are references to the object hierarchy - so "of me" will resolve to the individual object, for example, as if the script had been written for that object individually. It looks like linenumber has had no assignment or declaration within the behavior script so the engine is trying to interpret linenumber as a literal that has sloppily been left unquoted. Are you trying to share the linenumber variable among behaviors? If so, you will need to ensure you reference the correct scope - eg a global.

whitebrick
Posts: 18
Joined: Wed Jul 02, 2014 12:36 pm

Re: Weird issue making use of Behaviour Scripts

Post by whitebrick » Sat Feb 08, 2020 11:28 am

Ahhhh ok - that's good to know re: the long name and button button. Ok thanks I'll try that. And I'll look into long ID, which I was unaware of as an option.

RE: linenumber - it is definitely assigned as a script local at the top of the behaviour script and I was counting on "of me" resolving to the individual object.

The flow of the script calls routines from the main card and then returns to the object would that be an example of what you're suggesting?

Thanks for taking the time to comment here.

SparkOut
Posts: 2067
Joined: Sun Sep 23, 2007 4:58 pm

Re: Weird issue making use of Behaviour Scripts

Post by SparkOut » Sat Feb 08, 2020 12:29 pm

What is the specific statement in the behavior script that assigns the value to linenumber? Something like

Code: Select all

put mainscriptFunction(tVar) into linenumber
or...?

whitebrick
Posts: 18
Joined: Wed Jul 02, 2014 12:36 pm

Re: Weird issue making use of Behaviour Scripts

Post by whitebrick » Sat Feb 08, 2020 1:11 pm

Linenumber is sent from the card script. The UI element button (using the behaviour script) is supposed to call the main card script to register itself on button_playlist and then report and store what line in the playlist the button is registered on.

Code: Select all

--- on the card script 
on register_button button_name, register_sprite, register_tween
   
   -- add button_name to the playback list & tween/sprite flags
   if button_playlist is empty then 
      put 1 into linecount
   else
      get the number of lines in button_playlist
      put it into linecount
      add 1 to linecount
   end if
   put button_name into item 1 of line linecount of button_playlist
   put register_sprite into item 2 of line linecount of button_playlist
   put register_tween into item 3 of line linecount of button_playlist
   put "button_registered" && linecount into sendmessage
   send sendmessage to button button_name
end register_button

-- in the behaviour script
on button_registered linenumber
   get the long name of me 
   put it into button_name
   set the reg_id of button_name to linenumber 
   put linenumber into registered_linenumber -- keeps record of where the button is on the animation list
end button_registered
I added that set the reg_id bit after chatting with you guys. Interestingly that assigns the reg_id to the UI buttons on my first grouped panel but doesn't do anything to the buttons on the second grouped when I call the same init routine from the first group.

registered_linenumber is set up as a script local in the behaviour script.

SparkOut
Posts: 2067
Joined: Sun Sep 23, 2007 4:58 pm

Re: Weird issue making use of Behaviour Scripts

Post by SparkOut » Sat Feb 08, 2020 4:10 pm

For testing, try

Code: Select all

answer linecount
at the top of the button_registered handler of the behavior script and see if the parameter is being sent along with the message.

In the register_button handler of the card script, also try enclosing the variable names in parentheses to ensure the engine correctly resolves them before sending. Also answer the message variable to check beforehand that it looks correct

Code: Select all

answer sendmessage
--check the message being sent genuinely looks like 'button_registered 1' etc
send (sendmessage) to button (button_name)

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2234
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Weird issue making use of Behaviour Scripts

Post by [-hh] » Sat Feb 08, 2020 6:51 pm

Moreover you could try to use "call" instead of "send" (what changes the context).
shiftLock happens

whitebrick
Posts: 18
Joined: Wed Jul 02, 2014 12:36 pm

Re: Weird issue making use of Behaviour Scripts

Post by whitebrick » Sun Feb 09, 2020 9:01 am

Yeah I like to use Call too but in this instance, I needed the context to go back to the object that the handler was found on.

It seems like adding the parentheses to the send command is getting me a result! Or at least, further than I was getting. Nice one!!!

Thanks all.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7307
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Weird issue making use of Behaviour Scripts

Post by FourthWorld » Sun Feb 09, 2020 10:54 pm

whitebrick wrote:
Sat Feb 08, 2020 9:02 am
It'd be better to have this data stored in a script local but the behaviour seems to forget the script local and I can't work out why. I could just give up and use some globals I guess. But anyone got some tips or can point me in the write direction for learning more how script locals function in behaviour scripts?
Do you notice the script-local values being clear when editing the script?

If so, in Preferences see Script Editor -> Variable Preservation
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Post Reply

Return to “Talking LiveCode”