Learning GDScript
#4
I'll be honest, I don't really understand the term 'scope' in this setting. I am close to an absolute beginner (prior knowledge only being basic JS and decent html5).

Part 3!
Today's challenge was to create an object that, upon contact, "upgrades" your stats, for now, just the speed stat. You can connect a function in one script to a function in another, in Godot it's done with a 'signal'. You emit it when the two objects overlap and then tell the script to 'observe' it.
The game engine allows for the first part of this to be done automatically.
On the receiving end, you use this code:
Code:
func _on_SpeedUpgrade_body_entered(body):
        GlobalPlayer.speed_lvl += 1
        cur_speed_lvl = GlobalPlayer.speed_lvl
        max_speed = GlobalPlayer.stats.speed[cur_speed_lvl]

This updates the player's level on the GlobalPlayer script which holds the statistics dictionary, updates the current script's speed level so it can find that data in the GlobalPlayer script then updates max_speed accordingly. Max speed is used in another script to define the ships maximum speed like so:
Code:
func _physics_process(delta):
    #find out where the mouse is and create the vector from the ship towards it
    dir = (get_global_mouse_position() - position).normalized()    

    #define how the ship moves, max speed and also interpolate its slowdown speed based on the friction variable "fric".
    velocity.x = clamp(velocity.x, -max_speed, max_speed)
    velocity.y = clamp(velocity.y, -max_speed, max_speed)
    move_and_slide(velocity)

    velocity.x = lerp(velocity.x, 0, fric)
    velocity.y = lerp(velocity.y, 0, fric)
    
    #check for input and move if the up button is pressed (defined in my keymap as "w").
    if Input.is_action_pressed("ui_up"):
        velocity += dir * accel
Reply }


Messages In This Thread
Learning GDScript - by MetalRenard - 02-09-2021, 08:55 AM
RE: Learning GDScript - by MetalRenard - 02-09-2021, 10:45 AM
RE: Learning GDScript - by kyonides - 02-10-2021, 05:55 AM
RE: Learning GDScript - by MetalRenard - 02-10-2021, 04:16 PM
RE: Learning GDScript - by MetalRenard - 02-14-2021, 04:24 PM
RE: Learning GDScript - by MetalRenard - 02-19-2021, 08:25 AM
RE: Learning GDScript - by MetalRenard - 02-23-2021, 10:40 AM
RE: Learning GDScript - by MetalRenard - 03-03-2021, 12:54 PM



Users browsing this thread: 1 Guest(s)