#Trying to make a grid based Dice game

66 messages · Page 1 of 1 (latest)

karmic star
#

Trying to make a grid-based game with dice spawning in random positions and orientations. Here's what I've got so far:

DiceSpawner in main scene:

extends Node3D
#This script will be universal in places where dice can spawn. It will first create a random timer
#and count to it with _process then generate a dice when it meets that timer

#There's probably a better way to do it but for now since it's a square map I'll
#generate coords by distance then direction with dice_where. I'll at least make
#a variable so I can change the dimensions simply for testing.
var mapsize = 6 #Mapsize is maximum distance from 0, not total width
var dice_position = Vector2(0,0) #This creates the variables we'll use to generate the dice later
var dice_orientation = Vector3(0,0,0)

func spawn_dice():
    load("res://dice basic.tscn").instanciate()

func dice_where(): #this function is going to generate a dice at random coordinates
    var dis_x = randi() % mapsize #Here we generate how far from center we spawn it
    var dis_y = randi() % mapsize
    var dir_x = randi() % 1 #This decides if we go that far positive or negative
    var dir_y = randi() % 1
    if dir_x == 1: #If dir is 1, we invert the corresponding variable
        dis_x * -1
    if dir_y == 1:
        dis_y * -1
    dice_position = Vector2(dis_x, dis_y) #Dice should spawn at this location

func dice_what(): #This function chooses the orientation of the dice as it spawns
    var top = randi() % 5 #This decides the top number
    var north = randi() % 3 #This rotates the rest of the numbers on the dice
    var top_dice = [Vector3(0, north, 0), Vector3(-90, north, 0), Vector3(0, north, 90), Vector3(0, north, -90), Vector3(90, north, 0), Vector3(180, north, 0)]
    north *= 90
    dice_orientation = top_dice[top] #Each Vector3 in top_dice should be a different number on top, ordered numerically.



# Called when the node enters the scene tree for the first time.
func _ready():
    preload("res://dice basic.tscn")


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
    #We want to spawn dice every 8 to 15 seconds.
    var last_dice_spawn = 0
    #spawn_dice_in generates a timer to spawn the dice. Should loop when it spawns a dice.
    var spawn_dice_in = randi() % 420; spawn_dice_in += 480
    while last_dice_spawn <= spawn_dice_in:
        last_dice_spawn += 1
    if last_dice_spawn >= spawn_dice_in:
        dice_where(); dice_what()
        last_dice_spawn = 0; spawn_dice()

And Dice scene:

extends Node3D


# Called when the node enters the scene tree for the first time.
func _ready():
    position.y = 0.5 #Offsets dice from ground, setting it as base makes rotating weird
    position = dice_position
    rotation = dice_orientation


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    pass
dark gull
#

Okay, so what variable were you asking about how to access?

karmic star
#

I want the Dice _ready to be able to pull dice_position and dice_orientation from the SpawnDice script

#

The idea is DiceSpawner will generate the position and rotation, and then instanciate a Dice scene with those parameters

#

it's late but i eventually wanna have the spawner check if a dice is already there, too

dark gull
#

I see. I'd probably do something like this:
DiceSpawner

const Dice := preload("res://dice basic.tscn")
func spawn_dice():
  var new_die := Dice.instantiate()
  new_die.position = dice_position
  new_die.rotation = dice_orientation
  node_where_the_die_goes.add_child(new_die)

Dice

func _ready():
  position.y += 0.5
karmic star
#

oh!!

karmic star
dark gull
#

or if you end up needing the die to do more complex stuff when it's made you can do

func spawn_dice():
  var new_die := Dice.instantiate()
  some_node.add_child(new_die)
  new_die.initialize(some_parameters)

Dice

func initialize(some_parameters):
  # other stuff here
dark gull
# karmic star would this go under spawn_dice()?

yeah, I rewrote the spawn_dice function. The line at the top loads the scene file and stores it in the Dice variable so you don't have to use load() again to reference it, that line doesn't need to be in a function

#

I guess I wouldn't name the const Dice if that's already the name of the other script though

karmic star
#

Weird, putting it in gave me this error

dark gull
#

oh that means there's an empty function above it

#

i always get thrown off by where they put the error line

karmic star
#

Oh! Pass in _ready, right

#

How do I define node in .add_child?

dark gull
#

That depends on where you want the dice to be in the scene tree. Whatever node you call add_child on is going to be its parent

karmic star
dark gull
#

What is the DiceSpawner script attached to?

karmic star
#

Map

dark gull
#

oh I see it

karmic star
#

I also tried Map and DiceSpawner itself, same error

dark gull
#

there are a couple ways you can get a reference to GameWorld. If it's the root of the scene (and it looks like it is) then you can reference it with owner in DiceSpawner

karmic star
#

How do I do that?

#

Agh, sorry if I'm asking so much

dark gull
#

like owner.add_child(new_die) should work, I think

#

(I haven't actually tried it that way before, I only learned about it recently myself)

karmic star
#

ah! It worked!

dark gull
#

nice!

karmic star
#

oh no it hanged,

dark gull
#

oh yeah position is a Vector3 for 3D nodes

karmic star
#

Oh!

#

Running it again after changing that

#

Good news! It's generating random dice! Kind of.

dark gull
#

I think that 0.5 offset you do in Dice._ready() might need to be in the z-coordinate instead of y? looks like a lot of those are stuck in the ground

karmic star
#

They have no physics so they just generate like that

#

Yeah, they're just spawning in random orientations every frame

#

And only in that corner

dark gull
#

I haven't looked at the randomization math itself, bit out of my depth there

#

one thing I can say is that _process() is called by the engine every frame, so when you're doing your while loop you're not actually slowing down the spawn rate

#

so instead you should do something like

var time_since_last_spawn = 0
var spawn_delay = 8
func _process(delta):
  if time_since_last_spawn >= spawn_delay:
    time_since_last_spawn = 0
    # Spawn dice here
  else:
    time_since_last_spawn += delta
karmic star
#

Time's way better now

#

Now I uh, just gotta fix the spawning:

dark gull
#

Good luck! I've never worked with 3D physics for, uh, exactly that kind of reason lol

karmic star
#

They're actually all static, it's rotation and position that are messing up

#

I haven't programmed movement into them yet

dark gull
#

You can put in a print statement and/or breakpoint when you spawn in a die to see its exact position and compare it to the map height and stuff

karmic star
#

PROGRESS

#

i did * instead of *= when randomizing direction

karmic star
dark gull
#

And that's new? interesting

karmic star
#

also for some reason inside each other?

#

that might be the rotation actually

#

i think it's rising by 0.5 y then pivoting on the root position

#

instead of by its new center

#

like a lever

#

i might have to just lower the board

#

it works!!!!!!!!!!!!

#

YESSS

#

AS INTENDED

#

now all i need to do is teach it to look for a dice there and reroll if there's a dice there already

#

and then i'm good on the spawner