RANDOM colour in godot.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By THE HELIX

I am creating a game similar to bubble shooter.I instanced the bubble which is white in color.How can randomize the shader/color to generate different colors for the white colored bubble for the instanced scene ?

:bust_in_silhouette: Reply From: kidscancode

No need for a shader, just adjust the bubble’s modulate property. For example:

extends Area2D  # or whatever your bubble is

func _ready():
    modulate = Color(1.0, 0.0, 0.0, 1.0)  # red

If you want to select random colors, you would probably want to make an array of colors to choose from:

extends Area2D  # or whatever your bubble is

colors = [Color(1.0, 0.0, 0.0, 1.0),
          Color(0.0, 1.0, 0.0, 1.0),
          Color(0.0, 0.0, 1.0, 0.0)]

func _ready():
    randomize()
    modulate = colors[randi() % colors.size()]

Thank you so much.

THE HELIX | 2019-06-27 17:32

How can I make the viewport as a barrier to hold the blocks for a game like candy crush?

THE HELIX | 2019-07-05 18:10

Not sure what you mean. Do you mean you want moving objects to stop at the edge of the screen? You can put static colliders there, or you can clamp the coordinates to the coords of the viewport, as in the tutorial game.

kidscancode | 2019-07-06 17:04

I made the viewport as a barrier / a wall to hold the blocks for a game like candy crush but in the game the blocks are falling from above similar to a tetris game .I used kinematicbody 2D for the blocks and attached the code like this

  var extents
  var screensize
  var pos
  var velocity = Vector2()

  func _ready():
         screensize = get_viewport_rect().size
         extents = $Sprite.get_texture().get_size() / 8
         pos = screensize / 8      

  func _physics_process(delta):
         velocity.y += gravity * delta
         position.y += velocity.y * delta
         move_and_collide(velocity * delta)
         pos += velocity * delta
         if pos.x >= screensize.x - extents.x:
                pos.x = screensize.x - extents.x
                velocity.x *= 0
         if pos.x <= extents.x:
                pos.x = extents.x
                velocity.x *= 0
         if pos.y >= screensize.y - extents.y:
                pos.y = screensize.y - extents.y
                velocity.y *= 0
         if pos.y <= extents.y:
                pos.y = extents.y
                velocity.y *= 0

When I execute it the viewport acts as a wall/barrier.But during spawning the blocks are held by the viewport wall.When the lower blocks are cleared the upper blocks do not come in the place of the cleared blocks but stay where they where and comes down only when more blocks fall on them.How can I make the blocks to fall freely without staying where they where when the blocks below them are cleared ?

THE HELIX | 2019-07-07 05:11

I tried as in the tutorial game.The viewport acts as barrier but the blocks(Kinematicbody 2D) overlap on each other when more blocks fall on them which already have a collisionshape 2D attached to it.

THE HELIX | 2019-07-07 05:17

I also tried static colliders at the end of screen .They hold the blocks for some time when more blocks fall on them the static colliders let the blocks fall through them.

THE HELIX | 2019-07-09 05:33

Can you please solve this problem ?

THE HELIX | 2019-07-09 18:11