#ACCESSING FUNCTION FROM ANOTHER SCRIPT(GODOT)

86 messages · Page 1 of 1 (latest)

timid marten
#

Is there a way to trigger the change_difficulty(a function from another script)? with my difficulty button?

#

This is what I tried

rigid phoenix
#

Signals are the proper way, if you want to call it directly you need reference to the player NODE. Currently you're trying to get the node from the FileSystem, this isn't going to work. You will need to get the node from the SCENE.

timid marten
#

Can you help me? The tutorial about signals you sent earlier I find it not relatable to my problem as he is working with signals in the same node

rigid phoenix
#

Send me a screenshot of your scene

timid marten
#

this is the one i need to access

#

and this is the button i will use to trigger the method

rigid phoenix
#

And does SelectDifficulty load as default?

timid marten
#

yes

#

error shows when i click any of the buttons

rigid phoenix
#

Yes because you're not getting the player node correctly, one second.

#

Based on how you have this setup, you could create the player as an Autoload. That way it's always loaded no matter how you change your level

timid marten
#

how can I do that?

#

my player is almost the main code for the game

rigid phoenix
#

Go Project > Project Settings > Autoload. Select the path to your player SCENE, set the Node Name to Player then click Add

#

Now your Player scene will be loaded before everything else and will retain for the length of the game

#

Should look like this

#

well along those lines anyway

timid marten
#

done

#

but

rigid phoenix
#

Now

timid marten
#

now I cant run the whole code

rigid phoenix
#

to call the function

timid marten
rigid phoenix
#

oh remove class_name

#

you set that with the autoload

#

class_name doesn't matter in this instance and Player SHOULD only be created once

timid marten
rigid phoenix
#

It's based off the Singleton programming pattern but it's not exactly true to it. So whilst you can create more than one at runtime, I HIGHLY ADVISE NOT TO.

timid marten
#

I have removed the class name

rigid phoenix
#

Cool so in your select_difficulty scripts

#

remove the get node functions and just call Player.change_difficulty()

timid marten
rigid phoenix
#

Can you show me your autoload please?

timid marten
#

hey can a signal cause a function to work?

#

I am thinking on creating three separate functions for easy medium and hard

rigid phoenix
timid marten
#

can you teach me to do that?

rigid phoenix
#

lets step back and look at autoloads first

#

can I see how you configured it in your settings please?

timid marten
#

because i thought it is hard to make a function work while giving a value too

timid marten
rigid phoenix
#

Autoload's will work, we just need to modify some code

timid marten
#

my player has the movements in its script and the damage it get

rigid phoenix
#

Why don't you zip your project and send it to me

#

I'll make some adjustments and we can go from there

timid marten
#

how to zip it?

rigid phoenix
#

Windows 10 or 11?

timid marten
#

11

rigid phoenix
#

Right click project folder, compress to zip

timid marten
rigid phoenix
#

alright, give me a bit. Need to download godot 3.5

timid marten
#

i usually play scene from FirstScene

#

and it will go to menu and when you press start it will go to the game

#

now I am adding that selectdifficulty after that menu

rigid phoenix
#

I see you're already using signals in the Events autoload script

timid marten
#

yeah I just followed a tutorial

rigid phoenix
#

gotcha

timid marten
#

you did it??

rigid phoenix
#

Still going 🙂

#

download took forever

#

with 3.5.1

#

Alright

#

done

#

I've cleaned up your folder structure a bit, it was hard to navigate.

#

Instead of making the player an autoload, I created a gamemanger script.

#

You can use this to store values which you can access anywhere

timid marten
#

u done it?

rigid phoenix
#

it's not best practice and you will most likely have issues further down the track, but I think you'll have other problems before you get to that.

timid marten
#

I am just creating a project to pass

rigid phoenix
timid marten
#

All i need is to create 3 levels

rigid phoenix
#

school assignment?

timid marten
#

yes

#

Damn dude you are good

#

are there any other changes here other than you moving the max_health to the autoload?

rigid phoenix
#

Just the restructure of files

#

Move them as you will but it's alot easier to manage

#

Also i'm not happy with that answer, but it will do...

timid marten
#

It will do

#

It is like the last requirement of my project

#

to have a difficulty system