Update variables on standalone remotely

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Update variables on standalone remotely

Post by trevix » Sun Aug 08, 2021 6:51 pm

I am stuck with this:

I have two standalone in two different devices on a local network.
Standalone "A" has some globals (or global array), let's say "gSport" and "gColor."
Using socket communication, I want to be able to update, from Standalone "B", the values of those globals on Standalone "A".

I already completed the socket communication between the twos, so that's not a problem.

The problem is that I want to decide in Standalone "B" wich globals to update on Standalone "A" (not necessarily all of them).
For example, in standalone "A" I receive an array that contains my data, TheMessage["DATA"] that is:
TheMessage
DATA
gSport Tennis
In standalone "A" how do i capture the name of that global (by reference?) in a script like this, so that at the end the value of global gTennis in standalone "A" is "Tennis"?

Code: Select all

put line 4 of variableNames() into tGlobalList
   repeat for each line tKey in the keys of TheMessage["DATA"]
         if tKey is among the items of tGlobalList then
              put TheMessage["DATA"][tKey] into tVar --this doesn't work since it just update the variable tVar
         end if
   end repeat
Thanks
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Update variables on standalone remotely

Post by trevix » Sun Aug 08, 2021 7:41 pm

Or, to make it short, why this doesn't update the global gSport?

Code: Select all

global gSport

on mouseUp pButtonNumber
     local tvar
     put line 4 of variableNames() into tGlobalList
     put "Tennis" into tSport
     put tSport into TheMessage["DATA"]["gSport"]
     repeat for each line tKey in the keys of TheMessage["DATA"]
          if tKey is among the items of tGlobalList then
               put tKey into tVar
               put TheMessage["DATA"][tKey] into tVar
          end if
     end repeat
end mouseUp
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 516
Joined: Fri Feb 24, 2006 9:45 am

Re: Update variables on standalone remotely

Post by elanorb » Thu Aug 12, 2021 9:27 am

Hi Trevix

Even though tVar contains the name of a global variable LiveCode doesn't know that is what is being referred to so it will just update the contents of tVar.

You can get the result you want using a do statement e.g.

Code: Select all

do ("put" && TheMessage["DATA"][tKey] && "into" && tVar)
That should update the global gSport.

Kind regards

Elanor
Elanor Buchanan
Software Developer
LiveCode

trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Update variables on standalone remotely

Post by trevix » Thu Aug 12, 2021 9:45 am

Great. Thanks
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Update variables on standalone remotely

Post by trevix » Sun Sep 19, 2021 3:38 pm

One more thing:
What about if the content of TheMessage["DATA"][tKey] is an array and gSport is a global array?
With the do statement I got an error

Thanks
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

stam
Posts: 2634
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Update variables on standalone remotely

Post by stam » Sun Sep 19, 2021 5:59 pm

trevix wrote:
Sun Sep 19, 2021 3:38 pm
One more thing:
What about if the content of TheMessage["DATA"][tKey] is an array and gSport is a global array?
With the do statement I got an error

Thanks
Perhaps that should be a do merge() ? i.e.

Code: Select all

do merge("put" && TheMessage["DATA"][tKey] && "into" && tVar)

trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Update variables on standalone remotely

Post by trevix » Mon Sep 20, 2021 12:18 pm

No cigars. Throw an error.
Any other idea?
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

trevix
Posts: 958
Joined: Sat Feb 24, 2007 11:25 pm
Location: Italy
Contact:

Re: Update variables on standalone remotely

Post by trevix » Thu Sep 23, 2021 11:09 am

I found the solution. Here is a different example:

Code: Select all

global gSport,gPref,gPlayers

on mouseUp pMouseButton
     --save global value, single values and array
     --example data
     put 3 into tArray["DATA"]["gPref"]["sport"]["SetNum"]
     put "tennis" into tArray["DATA"]["gSport"]
     put "Bob" into  tArray["DATA"]["gPlayers"]["player1"]
     put "Simon" into  tArray["DATA"]["gPlayers"]["player2"]
     --save these globals on the stack script
     SetGlobalArray tArray --on stack sxript
end mouseUp
On the stack script the globals get updated

Code: Select all

global gSport,gPref, gPlayers

on SetGlobalArray pValue
     delete global gSport
     delete global gPref
     delete global gPlayers
     repeat for each line tKey in the keys of pValue["DATA"]
          if pValue["DATA"][tKey] is an array then
               --put tKey into pName
               do ("put" && 1 && "into" && tKey & "[" & quote & "fake" & quote & "]") --transfor pname in array
               put  "pValue[" & quote & "DATA" & quote & "]" & "[" & quote & tKey & quote & "]" into temp
               do ("put" && temp && "into" && tKey)
          else --single value global
               do ("put" && pValue["DATA"][tKey] && "into" && tKey)
          end if
     end repeat
     breakpoint
     beep--see variables, the global value has been updated, without this command knowing the name of the global
end SetGlobalArray
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”