Shared Functions()

Discussions and file drops for Auggie
Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

GlennD wrote: P.S. Did look up the Game Monkey and it does mention that no switch is available so blocks of if statements it is.  :D
I found this example and it worked for me.

Code: Select all

name = "pear";
switch (name)
{
  case "apple": { print("hurrah "); print("for apples"); } // braces are required
  case "banana":                                       // Fall through multiple cases
  case "pear":
  case "cherry": { print("fell to fruit", name); } 
  case favoriteFruit: { print("my favorite fruit"); }     // allow variable cases!
  default: { print("no fruit for you"); }                 // Default not required
}

 fell to fruit pear  
DanL
Old Timer
Posts: 362
Joined: Wed Sep 10, 2014 1:35 pm

Re: Shared Functions()

Post by DanL »

Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

How about a simple sorting routine?

Code: Select all

global  BubbleSort = function(iarray,num)  
{	
d={""};
   print("Unsorted Data:");
&nbsp;  for (k = 0; k < num; k=k+1) 
&nbsp;  {
&nbsp; &nbsp; &nbsp; print(iarray[k]);
&nbsp;  }
&nbsp;  for (i = 1; i < num; i=i+1) 
&nbsp;  {
&nbsp; &nbsp; &nbsp; for (j = 0; j < num - 1; j=j+1) 
&nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp;  if (iarray[j] > iarray[j + 1]) 
&nbsp; &nbsp; &nbsp; &nbsp;  {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; temp = iarray[j];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iarray[j] = iarray[j + 1];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iarray[j + 1] = temp;
&nbsp; &nbsp; &nbsp; &nbsp;  }
&nbsp; &nbsp; &nbsp; }
&nbsp;  }
&nbsp;  print("Sorted & After pass: " + k);
&nbsp;  for (k = 0; k < num; k=k+1) 
&nbsp;  {
&nbsp; &nbsp;  print(iarray[k]);
&nbsp; &nbsp;  d[k] = iarray[k]; 
&nbsp;  }
return;
};
a={33,6,11,3,"k","e","y"}; // the data array
b=tableCount(a); // number of items in the array
BubbleSort(a,b);


Unsorted Data:&nbsp; 
 33&nbsp; 
 6&nbsp; 
 11&nbsp; 
 3&nbsp; 
 k&nbsp; 
 e&nbsp; 
 y&nbsp; 
 Sorted & After pass: 7&nbsp; 
 3&nbsp; 
 6&nbsp; 
 11&nbsp; 
 33&nbsp; 
 e&nbsp; 
 k&nbsp; 
 y&nbsp; 


Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

Converting a Hex to a number
HexString is a Hex number represented as a string
Hex letters can be Upper or Lower case

Code: Select all

global&nbsp; HexStringToNum = function(HexString) 
{ 
&nbsp; &nbsp; HexString = ToString(HexString);
&nbsp; &nbsp; local char = "";
&nbsp; &nbsp; local rHexString = "";
&nbsp; &nbsp; local Val = 0;
&nbsp; &nbsp; local indx = 0;
&nbsp; &nbsp; local StringAsNum = 0;
&nbsp; &nbsp; local ZeroForNilChar = 0;
&nbsp; &nbsp; local StringLength = HexString.Length(HexString);&nbsp; &nbsp; 
&nbsp; &nbsp; rHexString = HexString.Reverse(HexString);&nbsp; &nbsp; 
//debug();
&nbsp; for (x = 0; x < StringLength; x=x+1)
&nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; indx = x + 1;&nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; char = rHexString.Mid(indx-1,1);
&nbsp; &nbsp; &nbsp; &nbsp; if (char == null)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; char = "0"; Val = 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }
&nbsp; &nbsp; &nbsp; &nbsp; if (char == "A" or char == "a")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Val = (10 * math.power(16, indx-1));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else if (char == "B" or char == "b")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Val = (11 * math.power(16, indx-1));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else if (char == "C" or char == "c" )
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Val = (12 * math.power(16, indx-1));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else if (char == "D" or char == "d")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Val = (13 * math.power(16, indx-1));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else if (char == "E" or char == "e")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Val = (14 * math.power(16, indx-1));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else if (char == "F" or char == "f")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Val = (15 * math.power(16, indx-1));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; else //&nbsp; if (char.String != "string")
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Valt = ToInt(char);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Valt == null) 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ZeroForNilChar = 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Val = 0;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Vals = ToInt(char);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Val = (Vals * math.power(16, indx-1));
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  }&nbsp; &nbsp; &nbsp; &nbsp;  
&nbsp; &nbsp; &nbsp; &nbsp; StringAsNum = StringAsNum + Val; 
&nbsp; &nbsp; &nbsp; &nbsp; local dummy = 0;
&nbsp; &nbsp; &nbsp; &nbsp; } 
&nbsp; &nbsp; local dummyvar = StringAsNum;
&nbsp; &nbsp; return StringAsNum;
};
stringasnum = HexStringToNum("22AF2DE");
print(stringasnum);

