#Identifier "input" not declared in the current scope

105 messages · Page 1 of 1 (latest)

forest niche
#

if you put in input (lower case) then godot looks for a variable called like that which you never defined hence the error "not declared in current scope"

north geode
#

raahhh oops >_<

#

oky thank u so much

forest niche
#

@north geode maybe we can continue here so shorter questions have a bit more visibility in the #godot channel. i´ll answer soon!

north geode
#

heres all the timer stuff

#

deleting the timer and starting over with that

#

still think the timer is the right idea i just think i took it in the wrong direction

#

we are now here

#

this line at the bottom but no timer to make it work

#

goal is to make a 10 second timer that will set this off, struggling to figure that out.

forest niche
#

Still semi distracted, but regarding the timer:
You dont need the script for it alone to start it

#

just reference it in the characterbody script + you had a typo since it is casesensitive

#

You can drag in the timer node in the editor at the top and before dropping it hold "ctrl" then this line will be created for you:

#

now you can use the timer inside the script

north geode
forest niche
north geode
#

ohhhh\

forest niche
#

drag it, and when its in the editor before dropping hold ctrl

north geode
#

okyoky

forest niche
#

Do you know what signals are?

north geode
#

and is that enough for it to work?

north geode
#

its how nodes communciate to nodes in other scenes right?

forest niche
#

The timer has a timeout signal. When it happens you can connect a function to it that runs (that´s what you wanted to do with _on_timer_timeout() i think). Did you connect that funciton yet?
And yes you can use signals to communicate to other scenes/nodes.

#

But mostly it´s to react to some event with a callback

north geode
#

how do i connect a function

#

and yeh i have that at the bottom

forest niche
#

Inside characterbody2d:


func _ready() -> void:
  timer.timeout.connect(on_timer_timeout)
north geode
#

where would that slot in exactly

#

i put it under @onready var timer: Timer = $Timer

#

@onready var timer: Timer = $Timer

forest niche
#

The _ready() function gets called by a node in the beginning when it gets added to the scene tree like some kind of setup.
Generelly it doesnt matter in which order you put the function definitions in your code.

north geode
#

oky

#

timers still not starting as far as i can tell

#

should it be?

#

or am i still missing something

forest niche
#

if it is not set as autostart in the editor then you ahve to manually start it

#

timer.start() inside the ready function for example

north geode
#

yeh its set to autostart

#

still not drifting 😔

forest niche
#

what does the script look like atm?

north geode
#

i have run out of time for now, ill post script real quick

#

thank you for all your help but i must go for now!!

forest niche
#

i´ll have a look and write smth about it in a bit^^
glad i could help!

forest niche
#

Since velocity is the thing that makes you move in a CharacterBody2D and you set it based on current_speed you have to move it below

#

Right now the timeout only executes randf() which does nothing

#

With the current code you wont see any drifting though. You will very slowly accelerate towards a speed in a random direction for one frame if current_speed is 0. In the next frame you'll decelerate towards 0 again first and then comes the if statement again. So in the end you will never be able to get to speed.

forest niche
#

Im not sure though how the logic of the game is supposed to work exactly though. I guess you want the drifting to start, if there is no player input at all. If you dont touch the steering wheel you start the drifting then, right?
Drifing causes the current_speed to rise so it will be different from zero and we shouldnt check for that.
Instead we should check for direction == 0.0 instead.

The question is also if you want to accelerate towards the speed (with move_toward) or just set the speed instantly in this game (when drifting or controlling it yourself). Maybe you need a different acceleration for the drifting then.

For example if you start drifting and you want to counter steer, then maybe you want to not "slowly" reduce movement in the wrong direction -> come to a stop -> accelerate towards the max speed in the wanted direction, but more like have it more responsive. I guess you could change that with the accel value and see for yourself how it feels.

#

About picking a random direction

I forgot about that but the solution is so obvious to random numbers that you can chose from:
The Array class has a .pick_random() method. So you can pick a random element of it. [-1,1].pick_random() will pick either -1 or 1
In your case for the drift_direction you could just write:

if direction == 0.0:
  drift_direction = [-1,1].pick_random()
  current_speed = move_toward(current_speed ,drift_direction *drift_max_speed,accel*delta)
#

