This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hello eveyone
I am trying to replicate a youtube tutorial but i'm stuck.Can you help me?
The tutorial is this https://www.youtube.com/watch?v=SCnnCl47dVM&t=3s

My project as a .rar file
https://drive.google.com/file/d/1rJJJ3eHg0FNjuOrpJ6wKLEJF-plApCWJ/view?usp=sharing

Basically i stuck in a line that have .z property but godot doesn't get what taht is. I think it is z_index and i tried to correct that but no success.

Game works at least functionality wise but procedural drawing shows nothing.
It is hard to explain if you watch tutorial and check my project you would understand.
Thanks for any help.

in Engine by (45 points)

I wouldn't expect people to blindly watch a complete tutorial and sift through your project with no indication of what/where the problem is. If the problematic code is referenced in the video, please at least provide a timestamp where that can be viewed. Also, where in your code is the problem?

In short, you're more likely to get assistance if you provide enough information to make it easy for people to help - don't make them guess...

2 Answers

0 votes
extends Node2D

var newMoto = load("res://SCENES/Moto.tscn")
var moto; var width = 1024; var height = 600
var lines = []; var col; var grass; var road; var border; var divid_line
var pos = 0; var num; var skyline; var skln_pos = 0; var skln_h = 0
export var road_width = 2000
export var seg = 200
export var cam = 0.8

func _ready():
    moto = newMoto.instance()
    add_child(moto)
    moto.set_position(Vector2(width/2+50, height-100))
    moto.set_scale(Vector2(2, 2))
    skyline = $Sprite
    skyline.set_region(true)
    skyline.set_z_index(-1)
    for i in range(1600):
        lines.push_back({x = 0, y = 0, z = 0, X = 0, Y = 0, W = 0, scale = 0, curve = 0 })
        lines[i].z = i*seg
        if (i>300 && i<700):
            lines[i].curve = 0.5
        if (i>800 && i<1200):
            lines[i].curve = -0.7
        if (i>750):
            lines[i].y = sin(i/30.0 - 25)*1500
        if (i>1200):
            lines[i].curve = 0.2
    num = lines.size()
    set_process(true)


func line(Line,cam_x,cam_y,cam_z):
    Line.scale = cam / (Line.z - cam.z)
    Line.X = (1 + Line.scale*(Line.x - cam_x)) * width/2
    Line.Y = (1 + Line.scale*(Line.y - cam_y)) * height/2
    Line.W = Line.scale * road_width * (width/2)
    return Line

func drawRoad(col,x1,y1,w1,x2,y2,w2):
    var point = [Vector2(int(x1-w1), int(y1)), Vector2(int(x2-w2), int(y2)),
    Vector2(int(x2+w2), int(y2)), Vector2(int(x1+w1), int(y1))]
    draw_primitive(PoolVector2Array(point), PoolColorArray([col,col,col,col,col]), PoolVector2Array([]))

func _draw():
    pos += 200
    if pos == 320000:
        pos = 0
    while (pos >= num*seg):
        pos -=num*seg
    while (pos < 0):
        pos += num*seg
    var num_pos = 0
    var start_point = pos/seg
    var cam_h = 1500 + lines[start_point].y
    var cutoff = height
    var x = 0
    var dx = 0
    skln_pos +=lines[start_point].curve * 2.0
    skln_h = -lines[start_point].y * 0.005
    skyline.set_region_rect(Rect2(skln_pos, skln_h, 1920, 320))
    var dir = lines[start_point].curve
    if dir >= 0.5:
        moto.rule = 3
#       moto.img.set_hidden(false)
        moto.img.show()
    if dir < 0.5 and dir > -0.5:
        moto.rule = 2
#       moto.img.set_hidden(false)
        moto.img.show()
    if dir <= - 0.5:
        moto.rule = 1
#       moto.img.set_hidden(false)
        moto.img.show()
    for n in range (start_point, start_point + 300):
        if n >= num:
            num_pos = num * seg
        else:
            num_pos = 0
        var l = line(lines[fmod(n,num)], -x, cam_h, pos - num_pos)
        var p = lines[fmod(n-1, num)]
        x += dx
        dx += l.curve
        if l.Y >= cutoff:
            continue
        cutoff = l.Y
        if fmod((n/3),2):
            border = Color(1,1,1)
            road = Color(.42,.42,.42)
        else:
            border = Color(0,0,0)
            road = Color(.4,.4,.4)
        if fmod((n/9),2):
            divid_line = Color(0,0,0)
            grass = Color(.2,.2,.2)
        else:
            divid_line = Color(1,1,1)
            grass = Color(.8,.8,.8)
        drawRoad(grass, 0, p.Y, width, 0, l.Y, width)
        drawRoad(border, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2)
        drawRoad(road, p.X, p.Y, p.W, l.X, l.Y, l.W)
        drawRoad(divid_line, p.X, p.Y, p.W*0.01, l.X, l.Y, l.W*0.01)

func _process(delta):
    update()
by (45 points)
0 votes

Line.scale = cam / (Line.z - cam.z)
says invalid get index 'z'

by (45 points)

I'd assume that's a reference to the cam.z (not Line.z) and should be changed to cam_z (based on the arguments being passed to that function)...

And, use the "Comment" function of the forum, rather than posting additional content as an "Answer"...

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.