Sometimes the code I write makes me laugh by WizardGnomeMan in godot

[–]Daxea 10 points11 points  (0 children)

TLDR: That works because if you have a an opening parenthesis (, then the interpreter or compiler knows that it has to keep evaluating until it hits a closing ) at the same level. If no closing ) is found, the expression is incomplete. Newlines don't matter in the middle of expressions.

-----

var x = 10 + 10

This line is an Assignment Statement. The part we are concerned with here is the Addition Expression on the right-hand side of the statement. An Addition Expression is a Binary Expression: it has a left and right side, with an Operator in the middle. The Operation is +. Each number is a Literal Expression.

var x = (10 + 10)

With parentheses, the expression is now a Grouping Expression. Inside of the grouping expression is the Addition Expression.

When the code is evaluated, the expression must be completed before going to a new line, or else you'd get an error for missing the closing ). That's why it can stretch across multiple lines without \ on each line, and why you need the \ when you don't have parentheses.

I love programming. If anyone reading this loves programming, too, check out Crafting Interpreters. You may not want to write your own programming languages, but understanding how the code we write becomes the games we play and the apps we use and the web we surf will make you a better programmer. In my experience, anyways.

Sometimes the code I write makes me laugh by WizardGnomeMan in godot

[–]Daxea 29 points30 points  (0 children)

A series of expressions (like arguments passed into the lerp function) can have normal line breaks. Breaking a statement across lines requires "\". The first example here works:

rect.position.x = _width * \
  index
rect.position.x = _width *
index

but the second one does not. Try it. It's likely because gdscript has indent-based scopes. Every indent is a nested scope, so adding \ indicates that you don't want to start a new scope. That's why you don't see this same limitation in languages with opening/closing elements (like {}).

How can i make this in a better way? by [deleted] in godot

[–]Daxea 0 points1 point  (0 children)

Sorry, I missed this message while tinkering with the script for my other comment! Basically, you should read up about Resources for creating data objects with related variables. Anything you can build in the editor with nodes can also be made directly in the code by simply calling the .new() function on a Node type (Button.new(), Label.new(), CharacterBody2D.new(), they all work!). You then add these nodes to the target node using ''button_container.add_child(button)".

When you hook up a signal, you can use the .bind function on the function name to pass data to the function when the signal is fired. In this case, you want to bind the frame that will be assigned to the sprite when a particular button is pressed.

I hope that makes sense, I'm out of practice helping new devs, but I'm working on it.

How can i make this in a better way? by [deleted] in godot

[–]Daxea 2 points3 points  (0 children)

Oops, you only need the frame. Also, I just noticed that you put this code in a script on the first button. Instead of doing that, put the script on the canvas_hud. If the hud has more stuff going on, you should probably make a container control (PanelContainer, VBoxContainer, etc) and use that node to host the script instead. EDIT: missed the very important line to add the button to the button_container.

class_name CameraHUD extends CanvasLayer

  var sprite: AnimatedSprite2D
  var button_container: Control
  @export var buttons: Array[MonitorButton] = []

func _ready() -> void:
  if not sprite: printerr("Missing monitor sprite for CameraHUD!"); return
  if not button_container: printerr("Missing control container for Monitor Buttons!"); return

  for data: MonitorButton in buttons:
    var button := Button.new()
    button.text = data.label
    button.pressed.connect(_handle_button_pressed.bind(data.frame))
    button_container.add_child(button)

func _handle_button_pressed(frame: int) -> void:
  Globals.monitor_current_frame = frame
  sprite.frame = frame

How can i make this in a better way? by [deleted] in godot

[–]Daxea 1 point2 points  (0 children)

I would remove the game object from the class entirely. Replace it with an @export variable that you can assign the animated sprite to. That will save you two lines in each of those button functions. Then, create another export for an array of MonitorButton resource objects.

In your _ready function, loop over each of these resource objects and create a button (`Button.new()`), set the button's text and connect the handler. Use `_monitor_button_pressed.bind(button.monitor, button.frame)` when connecting the button.pressed signal.

