MAXSCRIPT error with multiple If expressions - max

I've started learning Maxscripts and i've now hit a wall,
im trying to get the name of my selection, if it's a single object and
then if its more than 1, have the label display the number of objects as a string.
but i keep getting an error... any idea?
group "Current Selection:"
(
label lbl_01 "Nothing Selected"
)
---------------------------------------------------------------------------------------------------------------// Current Selection Function
fn letmeknow obj=
(
local contador = (selection.count as string)
if selection.count != 0 then
(
lbl_01.text = ("Name: " + obj.name)
)
else
(
lbl_01.text = "Nothing Selected"
)
if selection.count >= 2 do (lbl_01.text = ("Objects: " + contador))
)

Looks like the issue is outside the code you've supplied and without seeing the rest of the code, it's hard to tell. Anyway, here's a working example using case expression instead of multiple ifs:
rollout test "Test"
(
group "Current Selection:"
(
label lbl_01 "Nothing Selected"
)
button btnTest "Test"
fn getSelectionString =
(
case selection.count of
(
0 : "Nothing Selected"
1 : "Name: " + selection[1].name
default : "Objects: " + selection.count as string
)
)
on btnTest pressed do
lbl_01.text = getSelectionString()
)
createDialog test

Related

Index error occurs when trying to use an event listener