36369118

Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

temperature conversions

Code: Select all

global&nbsp; Temp_F_to_C = function(F) 
{
&nbsp; F=ToFloat(F);
&nbsp; celsius=(5.0/9.0)*(F -32.0);
&nbsp; return celsius;
};
global&nbsp; Temp_C_to_F = function(C) 
{
&nbsp; C=ToFloat(C);
&nbsp; Fehr=((9.0/5.0)*C) +32.0;
&nbsp; return Fehr;
};
User avatar
Mooselake
Old Timer
Posts: 522
Joined: Sun Dec 26, 2010 12:21 pm
Location: Mooselake Manor

Re: Shared Functions()

Post by Mooselake »

The linked GM (have to wonder if GameMonkey was an obvious choice for Gearotic Motion :) ) script reference only mentions switch() in an example, although they say it uses flex as a parser. &nbsp;Too many family things going on today to dig deeper today (like how much of flex gets carried along, does based on mean incorporating?). &nbsp;The Lua reference doesn't mention switch either. &nbsp;Something strange is going on here... &nbsp;I'll mention I'm not very enthused about the "read the source code and figure it out" style of documentation, but maybe that's necessary to see just what's included in the language.

Wonder if you could use array constructors to do pre-sorted tables? &nbsp;From the quick doc scan that's kinda ambiguous, too.

Kirk


User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4592
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Shared Functions()

Post by ArtF »

Kirk:

&nbsp; Its a fantastic scripter, but there are tons of ambiguities.. :)

Art
User avatar
Mooselake
Old Timer
Posts: 522
Joined: Sun Dec 26, 2010 12:21 pm
Location: Mooselake Manor

Re: Shared Functions()

Post by Mooselake »

ArtF wrote:Its a fantastic scripter, but there are tons of ambiguities.. :)
That's what makes it so much fun :)

I found a lengthy discussion of Tempest on the Mach3 forum that's taking a big chunk of that elusive spare time, plus the head scratching is risking the few remaining follicles.&nbsp; I didn't realize that you'd discovered 3rd order S curve motion long before the tinyg guys, but I'm not surprised.

Kirk
Last edited by Mooselake on Fri Dec 25, 2015 9:53 am, edited 1 time in total.
GlennD
Old Timer
Posts: 80
Joined: Tue Oct 18, 2011 4:19 pm

Re: Shared Functions()

Post by GlennD »

Group, sorry about the switch thing, I thought I had tried the braces in the case portion guess not.
I had tried several variants before I went looking at the Game Monkey stuff.

Hope everyone is having a wonderful Christmas.

Glenn

Edit:
Even funnier is when you look further down on the page as linked above and where I found that it said didn't use switch. you see switch used in the Constructor with Parameters example. &nbsp;The example isn't using the braces.

Attachments
GM Switch.PNG
Last edited by Anonymous on Fri Dec 25, 2015 3:30 pm, edited 1 time in total.
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4592
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Shared Functions()