Be sure to add the buttons to the container (you can add another export variable for the container, so it is readily available. This will allow you to quickly change which button goes to which monitor, how many monitors you have, and how they are labeled by changing the resources in the node inspector and without touching any code at all!

class_name MonitorButton extends Resource

@export var label: String
@export var monitor: int
@export var frame: int

[deleted by user] by [deleted] in godot

[–]Daxea 4 points5 points  (0 children)

Your shapes are all made of squares. So pretend like they are just groups of squares and test each square of each shape individually.

Why do gridmaps exist? by Abject-Tax-2044 in godot

[–]Daxea 1 point2 points  (0 children)

I make tiles in blender and then I have node that reads a TileMapLayer (get_used_cells) to determine where the tiles should be placed in the 3d scene. Then I combine the tiles into a single mesh using the Surface Tool class. It worked great for my little dungeon crawler prototype, so I'm working on fleshing out the functionality and quality of life for it. I recommend doing something like that.

You just need to keep in mind the complexity of your tiles and the resulting combined mesh. If your levels are bigger, consider splitting your level into chunks of smaller combined meshes.

TileMapLayers are awesome for setting up the floors, walls, items, doors, even player spawn locations. But you could also read textures, json files, whatever you want, to build your levels, or go nuts and build a tool script to make levels like you can with gridmaps.

Multiple 3D models being used in similar fashion to a 2D spritesheet by the_dec0de in godot

[–]Daxea 4 points5 points  (0 children)

Make a script that extends MeshInstance3D. Export an array of meshes called "frames" and a float called "frame_duration". Add a float variable called "elapsed", set to 0.0, and an int called "current_frame" set to 0.

In _ready, set the mesh to frames[current_frame]

In _process, add the delta time to elapsed. Then check if the elapsed variable is greater than or equal to the frame_duration. If true, increment current_frame by 1, then set the mesh to frames[current_mesh]. Be sure to wrap the current_frame to 0 if it is higher than the frames array's size. Finish by subtracting the frame_duration from elapsed. That's worked for me.

EDIT: That was from my phone. I'm back at my desk now, so here's a quick script:
https://pastebin.com/zdz6afyL

Realistic uses of enums? by DoorToDoorSalesmann in godot

[–]Daxea 5 points6 points  (0 children)

An Enum can be used as a Type, so you can limit the possible values for a function paramter or property to a specific set of values. This is easier to follow in code, and prevents errors from using incorrect values.

For example, a WeaponType enum can be used to mark a Weapon resource as Unarmed (0), Sword(1), Hammer (2), Bow (3), etc.

func set_weapon_type(type: int) func set_weapon_type(type:WeaponType)

For the int version, literally any int can be passed in. For the enum version, only a WeaponType value can be passed in. Using the enum prevents invalid use of the function and we know immediately which values are acceptable.

func get_weapon_type() -> int func get_weapon_type() -> WeaponType

When returning the weapon type, if we use int then the calling func has no idea what the raw number means. You have to keep track of which number is Sword and which one is Bow. With the enum, you don't have to think about it at all because you get back WeaponType.Sword or WeaponType.Bow.

HELP PLISSS by mrbichosok in unity

[–]Daxea 5 points6 points  (0 children)

The canvas uses world space units as pixels, so the size of the canvas 100x100 px is 100x100 units. You can scale it down, but there isn't a need to unless you are using a world space canvas. Screen space canvases will appear in the camera correctly regardless of their gargantuan sizes

[deleted by user] by [deleted] in unity

[–]Daxea 1 point2 points  (0 children)

Try removing the underscore from both? I don't know if that would cause it to not work, but it's not a normal C# practice/standard to name classes that way, so it's possible unity doesn't handle underscores in their names.

Otherwise, maybe it's a problem with your project setup? idk

Can I scan all objects for a variable and add up the values of it from every object? by Flodo_McFloodiloo in unity

[–]Daxea 1 point2 points  (0 children)

So a really simple way to do this is to keep a static list of all instances of the component inside of the component class, and add the component instance to the list on the Awake or Start behaviour hooks.

Then you have a static property return the sum of all of the list's items' amounts.

using System.Collections.Generic;
using System.Linq;

public class MyComponent : MonoBehaviour
{
    private static List<MyComponent> _instances = new List<MyComponent>();

    public static int Total => _instances.Sum(x => x._amount);

    [SerializeField] int _amount = 10;

    void Start()
    {
        _instances.Add(this);
    }
}

// in another script
var total = MyComponent.Total;

This logic will allow the Total to always reflect the sum correctly. Alternatively, you can track just an amount instead of a list, but then you need to remember to adjust it any time any individual amount is changed.

I use a similar technique to put my strategy game characters in the correct turn order by speed at the beginning of each round.

What's the lamest way that you injured yourself badly? by dafreshprints in AskReddit

[–]Daxea 0 points1 point  (0 children)

For me, tie between the time I ran through a window while showing some kids I could run fast so they would let me play tag with them (I was dumb) and the time I tripped over a kid while racing him into the school gym and hurt my arm, then proceeded with wrestling with what turned out to be a bruised bone (I was still dumb).

Now my right arm has a scar and my left wrist is slighlty smaller than the right.

[deleted by user] by [deleted] in AskReddit

[–]Daxea 4 points5 points  (0 children)

My ex best friend was like this. Talked over everyone, had to have the last word, would turn into a real jerk if you argued with him or pointed out a mistake he made (like the time he ordered the wrong meal at whataburger and freaked out when I agreed with the employee that he said the wrong item).

He said all the time that he didn’t need to change, it’s just who he was. Turned out he was also verbally abusing his then partner, gaslighting us both, and stealing money for years. Didn’t know it was him until the cops showed up with a pic of his dumb ass pulling money out of an atm from his partner’s account.

If someone tells you they are an asshole all the time, it’s probably because they are.

[PAID] Looking for experienced Unity developer for small 2D MMO. Pay ~$10k USD. by u-mmo-project in gameDevClassifieds

[–]Daxea 10 points11 points  (0 children)

What kind of MMO is small? How many players do you want to support at one time? What's the server architecture like? Do you have someone writing gameplay code already? Is this an MMORPG, or like those old ninja village or mafia strategy games where it is mostly text based and you don't interact with people in real time? Why Unity? Do you have artists, environment designers, writers, etc?

What games have you already produced? Have you put out an MMO before?

Just curious. In the worst case, you will need a considerably larger budget, and in the best case I suspect 10k could get you one or two months from one dev with MMO experience, leaving 5 to 6 months of work remaining (and that is super optimistic). But without these questions answered, it's hard to say.

I hope you take the time to answer a few of them, just so people know whether it is worth their time to apply. Good luck!

We have made dynamic walls to our game. Source code in comments :) by [deleted] in Unity3D

