The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hello I working at a sort pipe game for a game jam and i created a class for determine where to put a bunch of area2D nodes to collide with the pipe(in the game you ll have to drag the pipe and the area2D is like a magnet) but the problem is when i use an instance of the class in the tilemap script(if i use the class script it works). From what i seen it enters in methods but they dont change nothing. What I can do?

Grid_System.gd

class_name Grid_System
extends TileMap

#the tile size of the grid
var _tile_Size : Vector2

#the half of the tile size to center it 
var _half_Tile_Size : Vector2

#the grid size
var _grid_Size :Vector2

#where the grid start on the map
var _grid_Start:Vector2

#the offset needed to allign the grid with the background
var _offset : Vector2

#helps create the grid
var _grid : Array

#helps getting the position of the anterior created grid
var _positions : Array


#setter for the half/tile set
func set_Tile_Size(tile_Size : Vector2):
    print("1")
    _tile_Size = tile_Size
    _half_Tile_Size = tile_Size / 2

#setter for the grid size depending on the room
func set_Grid_Size(room : Vector2):
    _grid_Size = room

#setter for the grid starting position depending on the room
func set_Grid_Start(room : Vector2):
    _grid_Start = room

func set_Tile_Offset(offset : Vector2):
    _offset = offset

#creates the grid and save its positions in the positions array
func create_Grid():
    for x in range(_grid_Start.x + _grid_Size.x):
        _grid.append([])

        for y in range(_grid_Start.y + _grid_Size.y):
            if(y>=_grid_Start.y && x >= _grid_Start.x ):
                _grid[x].append(null)
                var grid_Pos = Vector2(x,y)
                _positions.append(grid_Pos)
                print(grid_Pos)

#creates a area2D for future collisions checks
func create_Collider():
    for pos in _positions:
        print("here")
        #creates a area2D node add pos it on center of the tile
        var area = Area2D.new()
        area.set_position(map_to_world(pos) +_offset - _half_Tile_Size) 
        add_child(area)
        print("made")

        #create a circlrhape2D to be added to the collision shape
        var shape = CircleShape2D.new()
        shape.set_radius(10)

        #create a collisionshape2D to be added to the area
        var collision = CollisionShape2D.new()
        collision.set_shape(shape)
        area.add_child(collision)

OxigenRoomGrid.gd

extends Grid_System

#create an object of type grid_system
var grid = Grid_System.new()

func _ready():

    #set up the tiles offset/size and grid start pos/size
    grid.set_Tile_Size(get_cell_size())
    grid.set_Grid_Size(Globals.grid_size["Oxigen_Room"])
    grid.set_Grid_Start(Globals.grid_Start_Pos["Oxigen_Room"])
    grid.set_Tile_Offset(Globals.tile_offset["Oxigen_Room"])

    #create grid and colliders
    grid.create_Grid()
    grid.create_Collider()

Globals.gd

extends Node

#grid offset to be in centre
var tile_offset : Dictionary = {
    "Oxigen_Room": Vector2(-9,3)
    } 

#grid starting position
var grid_Start_Pos :Dictionary = {
    "Oxigen_Room" : Vector2(12,4)
}

#grid starting position
var grid_size :Dictionary = {
    "Oxigen_Room" : Vector2(6,5)
}
Godot version 3.4 stable
in Engine by (54 points)

1 Answer

+1 vote
Best answer

WHat do you mean "they don't change nothing" ?
What happens ? Is anything created, is this "here" printed there ??
You must provide some more informations.

But without those informations I can see that You never set "positions" variable, that determine your colliders creation. If it is empty, obviously nothing is created :)

by (8,188 points)
selected by

Yes it prints here but everything else is skipped it the debugger the setter doesn't change anything and at the point of calling create grid an collider everything is 0. Referring to positions idk what you mean in the create grid method i save in positions every position that I need to save. I mean for testing purposes I put the ready() in the Grid_System.gd and attach it to the tilemap and it works

What is 0, what values ? It prints "here", does it print "made" ? Did You check remote tab, if any objects were created ?

Or could it be, that You only call those from script and don't have GridSystem in scenetree? You realize ready() is called after node is childed, not after it is created with new() ?

Ok I just added as i child thank you it's silly now that I didn't thinked of that.

Haha it is OK, I am glad it occured to not be so complicated :)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.