Post by ArtF »

Glenn:

:), yes, your seeing my only issue with Game Monkey script, its a bit hard to figure the rules, and Ive modified it
quite a bit as well, the scripts are preemptive now in some cases for example, necessary for Auggie because someone,
somewhere at some time will do a

while(1) { };

and lock up a system with a thread that never ends. The system tries to detect that and will put to sleep
a thread that takes more than 50ms or so. If, ( and it can happen) this fails to happen and a system appears locked
from a thread you wrote, Auggie has a "Three-Finger Salute" that kills all running threads. Ctrl-Shift-Alt. This kills
any threads running.

&nbsp; Monkey Script is a very easy and fun language, unlike VB in Mach3, where I dreaded even thinking of writing a
routine, Im finding I actually like scripting in Monkey for the most part. Ive modified it here and there to suit
Auggies needs, the Docs will need work as I release data for you guys to see how things interact, but as a
script engine decision, Im pretty happy with GM script.

(It does sound as if it were made for Gearotic Motion doesnt it?)

&nbsp;Im glad youve enjoyed playing in it, between you and YNN its firming up how I will go forward
with the next scripting system dedicated to the GCode callable scripts. Its a challenge to make that easy,
not too complex and powerfull enough to make a differerence.
&nbsp; &nbsp; &nbsp;I've decided I'll be doing it to a similar fashion as World of Warcraft Plugins actually, so the
worlds gaming systems are definitely having a major impact on Auggies development, and before
anyone thinks thats a disparaging thing to say, consider for a moment that just Game Monkey Script alone probably
has more development investment in it than Mach3 did after 4 years of development.
World of Warcraft has had hundreds of millions of dollars spent in their development over 10
years, and I follow such things as proof of direction for evolving software when I write things.

&nbsp; Now Im not as good as they are, so you guys need to keep commenting on what you
find easy, and what you find nasty, where I can adapt I will. &nbsp;

Thx
Happy New Year
Art


Last edited by ArtF on Sat Dec 26, 2015 3:10 am, edited 1 time in total.
GlennD
Old Timer
Posts: 80
Joined: Tue Oct 18, 2011 4:19 pm

Re: Shared Functions()

Post by GlennD »

Art
Thank you for the explanation.
A quick question on the SetText and GetText if you have time.

I have a button I want to change its text based on the state.
I started my button id as just 0 or 1 initially then thought I would add a 700 in front maybe to differentiate them.

Here is the different examples to see if I could get the text from a button.
Figured if I went that route I could figure out how to set it from there.
Since I tried a bunch of different iterations last night and today on the SetText with no luck.

Code: Select all

mTxt = Button(Main_BUT_7002).GetText;
mTxt1 = Button("Main_BUT_7002").GetText;
mTxt2 = Button(Main_BUT_7002).Text;
mTxt3 = Button(Main_BUT_7002).text;
mTxt4 = Button("Main_BUT_7002").Text;
mTxt5 = Button("Main_BUT_7002").text;

but= Button("Main_BUT_7002");
 mTxt6 = but.GetText;
"but" does show the hex val of the Main_BUT_7002

As you can see the .GetText gives a function:_native
The .text and .Text stay as null.

I even tried getting the text of a label same results.
A perfectly good answer is it is not hooked up yet and I can wait.
Or numb skull do it this way or that ain't happening.&nbsp; :)

Thanks
Glenn
Attachments
Panel_TabBox.PNG
ButtonChange.PNG
User avatar
ArtF
Global Moderator
Global Moderator
Posts: 4592
Joined: Sun Sep 05, 2010 6:14 am
Contact:

Re: Shared Functions()

Post by ArtF »

Hi Glen:

&nbsp; There are a couple ways to do that really. The way I recommend is to use

mybut = Button("Main_BUT_88");
mybut.SetText("New");

&nbsp; to get the text you can do a

text = mybut.GetText();

