A smarter way to compare to all objects

Discussion and feedback on Construct 2

Post » Mon Apr 16, 2012 2:06 pm

I would like to check if the player's object is bigger than every other object.

My code works like the following at the moment:

global is_winner = 1

for all opponent_objects:
   if opponent_object.width > player.width:
      is_winner = 0

if is_winner == 1:
   player has won the game!

I don't like to use a global variable for this purpose. Is there another way to do it?

I hope the pseudocode is easy to understand, thanks very much in advance!
B
3
G
2
Posts: 4
Reputation: 1,108

Post » Mon Apr 16, 2012 2:56 pm

What's wrong with using a global variable? What you've done looks fine. If you don't want the variable to be accessible anywhere else, move all that in to a group or subevent and it will become a local variable not available anywhere else.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600

Post » Mon Apr 16, 2012 4:34 pm

Thanks for your answer Ashley! I wasn't even aware of local variables :)

I thought one should try to avoid global variables if not necessary.
B
3
G
2
Posts: 4
Reputation: 1,108


Return to Construct 2 General

Who is online

Users browsing this forum: 99Instances2Go, Artpunk and 13 guests