Select smallest global from group

Get help using Construct 2

Post » Sun Jun 02, 2013 6:50 pm

Quick question, how do I select the lowest value from a group of global variables?

For instance, I have three globals:

G1= 100
G2= 200
G3= 500

They change occasionally.

On button clicked, I would like the lowest value returned.

How would I do this?

(Please keep scale in mind.... how would I compare 100 globals? 1000?)

Thanksbigpappapuck2013-06-03 03:32:34
B
11
S
3
G
2
Posts: 26
Reputation: 3,124

Post » Sun Jun 02, 2013 8:00 pm

I would give up globals, and instead try using an array or a dictionary, and iterate over it with something like:
for (x < size of my dict)
- lmin = min(lmin,mydict.get(loopindex("x")))

Greg
B
17
S
3
G
3
Posts: 79
Reputation: 3,703

Post » Sun Jun 02, 2013 8:41 pm

As long as you only need to test a few globals it should be fine, but if you plan on testing a lot then i wouldnt use it either.

But a way you can do it, is by adding a local variable or global, whatever suits you.

Lets call it:
Lowest_value_found

Then you add:

If G1 < G2 Set lowest_value_found = G1.value

After that you test the next one against than value.. so:

If Lowest_value_found > G3 then Set lowest_value_found = G3.value

and then you continue.

I dont think there is a way to loop through Global variables, so using this method is really bad if you have to test like 1000s, as you would have to do a lot of tests. Then you would be better off, using an array as already suggested. If you dont like arrays you can do it with a list as well.nimos1002013-06-02 20:41:59
B
44
S
11
G
2
Posts: 1,181
Reputation: 6,801

Post » Mon Jun 03, 2013 3:13 am

Thanks guys.

I ended up just using the globals as: min(G1, G2, G3, G4, G5). This worked fine.

Never used the dictionary before, so that was confusing.

I did try with an array, but wasn't successful. I have the globals being saved in the array, yet I couldn't find the correct way to get the action to return the min value.

I tried min(array.CurX), but that didnt work.

Any thoughts?
bigpappapuck2013-06-03 03:39:37
B
11
S
3
G
2
Posts: 26
Reputation: 3,124


Return to How do I....?

Who is online

Users browsing this forum: RogerThompson, SoldjahBoy and 8 guests