&nbsp; FYI:
&nbsp; &nbsp; When your typing these things, type the words&nbsp; "mybut = Button"", then left shift and click the button,
the rest will fill in for you.. If a button text doesnt set.. check its ID is unique..

Art



GlennD
Old Timer
Posts: 80
Joined: Tue Oct 18, 2011 4:19 pm

Re: Shared Functions()

Post by GlennD »

Thanks
You forgot the you numbskull part.
I tried everything except the parentheses.&nbsp; :D
Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

You might like Dialog popup boxes too, in this example called with a screen button named "Main_BUT_45"

Code: Select all

global &nbsp;TestDlgs = function(current) 
{
 &nbsp;print(current);
 &nbsp;dlg=Button("Main_BUT_45");
 &nbsp;if(current == 1)
 &nbsp; &nbsp;{
 &nbsp; &nbsp; &nbsp;a = Dlgs();
 &nbsp; &nbsp; &nbsp;b = a.GetCoord3("Enter 3 coords:", "x", "y", "z", Vec3(4,5,6));
 &nbsp; &nbsp; &nbsp;val0=(b[0]);
 &nbsp; &nbsp; &nbsp;val1=(b[1]);
 &nbsp; &nbsp; &nbsp;val2=(b[2]);
 &nbsp; &nbsp; &nbsp;print(b);
 &nbsp; &nbsp; &nbsp;print(val0);
 &nbsp; &nbsp; &nbsp;print(val1);
 &nbsp; &nbsp; &nbsp;print(val2);
 &nbsp; &nbsp;}
return;
};

Attachments
popup.JPG
popup2.JPG
Ya-Nvr-No
Old Timer
Posts: 188
Joined: Wed Sep 08, 2010 11:15 am

Re: Shared Functions()

Post by Ya-Nvr-No »

Here is an example CheckHomed(); of how to change the axis DRO's font color based on the homed condition.
I used a button to call the function to test.

Code: Select all

global x=DRO("Main_DRO_0");
global y=DRO("Main_DRO_1");
global z=DRO("Main_DRO_2");
global a=DRO("Main_DRO_3");
global axis=b; 
 &nbsp;x.DrawColor(0x00ff00, 0x000000);
 &nbsp;y.DrawColor(0x00ff00, 0x000000);
 &nbsp;z.DrawColor(0x00ff00, 0x000000);
 &nbsp;a.DrawColor(0x00ff00, 0x000000);

global Homed = function(axis,state) 
{ 
if(state==1){
 &nbsp;if (axis == 0){x.DrawColor(0xff0000, 0x000000);}
 &nbsp;if (axis == 1){y.DrawColor(0xff0000, 0x000000);}
 &nbsp;if (axis == 2){z.DrawColor(0xff0000, 0x000000);}
 &nbsp;if (axis == 3){a.DrawColor(0xff0000, 0x000000);}
 &nbsp;}
else{
 &nbsp;if (axis == 0){x.DrawColor(0x0000ff, 0x000000);}
 &nbsp;if (axis == 1){y.DrawColor(0x0000ff, 0x000000);}
 &nbsp;if (axis == 2){z.DrawColor(0x0000ff, 0x000000);}
 &nbsp;if (axis == 3){a.DrawColor(0x0000ff, 0x000000);}
 &nbsp;}
};

global CheckHomed = function() 
{ 
 &nbsp;state=GlobalGet("Axis1Homed");
 &nbsp;Homed(0,state);
yield();
 &nbsp;state=GlobalGet("Axis2Homed");
 &nbsp;Homed(1,state);
yield();
 &nbsp;state=GlobalGet("Axis3Homed");
 &nbsp;Homed(2,state);
yield();
 &nbsp;state=GlobalGet("Axis4Homed");
 &nbsp;Homed(3,state);
yield();
};

CheckHomed(); // i use this here to test in the mainscreen script to call the function
Last edited by Ya-Nvr-No on Mon Dec 28, 2015 3:50 am, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: No registered users and 77 guests