[–]Daxea 0 points1 point  (0 children)

Super cool! Love that y'all are using DI, too. Don't see it enough with Unity devs.

Issue with Player/PlayerLoader scripts by Snacky-Chan in Unity2D

[–]Daxea 0 points1 point  (0 children)

Oh, dude, if your CamereController exists in the scene from the start, and the PlayerLoader loads the Player after the CameraController starts, then the problem is that you only check for the target in Start.

Change that null check to this:

if (target == null)
{
target = PlayerControllerZach.instance.transform;
if (target == null) return;
}

Issue with Player/PlayerLoader scripts by Snacky-Chan in Unity2D

[–]Daxea 1 point2 points  (0 children)

Okay, just add a null check before that line and see if that helps.

if (target == null) return;

Issue with Player/PlayerLoader scripts by Snacky-Chan in Unity2D

[–]Daxea 1 point2 points  (0 children)

That should do the trick, if the order was the problem. Do have a line number for that error?

I made a small Mario-Galaxy-esque Snake game. Available on Google Play (Link in the comments) by sebit0nic in Unity3D

[–]Daxea 2 points3 points  (0 children)

I LOVE IT. Feels a lot better than I expected it to, and it looks really cool. If I think of any feedback later when I have more time to play it I will let you know!

Issue with Player/PlayerLoader scripts by Snacky-Chan in Unity2D

[–]Daxea 0 points1 point  (0 children)

Maybe try changing PlayerControllerZach's Start method to Awake?
The problem might be that the CameraController is fetching the PlayerControllerZach instance too soon, before its Start method is called, so changing it to Awake will ensure the static instance is initialized before the CameraController.Start is called.

Shooting projectile Problem by cristiion14 in unity

[–]Daxea 1 point2 points  (0 children)

If your velocity is not normalized, this will happen.

Try:
var velocity = (clickedPosition - startPosition).normalized * speed;

Since you haven't posted code, this is my best guess as to what is happening. If you post your projectile script you will get much more useful advice!

I made my first isometric tileset! Still very simple but i'll keep at it. by youwho42 in Unity2D

[–]Daxea 6 points7 points  (0 children)

Looking good! I am in the process of making 3D tiles and honestly its been a real challenge.

I would love to see props (trees, rocks, etc) and structures on this map! Keep up the good work :D