Still this direction change shouldn't be called each frame but more like in the timeout once. Or else you will frantically accelerate towards a different direction every 1/60 of a second. So you'd need a smarter check if you are driving yourself, or drifting. (Which smells a bit like statemachine).

  • You want to move at /accelerate towards a certain speed when you give player input and you don't want the timer to run -> "driving"
  • You want to stop moving, but start a timer as soon as stop giving input -> "idle"
  • You want to start drifting after a certain timeout
    • give a random direction and other things in the time out -> im now "drifting"
    • stop the timer (maybe you want to keep drifting when you started drifting and not randomly drift in other directions after another timeout)
    • now run the movement code with the drifting values each frame

In the end you´ll run

current_speed = move_toward(current_speed ,some_speed*some_direction,some_acceleration*delta)
velocity = Vector2(current_speed,0)
move_and_slide()

to move each frame.
What values

  • some_speed
  • some_direction
  • some_acceleration
    will be depends on the "state" you're in.
forest niche
#

I´m gonna write an implementation that could work, but maybe you wana try getting to that solution yourself, so im gonna mark it as spoiler:
||```swift
enum States {IDLE,DRIVING,DRIFTING}
var state:States = States.IDLE

func _ready() -> void:
timer.timeout.connect(on_timer_timeout)
timer.start()

func _process(delta: float) -> void:
var direction = Input.get_axis("ui_left", "ui_right")

if direction == 0.0 and state == States.DRIVING:
    state = States.IDLE
    timer.start()
if not direction == 0.0:
    state = States.DRIVING
    timer.stop()

match state: # // you can also just use if.. elif.. elif instead which does the same thing
    States.IDLE:
        current_speed = move_toward(current_speed ,0.0,accel*delta)    
    States.DRIVING:
        current_speed = move_toward(current_speed ,direction *max_speed,accel*delta)
    States.DRIFTING:
        current_speed = move_toward(current_speed ,drift_direction *drift_max_speed,accel*delta)

velocity = Vector2(current_speed,0)
move_and_slide()

func on_timer_timeout() -> void:
state = States.DRIFTING
drift_direction = [-1,1].pick_random()
timer.stop()

#

Alternative with bools instead of enums:
||```swift
var is_driving:bool
var is_drifting:bool

func _ready() -> void:
timer.timeout.connect(on_timer_timeout)
timer.start()

func _process(delta: float) -> void:
var direction = Input.get_axis("ui_left", "ui_right")

if direction == 0.0 and is_driving:
    is_driving = false
    timer.start()
if not direction == 0.0:
    is_drifting = false
    is_driving = true
    timer.stop()

if not is_drifting:    
    current_speed = move_toward(current_speed ,direction *max_speed,accel*delta)
else:
    current_speed = move_toward(current_speed ,drift_direction *drift_max_speed,accel*delta)    
velocity = Vector2(current_speed,0)
move_and_slide()

func on_timer_timeout() -> void:
is_drifting = true
drift_direction = [-1,1].pick_random()
timer.stop()

north geode
#

oh my god you are too kind 😭 i really appreciate all this!! trying to implement now

#

so i dont want the timer to start when the player lets go of input, i was thinking it would just constantly be going in the background to determine what way the vehicle drifts. Is there anyway I could make it so that it just chooses a random direct each time the player lets go of input? im trying to see if i can adapt this code to accomplish that

#

i either didnt know or forgot how states work so thats very helpful, thank u

#

lemme try something

#

i guess the issue im running into is how would i make it only happen once

#

cause when direction == 0 it randomly switches but that means its doing that every frame...

#

also the idea is that the vehicle immediately starts drifting after you let go of controls so i dont think we need the idle state

#

lemme see if i can figure this out

#

i think i may have an idea

#

OH MY GOD I DID IT

#

SNIFFI I FIGURED SOMETHING OUT ON MY OWN 😭

#

WAHOOOOOOOOOOOOOOOOOOO

#

well obviously not on my own my own, i very much adapted a bunch of the things you posted but i used them in a new way i think

#

`extends CharacterBody2D

@export var max_speed: float = 100
@export var accel: float = 100
@export var drift_max_speed: float = 20
@export var drift_accel: float = 20

var drift_direction = 0

var current_speed :float = 0.0

enum States {DRIVING,DRIFTING}
var state:States = States.DRIVING

func _physics_process(delta:float) -> void:
var direction = Input.get_axis("SteerLeft", "SteerRight")

if direction == 0.0 and state == States.DRIVING:
    state = States.DRIFTING

if state == States.DRIFTING:
    drift_direction = [-1,1].pick_random()
    current_speed = move_toward(current_speed ,drift_direction *drift_max_speed,accel*delta)


if not direction == 0.0:
    state = States.DRIVING
    print(direction)
    current_speed = move_toward(current_speed,direction *max_speed,accel*delta)


velocity = Vector2(current_speed,0)
move_and_slide()

`

