Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Some array stuff~

My knowledge on arrays is quite limited, and I don't want to do something wrong so I'm posting it here. :)

This is the code:

if $eds0 != nil
< add $eds0 to array A >
end

(repeat the above code a few times..)

< for each thing in the array A do:>
i = < the current thing from the array A >
key = [$game_map.map_id,i.id,"B"]
$game_self_switches[key] = false
key = [$game_map.map_id,i.id,"C"]
$game_self_switches[key] = false
end

< clear array A >

The stuff in < and >'s is the stuff I'm not sure how to do.

Basically what it does/needs to do is to turn self switch B and C off for each thing in the array that got added previously. :)

Thanks again, RMXP.org people. :3
 
I hope I understood this right. Here's a functional code (of course, expecting that @array is defined as an array before this point).
Code:
if $eds0 != nil
  @array.push($eds0)
end

@array.each do |i|
  key = [$game_map.map_id, i.id, 'B']
  $game_self_switches[key] = false
  key = [$game_map.map_id, i.id, 'C']
  $game_self_switches[key] = false
end

@array.clear

Hope this helps.

Just a quick edit. There's other ways around this, but it might explain arrays better for you. When you create loops to find indexes, you can use the 'for' loop. This is commonly used in the RMXP default engine. Like so:
Code:
for i in 0...@array.size
  p @array[i]
end
The syntax for the 'for' loop is as follows:
Code:
for variable to set in range/array
So as above, it sets every index to 'i' every loop, increasing it by 1. Two dots (..) go from A to B. Three dots (...) goes from A to B, but excludes the last. Useful in arrays. For instance, the first index of arrays are usually 0. The size of an array with 0, 1 and 2 indexes is 3. You don't have anything at index 3, so you will put the three dots to exclude the last index.

Makes sense?
 
Twin Matrix":3f33azn7 said:
Yup, makes sense. :)

Thanks so much!

Which one would be better though? The 'for' loop or the .each do?
It surely depends on the context. For this, I suggest using the .each, as it saves you typing out:
Code:
@array[i].id
instead of just 'i.id'.

Also note that do ... end is the same as using { }.

Example:
Code:
@array.each {|i| p i.id}
 

Zeriab

Sponsor

There is a scope difference. The difference is small and is very unlikely to cause problems. This snippet illustrates the difference:
Code:
a = [1,2,3,4]

for x in a
  x = x + 1
end

p x, a

a.each {|y| y = y + 1}

p y, a

Look also at this code:
Code:
a = [1,2,3,4]

x = 'hello'

for x in a
  x = x + 1
end

p x, a

y = 'hello'

a.each {|y| y = y + 1}

p y, a

Can you spot the difference?
In practice it's really more a question of preference.
Conceptually you can say that the .each method takes a piece of code and executes it internally on each element in the collection. The 'for'-loop takes each element out of the collection in turn and executes the given code on it externally.
That's how I look at them and how I decide which to use.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top