Learning GDScript
#2
Part 2!
Goal: Be able to change the character's rotation speed (they look at the mouse).
So the above script runs in the background and is used to store data. Now I want to access this data from another script called 'Player'.
It took me a little while to work this out because it requires specific formatting to tell the engine where to look.
Here's the updated GlobalPlayer script.

Code:
extends Node


var hp_lvl
var shield_lvl
var shot_lvl
var speed_lvl
var rot_lvl


var stats = {
    "hull": [6, 12, 18, 24],
    "shield": [10, 20, 30, 40],
    "shot": [8, 16, 24, 32],
    "speed": [150, 200, 250, 300],
    "rotation": [0.05, 0.07, 0.9, 0.11],
}


I'm checking to see what the rotation speed of the player is in position 0 of the dictionary's "rotation" array.
Code:
var rot_speed = GlobalPlayer.stats.rotation[0]
The thing that took me ages was learning how to look at data stored in an array which is stored in a dictionary! Finally arrived to this conclusion, very happy.

Next step: Use the _lvl variables to store the player's level and use that to compare with the position in the dictionary.

To accomplish this, I added these varibles above the Player script and compared them to the GlobalPlayer variables.
Code:
var cur_rot_lvl = GlobalPlayer.rot_lvl
And then updated the formula to compare the player's level to the position in the array.
Code:
var rot_speed = GlobalPlayer.stats.rotation[cur_rot_lvl]

It works!
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)