Éviter les If en cascade

Aide et conseils concernant AutoIt et ses outils.
Règles du forum
.
Répondre
Avatar du membre
Nicoloquinte
Niveau 4
Niveau 4
Messages : 70
Enregistré le : ven. 15 juil. 2016 11:57
Status : Hors ligne

Éviter les If en cascade

#1

Message par Nicoloquinte »

Bonjour :mrgreen:

Je me heurte a un gros problème ,
Je souhaite effectuer une action quand j'appuie sur une touche de mon contrôleur Xbox 360 , Lancer une action , cela fonctionne , maintenant , si j'ai 14 boutons sur mon contrôleur Xbox et 14 choix pour chaque bouton , ça se complique et ça fini en
IF .. Then
     IF .. Then ... EndIf
     IF .. Then ... EndIf
     IF .. Then ... EndIf
EndIf
:( :(

Cela 14 lignes 14 fois , ça fait beaucoup :shock: , alors je voulais vous demander si ce n'était pas possible de compacter cela :?:

Voici le code pour detecter une touche Préssée (plus Xinput.au3)
#include <_XInput.au3>
$inputhwnd = _XInputInit()
$message = 0
SplashTextOn("Xbox Controller Text", $message, -1, -1, -1, -1, 4, "")
While 1
$btext = ""
$timer = 50
$input = _XInputGetInput($inputhwnd)
$buttons = _XInputButtons($input[2])
_XInputVibrate($inputhwnd,round(($input[3] / 255) * 100),round(($input[4] / 255) * 100))
_XInputVibrate($inputhwnd,round(32768),round(32768))
If $buttons[1] Then $btext = $btext & "Up" & @CRLF
If $buttons[2] Then $btext = $btext & "Down" & @CRLF
If $buttons[3] Then $btext = $btext & "Left" & @CRLF
If $buttons[4] Then $btext = $btext & "Right" & @CRLF
If $buttons[5] Then $btext = $btext & "Start" & @CRLF
If $buttons[6] Then $btext = $btext & "Back" & @CRLF
If $buttons[7] Then $btext = $btext & "LJoy" & @CRLF
If $buttons[8] Then $btext = $btext & "RJoy" & @CRLF
If $buttons[9] Then $btext = $btext & "LButton" & @CRLF
If $buttons[10] Then $btext = $btext & "RButton" & @CRLF
If $buttons[11] Then $btext = $btext & "A" & @CRLF
If $buttons[12] Then $btext = $btext & "B" & @CRLF
If $buttons[13] Then $btext = $btext & "X" & @CRLF
If $buttons[14] Then $btext = $btext & "Y" & @CRLF

$message = "-=Input=-" & @CRLF & "Packet Number: " & $input[1] & @CRLF & _
   "Buttons: " & $input[2] & @CRLF & "Left Trigger: " & $input[3] & @CRLF & _
   "Right Trigger: " & $input[4] & @CRLF & "Left Joy X: " & $input[5] & @CRLF & _
   "Left Joy Y: " & $input[6] & @CRLF & "Right Joy X: " & $input[7] & @CRLF & _
   "Right Joy Y: " & $input[8] & @CRLF & @CRLF & "-=Buttons=-" & @CRLF & $btext
ControlSetText("Xbox Controller Text", "", "Static1", $message)
Sleep($timer)
WEnd
Merci d'avance :mrgreen:
Nico ¤_¤
Faco
Niveau 4
Niveau 4
Messages : 80
Enregistré le : lun. 29 juil. 2013 18:09
Status : Hors ligne

Re: Éviter les If en cascade

#2

Message par Faco »

Salut,

Tu peux faire un switch au lieu que tous tes if mais ça fera pas moins de lignes.

++
Avatar du membre
orax
Modérateur
Modérateur
Messages : 1479
Enregistré le : lun. 23 mars 2009 04:50
Localisation : ::1
Status : Hors ligne

Re: Éviter les If en cascade  

#3

Message par orax »

Local $btext = ""
Local $buttons[15]
$buttons[1]  = 1 ; Up
$buttons[8]  = 1 ; RJoy
$buttons[14] = 1 ; Y

Local $a = [0, "Up", "Down", "Left", "Right", "Start", "Back", "LJoy", "RJoy", "LButton", "RButton", "A", "B", "X", "Y"]
$a[0] = UBound($a) - 1

For $i = 1 To $a[0]
   If $buttons[$i] Then $btext &= $a[$i] & @CRLF
Next

ConsoleWrite($btext)
De petits détails peuvent faire toute la différence. — Quand la boule de neige commence à rouler… poussez-la. (Columbo)
Avatar du membre
Nicoloquinte
Niveau 4
Niveau 4
Messages : 70
Enregistré le : ven. 15 juil. 2016 11:57
Status : Hors ligne

Re: Éviter les If en cascade

#4

Message par Nicoloquinte »

Merci pour ce code , mais je n'arrive pas a l'appliquer au fait que ce soit un fichier texte qui définisse la touche "remappé"
Nico ¤_¤
Avatar du membre
Nicoloquinte
Niveau 4
Niveau 4
Messages : 70
Enregistré le : ven. 15 juil. 2016 11:57
Status : Hors ligne

Re: Éviter les If en cascade

#5

Message par Nicoloquinte »

En fait , je comprend pas trop ce code , tu utilise des tableaux multi dimmentionels ? :shock:
Nico ¤_¤
Avatar du membre
mikell
Spammer !
Spammer !
Messages : 6292
Enregistré le : dim. 29 mai 2011 17:32
Localisation : Deep Cévennes
Status : Hors ligne

Re: Éviter les If en cascade

#6

Message par mikell »

Non, c'est 2 tableaux 1D (de taille identique)

Code : Tout sélectionner

If $buttons[$i] Then $btext &= $a[$i] & @CRLF
ça veut dire : "si l'array $buttons n'est pas vide à l'index $i , alors rajoute à $btext la valeur qui figure dans l'array $a à l'index $i "

Edit
et un @CRLF :mrgreen:
" L'échec est le fondement de la réussite. " (Lao-Tseu )
" Plus ça rate, plus on a de chances que ça marche " (les Shadoks )
Avatar du membre
Nicoloquinte
Niveau 4
Niveau 4
Messages : 70
Enregistré le : ven. 15 juil. 2016 11:57
Status : Hors ligne

Re: Éviter les If en cascade

#7

Message par Nicoloquinte »

Haaa ok , ça supprime 196 lignes a mon code , c'est pas mal :mrgreen:
merci beaucoup :wink:
Nico ¤_¤
Répondre