Jump to content


Photo

creating a random object on a random location


  • Please log in to reply
4 replies to this topic

#1 _174286

_174286

    GMC Member

  • New Member
  • 10 posts

Posted 02 November 2010 - 11:25 AM

Hello,

I want to create every second a random object (they are all in a map) on a random location. The problem is that gm may not create an object on the place of a wall, or outside my 'playground' (i dont use the entire room for my pacman game). How do i do this in GML?
  • 0

#2 jvpernis

jvpernis

    GMC Member

  • New Member
  • 19 posts

Posted 02 November 2010 - 02:57 PM

Hello,

I want to create every second a random object (they are all in a map) on a random location. The problem is that gm may not create an object on the place of a wall, or outside my 'playground' (i dont use the entire room for my pacman game). How do i do this in GML?

Room start event
-------------------------
:GM040:
-number of steps: 30 (I thought GameMaker runs at 30 steps/second)
-clock: Clock0
-------------------------



Clock0 event
-------------------
:GM072:
-variable = newrandomx
-new value = irandom_range(minimumx*,maximumx*)
*just fill in between which x the new object can be created, these are not meant to be variables. So if your room is 1000*500 (x*y) but your playground is centered 500*250 fill in "250,750"
:GM072:
-variable = newrandomy
-new value = irandom_range(minimumy*,maximumy*)
*same here, just fill in between which y then new object can be created.
:GM054:
-x=newrandomx
-y=newrandomy
-only solid objects (only if you want that it can be drawn evereywhere except where the wall is, if you want it to avoid any object then select any object)
:GM063:
:GM075:
-object1=...
-object2=...
-object3=...
-object4=...
-x=newrandomx
-y=newrandomy
:GM066:
:GM064: (if there is e.g. a wall in this new position)
:GM063:
***something to repeat this event i can't think of anything right now (you can set the alarm clock at one step but after 30 new places where already exists an object there will be a time difference of one second and you will notice..)***
:GM066:
:GM040:
-number of steps: 30
-clock: Clock0
-------------------


Try this..
  • 1

#3 _174286

_174286

    GMC Member

  • New Member
  • 10 posts

Posted 03 November 2010 - 10:59 AM

it works, but not like i want it, i want that the objects are created in the grid.

ps: Posted Image have to become another image (that one with lights) i think.
  • 0

#4 jvpernis

jvpernis

    GMC Member

  • New Member
  • 19 posts

Posted 03 November 2010 - 02:51 PM

it works, but not like i want it, i want that the objects are created in the grid.

ps: Posted Image have to become another image (that one with lights) i think.

Yeah you're right. Such a shame :whistle:. Anyway I have thought of what you really wanted with the grid. When you put :GM014: after the x and y are determined you have the chance that it aligns to the wall piece.

So eventually I came up with the idea of dividing the minimumx, mimimumy, maximumx and maximumy by 16 (if your grid is 16x16 else you must configure this), then return a random number between these and multiply it back with 16. In this way you know for sure that the newrandomx and newrandomy will always be in a grid ^_^ .
So this will do (I copied the most from my last post):

Room start event
-------------------------
:GM040:
-number of steps: 30 (I thought GameMaker runs at 30 steps/second)
-clock: Clock0
-------------------------



Clock0 event
-------------------
:GM072:
-variable = newrandomx
-new value = irandom_range(minimumxdividedbygridwidth,maximumxdividedbygridwidth)*gridwidth
:GM072:
-variable = newrandomy
-new value = irandom_range(minimumydividedbygridheight,maximumydividedbygridheight)*gridheight
:GM054:
-x=newrandomx
-y=newrandomy
-only solid objects (only if you want that it can be drawn evereywhere except where the wall is, if you want it to avoid any object then select any object)
:GM063:
:GM134:
-object1=...
-object2=...
-object3=...
-object4=...
-x=newrandomx
-y=newrandomy
:GM066:
:GM064: (if there is e.g. a wall in this new position)
:GM063:
:GM069:
event_perform(ev_alarm,0)
:GM066:
:GM040:
-number of steps: 30
-clock: Clock0
-------------------

Ps: I think I fixed the repeat event problem too..

Edited by jvpernis, 03 November 2010 - 04:09 PM.

  • 1

#5 _174286

_174286

    GMC Member

  • New Member
  • 10 posts

Posted 03 November 2010 - 03:21 PM

Thanks but i noticed another problem: he justs ignores the Posted Image thing. But no problem, i fixed it (but not on a good way, i think) like this
every object that should be created has on Collision with wall:
:GM124:

:) it works so much thanks for your help!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users