#can anyone help me with my project? I'm having issues with the game, and I can't figure out what's w

1 messages · Page 1 of 1 (latest)

stuck scarab
#

First I recommend reviewing or code structure to avoid those many if statements. Second, store the reference for your nodes in variables instead of using get node every time you need them.

#

That said, what issues are you having?

#

But refactoring the code will make it much easier to debug and later on scale your game.

strong bridge
#

The images you posted don't really include any indication of what the problem is. Let's start there, what do you need help with?

mild sapphire
#

right now, the issues I'm facing are mostly to do with the biscoff

#

the knife is meant to spread spreads on the toast, and when you right-click, it should swap the spreads of the knife and the toast

#

but instead, when I right click the butter on the toast with the knife with nutella, it spreads biscoff on the toast and keep the nutella on the knife

#

also, when I'm holding a biscoff knife, I right-click the toast with butter and it picks up the butter on the knfie as if the knife didn't already have biscoff.

strong bridge
#

So, there's a few potential issues I can see. If you can only have one spread on the knife at a time (which I assume is the case) why are you using multiple booleans to represent it?.

mild sapphire
#

I don't know what else

#

if I change it now, wouldn't I have to redo the entire code?

strong bridge
#

You could just use an enum:

var spread: spread_type = spread_type.BUTTER

match spread:
    spread_type.BUTTER:
        # do stuff with butter only
    spread_tupe.BISCOFF:
        # do stuff with biscoff only
    spread_type.NUTELLA:
        # do stuff with nutella only```
mild sapphire
#

what's an enum?

strong bridge
#

Short for enumerator. It can only hold one value. Internally, it's just a number from 1-x depending on how many values you need.

#

But it allows you to name them individually for easier reading later.

#

This way it's also expandable. Add a new ENUM and match case for a new spread.

mild sapphire
#

this is confusing. does it had a new type of variable?

strong bridge
#

It's an int for all intents and purposes, but it allows you to name the numbers for easier reading:
Eg.
1 = Nutella
2 = Biscoff
3 = Butter

... and so on

stuck scarab
#

Having a Resource to hold your spread data would make it even more scalable as you would not need any if or match statements.

mild sapphire
#

ohh. I gues the makes sense, but then what is this? var spread: spread_type = spread_type.BUTTER

strong bridge
#

It was an example for simplicity

mild sapphire
strong bridge
#

Var defines a new variable. Spread is the variable name, spread_type is the type (we defined the enum earlier) and spread_type.BUTTER is the specific value we want.

mild sapphire
#

k

#

what's match spread? it doesn't have a class body or whatever it's called

strong bridge
#

randi() just returns a random number ENUM's are fixed.

stuck scarab
mild sapphire
#

doesn't randi() start with 0 too?

mild sapphire
strong bridge
#

Match is a key word. It checks the variable to see if it matches one from the list below it.

mild sapphire
#

and it doesn't need to be in a func?

strong bridge
#

here's another example.


match myString:
    "Hello":
        print("Hello to you too.")
    "Goodbye":
        print("Goodbye. Hope to see you again.")```
#

Yes, they would normally be in functions. They're just use-case examples.

mild sapphire
#

k

#

would I put it in 'func _process(delta):'

mild sapphire
strong bridge
#

Three reverse apostrophe's at the start and end of your code block = `

mild sapphire
#

k

mild sapphire
strong bridge
#

If you need to ask, you don't understand the purpose of the code. The key here is understanding what it does and how best to use it. I don't have access to your code, so there may be a few places it's needed.

#

Anywhere you have run-on if-else statements, consider whether match might be a better option.

mild sapphire
#

but should I put it in the process function so it matches every frame, so there is never delay?

stuck scarab
#

you don't have to do it in process, update just when the value change. You can use a setter for that.

example of setter:

var my_var:String:
    set(value):
        my_var = value
        //anything else you might need to do when the value change
mild sapphire
#

thank you!

mild sapphire
#

for example, what would I do here?

stuck scarab
#

my_var is just any var name you want.

#

And String you change to the type of the var

#

If you want to have a setter for spread you add it after that var directly. Also, I recommend using TitleCase for class names, will make your code easier to read, separating them from var names.
Same for enums, and the values full UPPERCASE, as they are constants.

enum SpreadType {EMPTY, NUTTELA, BISCOFF,  BUTTER}
var spread := SpreadType.EMPTY:
   set(value):
      spread = value
      //anything else you want to do
mild sapphire
#

also where do I add the match spread:

stuck scarab
#

inside the setter

mild sapphire
#

k

stuck scarab
#

the parameter?

mild sapphire
#

the 2nd

stuck scarab
#

none

#

keep like that

mild sapphire
#

k

stuck scarab
#

it will set spread to the value when you set the variable

#

if you change your mind about refactoring your logic to be data driven instead of using match or if statements let me know.

mild sapphire
#

Also, how do I fix this?

stuck scarab
#

Look at the code example I sent

#

You put your while code inside the parenthesis of the set function

#

I even put a comment where you should add your code

mild sapphire
#

like this?

stuck scarab
#

yeah, but the spread = value should be inside the set function as well

#

so move it one indentation

#

and either move spread = value to be above the match statement

#

or use value in the match statement

#

because right now you are testing it before you actually change it

mild sapphire
mild sapphire
stuck scarab
#

Yeah, that should work

mild sapphire
stuck scarab
#

You just have to set spread to the value you want

#

spread = SpreadType…

#

You are already setting it to empty by default

mild sapphire
mild sapphire
stuck scarab
#

If you don’t set to nothing it will default to first value, but you are setting it

#

Read the code

#

Where you declare the variable

mild sapphire
#

ohhh

#

k tahnks bro

#

you're a legend

#

I'll get backt o you tmr to see if it works