SOLVED Help with switch (Check for multiple variables "disregarding" order)

Radulosa

Member
Hello,

I'm thinking of the following thing:

We have a character that can interact with an item which generates other random items from a list.
The items generated are made with instance_create_layer.
When they touch the character they can get assigned either to its leftHand (variable) or its rightHand (variable) depending on which one is free.

The character also has the ability to combine them, and depending on which items are being held different results are possible.

All good until now.

Is there a way to check what instance are being assigned to leftHand/rightHand without having zillions of if statements?

something like

switch (leftHand + rightHand)

case x:

....
break

Also if there's a way to disregard the order that would be awesome (leftHand = oBat rightHand = oNails generate the same result as leftHand = oNails rightHand = oBat)

Hope this makes sense
Radu
 
Last edited:

curato

Member
conditional statements like if or switch would be the standard way to go there. If is possible if you were really clever with the numbers you had for your item variables that item1 + item2 could yield Item3 without the if statements. I saw the old school inventory system like that each combination was unique. It might take more thought that it is worth to avoid a block of conditionals these days thought.
 

chamaeleon

Member
Perhaps some of the functionality can be hidden in a function that returns true/false if both arguments satisfies the condition.
GML:
function dualHeld(item_a, item_b) {
    return (leftHand == item_a && rightHand == item_b) || (leftHand == item_b && rightHand == item_a)
}

function singleHeld(item) {
    return leftHand == item || rightHand == item;
}
GML:
if (dualHeld(oBat, oNails)) {
    ... // do something
} else if (singleHeld(oBat) {
   ... // do something else
} else if (...) {
   ... // do something else
}
Or just have a single function and call it multiple times
GML:
function held(item) {
    return leftHand == item || rightHand == item;
}
GML:
if (held(oBat) && held(oNails)) {
    ... // do something
} else if (held(oBat) {
   ... // do something else
} else if (...) {
   ... // do something else
}
 
Last edited:

Radulosa

Member
There is a way!

you can do the following:


GML:
switch (sprLeft + sprRight) {
        
       case "oBatoNails":
       case "oNailsoBat":
      
       Result = oNailsBat
      
       break
      
       case "oWhatever":
      
       ....
      
       break

    
switch (Result) {
        
    case oNailsBat:
    
    instance_create_layer(x,y-70,"Instances",Result)

    break

    case Whatever:
    
    ...

    break
 

Nidoking

Member
Of course, you'll have to get the asset name of the object index for both objects. I suspect that many people reading this are just going to try adding the numbers and comparing them to a string and wonder why it doesn't work. It also potentially means quite a lot of work if you ever change the object names, because a simple Search-and-Replace isn't going to do the job anymore. But that is a creative way to handle the problem.
 
Top