Efficient way of checking multiple values?

This forum is currently in read-only mode.
  • What is the best way to check multiple values against a variable? For example, if I have a private variable "state" and in my event sheet I have:

    if state = 0

    - do these events

    if state = 1

    - do these events

    if state = 2

    - do these events

    ...etc, for maybe 30 checks on the value of "state". This seems inefficient because if state=0, I don't need it to also check if it equals 1 through 30. I believe the switch statement from Gamemaker is similar to what I'm looking for. Is there anything like that or a better way of doing these checks?

    Thanks!

  • You could use the function object, set up a bunch of functions which are named using numbers between 1 and 30, then call a function using the private variable as the name. That should work as a reasonable work around, although I don't know if it would be more efficient.

    A switch function would be pretty sweet for Construct 2.

  • What is the best way to check multiple values against a variable? For example, if I have a private variable "state" and in my event sheet I have:

    if state = 0

    - do these events

    if state = 1

    - do these events

    if state = 2

    - do these events

    ...etc, for maybe 30 checks on the value of "state". This seems inefficient because if state=0, I don't need it to also check if it equals 1 through 30. I believe the switch statement from Gamemaker is similar to what I'm looking for. Is there anything like that or a better way of doing these checks?

    Thanks!

    I think this is the best way to do It. Just set state to random.

    if state = random (0,2)
       if state = 0
          - do these events
       if state = 1
          - do these events
       if state = 2
          - do these events[/code:1ev38f9e]
    
    This way it will switch the value. 
    Hope this help you.
  • Toralord is right, you can just set a number then use subevents to check that number, like a series of Ifs following on each other.

    Just make the events in the easiest way possible and don't worry about inefficiency - events add so much overhead that it's kind of ridiculous to worry about efficiency. You could set up an if-else-if-else-if event chain, but paradoxically, that might be less efficient, because the 'else' event does some special processing.

  • Thanks for the replies,

    linkman: Thats an interesting workaround. I agree though, not sure if that's more efficient and I think the way I'm coding things, using functions would make it a little messier. Thanks for the suggestion though.

    toralord: Does that work? If the value of state=0 you would still need to check if state=1 through 30 or however many values of state there are. Or am I not understanding how that works? I'm not concerned with switching the value of state, I'm concerned with checking the value of state 30 times rather than just once.

    Ashley: I guess I've found that when you do things efficiently, it has the side effect of making code more organized (though since I'm not really a coder that could be wrong). I also like to keep thinking in terms of optimization in case I switch to another engine that doesn't handle code so well.

  • Yes I will work.

    It will do it randomly just like the game maker switch statement you linked to.

    Thing about this cap and the game maker switch statement is that the low the range of values to check the more likely is going repeat it self but it will eventually check the all values.

    This kind of code is good for dialogues( character saying more then one thing.), guessing games, casino games, puzzles game, but mostly dialogues.

    I made a cap to show you an example.

    http://download71.mediafire.com/m2faimr ... witchs.cap

  • I see what you're saying and thanks for the cap. I probably didn't explain correctly so ill try and make it more clear. I want to check "state" only once per frame and not 30 times per frame if there are 30 state checks.

    So in your example, when state is found to equal 0 it runs those state=0 events. But then it also goes through and checks if it equals 1,2,3,4,5 etc. I want it to skip checking if state=1, if state=2, if state=3, etc. because state is already known to equal 0. I'm pretty sure thats how the switch statement works in gamemaker.

  • It doesn't really matter if you check all 30 variables, because the performance impact is ultra small. Just one collision check is probably hundreds of times more intensive than a simple variable check.

    [quote:s8pz6ncv] events add so much overhead that it's kind of ridiculous to worry about efficiency.

    Did you mean so little overhead? Or are you comparing it to real code?

  • You could also run a loop.

    For 1-30

    if state=loopindex

    • do events
    • end loop

    That way it will stop when the event runs.

  • You should use this.

    Every tick
    if state = 0
      - do this event[/code:1fxbs5bn]
    This way it will check it once per frame.
    That should do it.
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • It doesn't really matter if you check all 30 variables, because the performance impact is ultra small. Just one collision check is probably hundreds of times more intensive than a simple variable check.

    Well said. There's no good way to do this - you can't skip the other events once you know you've hit one - but it doesn't matter, because the impact is so tiny you'd have 10,000x more effect (literally) on performance worrying about, say, the rendering efficiency. So why sweat the small stuff?

    [quote:28hk23w1] events add so much overhead that it's kind of ridiculous to worry about efficiency.

    Did you mean so little overhead? Or are you comparing it to real code?

    Compared to real code, I mean. In traditional languages individual commands are very basic and low overhead. But events are very high-level, and do a lot of work for you in comparison to traditional script, so worrying about their performance is counterintuitive: they already perform badly compared to real script, but it doesn't really matter.

    For 1-30

    if state=loopindex

    • do events
    • end loop

    This would only work if each event for 1-30 had the same actions. But then why would you repeat an identical event 30 times in the first place?

    Every tick

    if state = 0

    - do this event

    An 'every tick' or 'always' event is redundant, because all events are already checked every tick.

  • I thought of that after posting. It could still potentially work if you used a function to call another event (call event loopindex), but IIRC there's some overhead calling a function, which might make it less efficient.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)