#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)
That said, what issues are you having?
But refactoring the code will make it much easier to debug and later on scale your game.
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?
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.
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?.
I don't know what else
if I change it now, wouldn't I have to redo the entire code?
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```
what's an enum?
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.
this is confusing. does it had a new type of variable?
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
Having a Resource to hold your spread data would make it even more scalable as you would not need any if or match statements.
(Starts with 0 though)
ohh. I gues the makes sense, but then what is this? var spread: spread_type = spread_type.BUTTER
It was an example for simplicity
like randi()?
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.
randi() just returns a random number ENUM's are fixed.
I am not sure which connection you are making here. I was just pointing that enums starts from 0.
doesn't randi() start with 0 too?
anyways back to the point
Match is a key word. It checks the variable to see if it matches one from the list below it.
and it doesn't need to be in a func?
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.
also how do you put it in code like that?
Three reverse apostrophe's at the start and end of your code block = `
k
?
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.
but should I put it in the process function so it matches every frame, so there is never delay?
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
thank you!
where does the match go?
for example, what would I do here?
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
should I make value equal to something like NUTELLA, or should it be equal to something like SpreadType.NUTELLA, or should it be an integer?
also where do I add the match spread:
inside the setter
k
wdym? Which value?
the parameter?
the 2nd
k
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.
what does that mean?
Also, how do I fix this?
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
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
good?
I chose this option
Yeah, that should work
how do I change the value of it so the match picks the right one, and also which one is selected by default?
You just have to set spread to the value you want
spread = SpreadType…
You are already setting it to empty by default
spread=SpreadType.BUTTER for example?
Is it because EMPTY is the first one I wrote, and enums start at 0?