How to use the "snap to grid" function during the game?

Get help using Construct 2

Post » Sat Jul 12, 2014 9:06 am

Hello

I'm making a space buldier game and I'd like to do a similar function as snap to grid but during in game!
For exemple I'll select a station and I want to build it. This can only build places specified in the space.
Such as snap to grid function in the game editor!

Is there some simple solution? For exemple something event settings or other else...

I hope you understand what I want :)
B
5
S
1
Posts: 37
Reputation: 441

Post » Sat Jul 12, 2014 9:58 am

Make 2 variables for the amount of pixels in each incrememnt you want objects to snap to named snapX and snapY.

Then when you are placing the object, set it to Self.X - (Self.X % snapX) and Self.Y - (Self.Y % snapY)

The % operator is called modulo, modulus, mod, etc. It gets the reainder after a division. So youre subtracting the amount of pixels the object is between 2 snap points, and setting it to the lower one.
B
46
S
14
G
7
Posts: 603
Reputation: 8,096

Post » Sat Jul 12, 2014 10:05 am

Following on from EncryptedCow, if you are using the mouse to then:

Set X position to : Mouse.X - Mouse.X % gridSize
Set Y position to : Mouse.Y - Mouse.Y % gridSize

where gridsize is whatever you want.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
131
S
29
G
81
Posts: 5,328
Reputation: 56,630

Post » Sun Jul 13, 2014 11:31 am

Thanks! Your solutions is worked!

------

Global number: snapY = 22
Global number: snapY = 20

Set position to + Create object:
Mouse.X - (Mouse.X%snapX)
Mouse.Y - (Mouse.Y%snapY)

Thanks!
B
5
S
1
Posts: 37
Reputation: 441


Return to How do I....?

Who is online

Users browsing this forum: Darth Crusher, illusionbr, zsoldiers and 7 guests