I am trying to add a touch event listener to the image object being loaded. Although this is practically an exact copy and paste from the documentation:
https://docs.coronalabs.com/api/type/EventDispatcher/addEventListener.html
It returns the following error:
36: attempt to index local 'object' (a nil value)
local t = {}
local img = {}
local i = 1
local function showImages ()
local function networkListenerImg( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end
end
for k,v in pairs(t) do
img[#img + 1] = v
end
local object = display.loadRemoteImage( event.params.chapter .. img[i], "GET", networkListenerImg, img[i], system.TemporaryDirectory, 50, 50 )
function object:touch( event )
if event.phase == "began" then
print( "You touched the object!" )
return true
end
end
object:addEventListener( "touch", object )
end
The table, t, is populated elsewhere in the code and is populated properly.
While you didn't mention which of those lines is line 36 (there are only 28 lines there), I can still see your error. The issue is that object is and always will be nil: display.loadRemoteImage() does not return anything, see this.
What you need to do is have your listener callback capture object, which must be declared before the callback is. The callback should then set the value of object to the results of the download. Like so...
local t = {}
local img = {}
local i = 1
local function showImages ()
local object
local function networkListenerImg( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
-- fill in code to save the download object into "object"
end
end
for k,v in pairs(t) do
img[#img + 1] = v
end
display.loadRemoteImage( event.params.chapter .. img[i], "GET", networkListenerImg, img[i], system.TemporaryDirectory, 50, 50 )
function object:touch( event )
if event.phase == "began" then
print( "You touched the object!" )
return true
end
end
object:addEventListener( "touch", object )
end

lua corona - How to load an image using loadRemoteImage

I want to load an image from a URL.
I can load an image using this code:
local group = display.newGroup();
local testImg;
testImg = display.newImage( "cells/cellBottom.png");
group:insert( testImg );
but i need to use something like:
testImg = display.loadRemoteImage( "https://www.dropbox.com/s/fqlwsa5gupt5rsj/amcCells.png")
group:insert( testImg );
Please tell me how to load this image.
Cheers :)
Just use example given in Corona. This works with the following url but your url seems to have problem.
local function networkListener( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
end
print ( "event.response.fullPath: ", event.response.fullPath )
print ( "event.response.filename: ", event.response.filename )
print ( "event.response.baseDirectory: ", event.response.baseDirectory )
end
display.loadRemoteImage( "http://coronalabs.com/images/coronalogogrey.png", "GET", networkListener, "coronalogogrey.png", system.TemporaryDirectory, 50, 50 )
try add yougroup:insert(event.target) in listener
local function networkListener( event )
if ( event.isError ) then
print ( "Network error - download failed" )
else
event.target.alpha = 0
transition.to( event.target, { alpha = 1.0 } )
**yourgroup:insert(event.target)**
end
print ( "event.response.fullPath: ", event.response.fullPath )
print ( "event.response.filename: ", event.response.filename )
print ( "event.response.baseDirectory: ", event.response.baseDirectory )
end
display.loadRemoteImage( "http://coronalabs.com/images/coronalogogrey.png", "GET", networkListener, "coronalogogrey.png", system.TemporaryDirectory, 50, 50 )

FileMaker CurrentTime conversion to 24-hour format

In FileMaker Pro, I am trying to append the current date and time to a filename to which I export data. If I use
Get (CurrentTime)
I get 12-hour time, complete with " PM" or " AM" at the end. Is there built-in functionality to return 24-hour time, instead?
FileMaker help says that the format follows the format of system time, but that is not the case. System time is showing as 17:22, but CurrentTime is returning 52218 PM. (Mac OS 10.8.5, FileMaker Pro 12.0v4.)
Filemaker's internal time storage notation is simply the number of seconds elapsed since midnight of the current day.
I.e. 56659 seconds since midnight = 3:44:19 PM.
When exporting data, you can check off the "Apply current layout's data formatting to exported data" checkbox, so that times displayed as 24-hour in FMP layouts are exported as such.
But, for other internal use such as the file-naming case you're asking about, you will need to use a custom function to convert the output of Get(currentTime) to 24-hour format.
For example, see the TimeFormatAs ( theTime ; type12or24 ) function at Briandunning.com.
(Full code of the custom function is pasted below for protection against dead links in the future, but if the link above still works, use that version as it may be more up-to-date:)
/*---------------------------------------------------------------
Function Name: TimeFormatAs
Syntax: TimeFormatAs ( theTime; type12or24 )
Author - Jonathan Mickelson, Thought Development Corp.
(www.thought-dev.com)
---------------------------------------------------------------*/
Case ( not IsEmpty ( theTime ) ;
Let (
[
// FIXED VARIABLES
padHoursChar = "" ; // Character to pad the Hours with in a text result, (Ex."0", " ", "")
padAMPMChar = " " ; // Character to pad the AM/PM with in a text result, (Ex."0", " ", "")
suffixAM = "AM" ; // <------------ CHANGE AM Suffix Here
suffixPM = "PM" ; // <------------ CHANGE PM Suffix Here
// DYN. VARIABLES
theTime = GetAsTime ( theTime ) ;
hasSeconds = PatternCount ( GetAsText ( theTime ) ; ":" ) = 2 ;
secs = Mod ( Seconds ( theTime ) ; 60 ) ;
mins = Mod ( Minute ( theTime ) ; 60 ) + Div ( Seconds ( theTime ) ; 60 ) ;
hours = Hour ( theTime ) + Div ( Minute ( theTime ) ; 60 ) ;
// -------------- BEGIN 24 HOUR TIME CALC ----------------------
result24 = GetAsTime ( theTime ) + 1 - 1 ;
// -------------- BEGIN 12 HOUR TIME CALC ----------------------
hours = Mod ( Hour ( theTime ) ; 12 ) ;
tempHours = Case ( ( hours < 1 ) or ( hours - 12 = 0 ) ; 12 ; hours ) ;
calc12Hours =
Left (
padHoursChar & padHoursChar ;
2 - Length ( tempHours )
) &
tempHours ;
calc12Minutes = Left ( "00" ; 2 - Length ( mins ) ) & mins ;
calc12Seconds = Left ( "00" ; 2 - Length ( secs ) ) & secs ;
calc12Suffix = Case ( Mod ( Hour ( theTime ) ; 24 ) >= 12 ; suffixPM ; suffixAM ) ;
result12 = calc12Hours &
":" & calc12Minutes &
// if original time included a non-zero seconds value, display seconds
Case ( hasSeconds and secs > 0 ; ":" & calc12Seconds ) &
padAMPMChar & calc12Suffix
] ;
Case ( type12or24 >= "24" ; result24 ; result12 ) // END CASE
) // END LET
) // END CASE

Resetting button input in xInput using an Xbox controller

I'm using XInput for a project and I'm having a problem using the xbox controller. When I use the B button to return to a previous state, its registering the button press so fast that it continues to push back multiple states that use the B button to exit.
if( ( player1.GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B ) || ( player2.GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B ) ) {
return ESC_BUTTON;
}
I've tried resetting the buffer by adding an & 1 but than the input never registers. Is there an easy way to fix this problem?
UPDATE:
current = (DWORD)( ( start - GetTickCount() ) / 1000.f );
if( current > 0.005f )
{
if( ( player1.GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B ) || ( player2.GetState().Gamepad.wButtons & XINPUT_GAMEPAD_B ) )
{
start = GetTickCount();
return ESC_BUTTON;
}
}
Still having issues with resetting. Sometimes it works, sometimes it doesn't...thoughts?

How to Winwait for two windows simultaneously in AutoIt?

I would like to know if its possible to WinWaitActive for WindowWithThisTitle and WindowWithThatTitle at the same time. I'm executing a command and there could be a window telling me that the connection failed or a user/pass dialog coming up.
Is there another way doing it as this?
WinWaitActive("Title1", "", 5)
If(WinExists("Title1")) Then
MsgBox(0, "", "Do something")
Else
If(WinExists("Title2")) Then
MsgBox(0, "", "Do something else")
EndIf
EndIf
Because I don't want to have the timeout which could be more than 15 seconds.
A simpler solution might be to use a REGEX title in your WinWaitActive as defined here
You would then have something like this:
$hWnd = WinWaitActive("[REGEXPTITLE:(WindowWithThisTitle|WindowWithThatTitle)]")
If WinGetTitle($hWnd) = "WindowWithThisTitle" then
DoSomething()
Else
DoSomethingElse()
EndIf
How about something like this.
$stillLooking = True
While $stillLooking
$activeWindowTitle = WinGetTitle(WinActive(""))
If $activeWindowTitle == "Title1" Then
MsgBox(0, "", "Do something")
$stillLooking = False
ElseIf $activeWindowTitle == "Title2" Then
MsgBox(0, "", "Do something else")
$stillLooking = False
EndIf
sleep(5)
WEnd
Because I don't want to have the
timeout which could be more than 15
seconds.
WinWaitActive() doesn't have a timeout unless you specify one. You gave it a five second timeout but you could leave that off and it would wait forever.
You can use this Functions for two windows ..
; #FUNCTION# ====================================================================================================================
; Name...........: _2WinWait
; Description ...: Wait For Tow Windows .
; Syntax.........: _2WinWait ($FirstTitle,$SecondTitle,[$FirstText = "" ,[$SecondText = ""]] )
; Parameters ....: $FirstTitle - Title Of First Wondow
; $SecondTitle - Title Of Second Wondow
; $FirstText - Text Of First Wondow
; $SecondText - Text Of Second Wondow
; Return values .: Success - None
; Failure - Returns a 0 => If Your Titles Is Wrong
; Author ........: Ashalshaikh : Ahmad Alshaikh
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _2WinWait ($FirstTitle,$SecondTitle,$FirstText = "" ,$SecondText = "" )
If $FirstTitle = "" Or $SecondTitle = "" Then
Return 0
Else
Do
Until WinExists ($FirstTitle,$FirstText) Or WinExists ($SecondTitle,$SecondText)
EndIf
EndFunc
; #FUNCTION# ====================================================================================================================
; Name...........: _2WinWait_Any
; Description ...: Wait For Tow Windows And Return Any Window Id Exists .
; Syntax.........: _2WinWait_Any ($FirstTitle,$SecondTitle,[$FirstText = "" ,[$SecondText = ""]] )
; Parameters ....: $FirstTitle - Title Of First Wondow
; $SecondTitle - Title Of Second Wondow
; $FirstText - Text Of First Wondow
; $SecondText - Text Of Second Wondow
; Return values .: Success - Number Of Window ==> 1= First Window , 2= Second Window
; Failure - Returns a 0 => If Your Titles Is Wrong
; Author ........: Ashalshaikh : Ahmad Alshaikh
; Remarks .......:
; Related .......:
; Link ..........;
; Example .......; No
; ===============================================================================================================================
Func _2WinWait_Any ($FirstTitle,$SecondTitle,$FirstText = "" ,$SecondText = "" )
If $FirstTitle = "" Or $SecondTitle = "" Then
Return 0
Else
Do
Until WinExists ($FirstTitle,$FirstText) Or WinExists ($SecondTitle,$SecondText)
If WinExists ($FirstTitle,$FirstTexit) Then
Return 1
Else
Return 2
EndIf
EndIf
EndFunc
for more with examples
I'm fairly new to autoit and the programming world in general and I had this same dilemma. Luckily I figured out a straight fwd way to do it:
Do
$var1 = 0
If WinGetState("Document Reference","") Then
$var1 = 1
ElseIf WinGetState("Customer Search","") Then
$var1 = 1
EndIf
Until $var1 = 1
So it'll stay in the loop until it finds the window and sets $var1 to 1. There's probably easier ways (I'm sure developers are gasping at this) but this is straight fwd enough for me.
You can create an infinite while loop with if statements in there:
#include <MsgBoxConstants.au3>
Example()
Func Example()
While 1
; Test if the window exists and display the results.
If WinExists("Windows Security") Then
Local $hWnd = WinWaitActive("Windows Security", "", 2000)
ControlSetText($hWnd, "", "[CLASS:Edit; INSTANCE:1]", "hel233")
ControlClick("Windows Security","","[CLASS:Button; INSTANCE:2]")
Sleep(5000)
EndIf
; Test if the window exists and display the results.
If WinExists("Spread the Word") Then
'The line below will wait until the window is active, but we don't need that
'Local $hWnd = WinWaitActive("Spread the Word", "", 2000)
WinClose("Spread the Word")
Sleep(5000)
EndIf
wend
EndFunc

Resources