Jump to content


Photo

Selecting an object (randomly) from a few.


  • Please log in to reply
5 replies to this topic

#1 HatchetHarriette

HatchetHarriette

    GMC Member

  • New Member
  • 16 posts
  • Version:GM8

Posted 10 July 2012 - 06:18 PM

So, I have a bunch of characters. I'd like to have Game Maker, though scripting, select one of those characters and spawn it.

So far I know you can store arrays, lists as it were. However I do not know how, if able, to store an array of objects. I think I am able to declare a global variable. Say:

global.nextCharacterToSpawn = (random from list)

And then, at the end of an alarm :

instance_create(x, y, global.nextCharacterToSpawn)

Now, how would I store objects into an array and then select randomly one randomly from the array, at random randomly. ;)

(for a more winded explanation- In this project I am having the game select a random character, who would then walk on screen and stay at a position for a randomly allotted time (from a range) then that character would wander off screen and destroy.)

Thanks community!
  • 0

#2 Johnjwj

Johnjwj

    Question.

  • GMC Member
  • 111 posts
  • Version:GM8

Posted 10 July 2012 - 06:24 PM

Couldn't you just store a number of objects in an array, like:

array[0]=Object1
array[1]=Object2
array[2]=Object3
// and so on...

and then when spawning use:

instance_create(x, y, array[random(2)])

Or have you already tried that?
  • 0

#3 LOS.A

LOS.A

    GMC Member

  • GMC Member
  • 597 posts
  • Version:GM8

Posted 10 July 2012 - 06:26 PM

Well, it's really very straight forward. Just set the array indices to the object ids and you're all done.
array[0] = object0
array[1] = object1
array[2] = object2
array[3] = object3
array[4] = object4

random_object = array[irandom(4)]

Edit: Oh.

Edited by LOS.A, 10 July 2012 - 06:27 PM.

  • 0

#4 HatchetHarriette

HatchetHarriette

    GMC Member

  • New Member
  • 16 posts
  • Version:GM8

Posted 10 July 2012 - 07:00 PM

Arrays are really that easy huh? That's good. However how does the scope work on Arrays? Could I define it with global. ?
  • 0

#5 LOS.A

LOS.A

    GMC Member

  • GMC Member
  • 597 posts
  • Version:GM8

Posted 11 July 2012 - 03:18 PM

Could I define it with global. ?

Define it and use it as normal variables. If you have local arrays with the same name as the global ones, use global.; if not, you could define the array like this:
globalvar array;
and use the array freely without the need of the global. prefix.

Edited by LOS.A, 11 July 2012 - 03:19 PM.

  • 1

#6 HatchetHarriette

HatchetHarriette

    GMC Member

  • New Member
  • 16 posts
  • Version:GM8

Posted 12 July 2012 - 12:35 PM

Thank you very much for your help!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users