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.

Calling a function with a random element from an array?

Yeah, sorry, I'm really new to object-oriented programming, and I'm really only familiar with C and perl, so...

Anyway, what I'm trying to do is make an event-based battle initiator that opens a battle with one of a list of possible enemy troupes. I was thinking about designating a group of system variables as this list of potential battles, putting those variables into an array, and then calling the battle control with a random element from that array.

So here's my specific problem: how do I put those system variables in an array? Then how do I pass a random element to the battle initiator function? Actually...how do I pass anything other than a raw system variable (using the pop-up selection screen in the event editor) to that function?

I don't know if this is even the best way to do what I want, but I'd really like to try to keep it in the event editor so that it's easier for non-scriptors to modify it. That, and I sort of hate the way RGSS seems to work, haha

Any help is really appreciated, thanks!!!
 

arev

Sponsor

To put a variable into array, first create this array:

@array = []

then type:

@array << variable

To chose a random element go with:

@array[rand(@array.size)]

Hope that's what you were asking ; )
 
OMG perfect, thanks so much!!! I just hope I don't have any more questions about how to make this work, haha, I'm sort of backwards on programming and don't understand syntax sometimes. xP

Edit: actually, here's another array question... Is there a way to send a batch of elements to the array? I.E. when I want to actually populate the array with enemy troops, can I do something like @array << (troop00..troop10)? Haha, this would be so much easier in perl or python! ><
 

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