#

this is what i ended up with

#

AND IT WORKS!!!

#

im still getting it switching back and forth but i believe i have a solution

north geode
#

oky lets see if i can fix the issues i have created for myself, by myself

#

AAAAAAAAAAAAAAAAAA I GOT IT

forest niche
#

im glad youre trying stuff and having success^^! getting inspiration from other code and modifying it to your needs is a good way to learn!
for now im just watrching your thought process lol

north geode
#

`extends CharacterBody2D

@export var max_speed: float = 100
@export var accel: float = 100
@export var drift_max_speed: float = 20
@export var drift_accel: float = 20

var drift_direction = 0

var current_speed :float = 0.0

enum States {DRIVING,DRIFTING,START_DRIFTING}
var state:States = States.DRIVING

func _physics_process(delta:float) -> void:
var direction = Input.get_axis("SteerLeft", "SteerRight")

if direction == 0.0 and state == States.DRIVING:
    state = States.START_DRIFTING

if state == States.START_DRIFTING:
    drift_direction = [-1,1].pick_random()
    state = States.DRIFTING

if state == States.DRIFTING:
    print(drift_direction)
    current_speed = move_toward(current_speed ,drift_direction *drift_max_speed,accel*delta)

if not direction == 0.0:
    state = States.DRIVING
    print(direction)
    current_speed = move_toward(current_speed,direction *max_speed,accel*delta)


velocity = Vector2(current_speed,0)
move_and_slide()

`

#

bam, literally got it working EXACTLY how i want it to. made a state for start_drifting so that it only triggers once per idle

#

oh em gee im so hyped

forest niche
#

btw if you can write codeblocks like this (swift is closest to gdscript)

func blabla() -> void:
north geode
#
func blabla() -> void:
#

ohhhhhh thats how you do that thank u

#

omg im like so pumped that it works

#

im actually really glad i changed my mind on how i wanted it to work because that forced me to actually have to try and figure some stuff out myself and i am SO PUMPED that i did

#

obviously that "figure it out myself" is with a huge asterisk haha, could not have figured it out without all your help and the code you posted, the stuff i figured out stood on the foundation of that code, thank you so much!!!

forest niche
#

no problem! as i said it is a totally valid way to learn things^^!

north geode
#

oh last question about this code. Im pretty sure i understand everything in here except for this. I get that its declaring state as a variable and setting the state to DRIVING but what is ":States" for and why is it there. Why cant it just be "var state = States.DRIVING"

#

i know theres def a reason, i just wanna know why

forest niche
#

you could do that without it, but doing this gives you autocompletion in the editor, because then it knows what to expect

#

everytime i forget there is a 10 seconds cooldown here 😅

north geode
#

oohhh oky

forest niche
#

in general if you define any class in any script and type your variable with it you will get autocompletion

north geode
#

okyoky

#

anyway, again, thank you so much!!! im gonna keep working my way down my checklist of features i wanna add to this, the rest of the items i think will be way easier so hopefully i can figure those out on my own with some youtube videos but i am positive i will be back here eventually in confusion 💀

#

based on the fact youre clearly fluent in godot, do you have any games or projects of yours out in the world? id love to check them out!

forest niche
#

glad i could help you and i wish you alot of fun and good luck figuring things out!

forest niche
# north geode based on the fact youre clearly fluent in godot, do you have any games or projec...

Ah thanks if you think that^^ Id still say im relatively "new" (1 year) and following the paid course here, but trying to learn as much as i can^^ In term of projects i have nothing big yet. I did alot of small projects where i just implement certain game mechanics, tools or minigames but nothing really to play to be published yet. Whenever i see something in some games i play i wonder how someone would implement it and then i give it a go myself :P So i have some prototype but not really a fleshed out game.
That will change soon though hopefully! I plan to work on some "bigger" projects now and put them on itch.io.
Atleast the one idle/basebuilding game i try to make all the time is something on my list!

#

Whenever it´s ready i can keep you up to date or i post it in the #showcase-games channel!

north geode
#

it gives me so much hope that youve only been using godot for a year because youre skills are like incomprehensible to me at the moment haha