Programming thread

  • 🐕 I am attempting to get the site runnning as fast as possible. If you are experiencing slow page load times, please report it.
I'm working on a broadphase collision detection system, I haven't decided which exact structure I'm going to use(e.g. uniform grid or quadtree), so I'd like the, to work as drop-in replacements, so I came up with a following interface that they would implement. However, there's a problem: every item has to cache its previous index data, so that it doesn't have to be recalculated on every update, and its type depends on the broadphase class that I use, so the item has to have a type parameter, and that ruins everything.
C-like:
/**
 * Structure that performs broadphase collision detection
 * @param U index data type
 */
interface Broadphase<U> {
    /**
     * Adds an item
     * @param item item to add
     */
    function add(item: Collider<U>): Void;
    /**
     * Updates an item that's already added
     * @param item item to update
     * @param data old index data
     */
    function update(item: Collider<U>): Void;
    /**
     * Removes an item that's already added
     * @param item item to update
     * @param data old index data
     */
    function remove(item: Collider<U>): Void;
    /**
     * Gets pairs of pottentially colliding items
     * @return array of pairs
     */
    function getPairs(): Array<Pair<Collider<U>>>;
}
So, the question is: how do I prevent it while also tying the index data to the item. Obvious answer: use a hastable inside the broadphase class, that maps every item to its index data, but accessing the hashtable becomes slower as it grows, and potentially there's gonna be tons of items.
 
C#:
                if(itmID == 0){
                    Vector2I Size = new Vector2I(640, 360);
                    DisplayServer.WindowSetSize(Size);
                } else if(itmID == 1){
                    Vector2I Size = new Vector2I(1280, 720);
                    DisplayServer.WindowSetSize(Size);
                } else if(itmID == 2){
                    Vector2I Size = new Vector2I(1920, 1080);
                    DisplayServer.WindowSetSize(Size);
                }

maxresdefault-1595723723.jpg

C#:
        int[] x = [640, 1280, 1920];
        int[] y = [360, 720, 1080];
        Vector2I Size = new Vector2I(x[itmID], y[itmID]);
        DisplayServer.WindowSetSize(Size);
 
Last edited:
C#:
                if(itmID == 0){
                    Vector2I Size = new Vector2I(640, 360);
                    DisplayServer.WindowSetSize(Size);
                } else if(itmID == 1){
                    Vector2I Size = new Vector2I(1280, 720);
                    DisplayServer.WindowSetSize(Size);
                } else if(itmID == 2){
                    Vector2I Size = new Vector2I(1920, 1080);
                    DisplayServer.WindowSetSize(Size);
                }

View attachment 6529529

C#:
        int[] x = [640, 1280, 1920];
        int[] y = [360, 720, 1080];
        Vector2I Size = new Vector2I(x[itmID], y[itmID]);
        DisplayServer.WindowSetSize(Size);
View attachment 6529586
C#:
int[] y = [360, 480, 720, 1080];
Vector2I Size = new Vector2I(y[itmID] / 9 * 16, y[itmID]);
DisplayServer.WindowSetSize(Size);
hqdefault-3415849216.jpg
 
C#:
int[] y = [360, 480, 720, 1080];
Vector2I Size = new Vector2I(y[itmID] / 9 * 16, y[itmID]);
DisplayServer.WindowSetSize(Size);
Annoy your coworkers with this one weird trick.
C#:
int y=60*itmID*itmID+60*itmID+360;
Vector2I Size = new Vector2I(y / 9 * 16, y);
DisplayServer.WindowSetSize(Size);
 
Last edited:
Annoy your coworkers with this one weird trick.
@y a t s, too:
math is fun, but not easily readable and ill-suited to different window proportions.
What if you need to display or otherwise use a list of existing options, would you be recalculating x for that every time?
@${Sandy} 's variant is the best. (Or an array of 2-tuples, to ensure collections of widths and heights of equal length.)
 
@y a t s, too:
math is fun, but not easily readable and ill-suited to different window proportions.
What if you need to display or otherwise use a list of existing options, would you be recalculating x for that every time?
@${Sandy} 's variant is the best. (Or an array of 2-tuples, to ensure collections of widths and heights of equal length.)
I agree about the readability; I was just being a smartass.

Realistically, I would probably have a setup that allows for varying aspect ratios, but in a more readable way. Simply moving the inlined math to its own descriptively named variable helps a lot.
imo this sort of thing is better suited for a helper function that is used to help assemble and manage the list of valid resolutions for a given display. This list could then be saved into memory by a calling function or something. These calculations aren't expensive enough to outweigh the benefits of the increased flexibility, especially considering you would probably only need to calculate them once on startup.
 
Im gonna end up rewriting almost all of it anyways because I set it up to save everything to a cfg file and it works as intended but I feel like I can clean my code up a bit and do it better.
 
>Write a Script
>Click run project on godot to see if it works
>Have to wait for it to build project
>Script doesn't work
>Wash rinse and repeat for hours on end
>Script finally works
>Get excited and try telling people in my family
>They just say, "Thats cool buddy" like I'm an autistic 8 year old that drew a picture of a minecraft creeper with crayons
>Mfw
i_hate_being_alive.jpg
 
I tried asking somebody IRL this question and they didn't have an answer.

But I've been using the Godot engine for C#.
The way the engine works is you have different scenes, and all the ui elements and game shit are objects called nodes.

When I attach a script to the root node of the scene and I try to tell other nodes such as an option box or check button or label node to do something in that script it doesn't work but doesn't throw any errors.

I find that all of my scripts work just fine if I just make a script for each node in the scene.

This seems to work and it also keeps alot of my code separated and easy to sort through which I like. But I was wondering, for a project of this nature is it a good practice to have a cs file for every component in a given scene?
 
But I was wondering, for a project of this nature is it a good practice to have a cs file for every component in a given scene?
No
When I attach a script to the root node of the scene and I try to tell other nodes such as an option box or check button or label node to do something in that script it doesn't work but doesn't throw any errors.
In gdscript you simply do $node/node.function_call() arbitrarily if you're traversing down the tree, should be the same concept in c# but I've never used c# with godot, apparently it's GetNode<Node3D>("node/node").
 
In gdscript you simply do $node/node.function_call() arbitrarily if you're traversing down the tree, should be the same concept in c# but I've never used c# with godot, apparently it's GetNode<Node3D>("node/node").
That seems to work for most things and I do that as much as I can. But for example, I have an option box and it saves its current selection to a config file.

But when you reopen the options menu
C#:
int current = (int)config.GetValue("Resolution", "Selection")
GetNode<OptionButton>("nodepath here").Select(config)

Simply just does not work and it wont even throw an error.

But when attach a script to the options box node and type in
C#:
int current = (int)config.GetValue("Resolution", "Selection")

this.Select(config)

It works exactly as intended.
Edit, I just noticed I made a few syntax errors but I made this post from the phone. Im sure you guys get the point though.
 
That seems to work for most things and I do that as much as I can. But for example, I have an option box and it saves its current selection to a config file.

But when you reopen the options menu
C#:
int current = (int)config.GetValue("Resolution", "Selection")
GetNode<OptionButton>("nodepath here").Select(config)

Simply just does not work and it wont even throw an error.

But when attach a script to the options box node and type in
C#:
int current = (int)config.GetValue("Resolution", "Selection")

this.Select(config)

It works exactly as intended.
Idk your node path is probably wrong? add some printlns or use a debugger
 
Idk your node path is probably wrong? add some printlns or use a debugger
I did actually make that mistake originally and fixed it.

Could the issue be that I need to attach the root nodes script to the other nodes as well?
 
I did actually make that mistake originally and fixed it.

Could the issue be that I need to attach the root nodes script to the other nodes as well?
No you can put the script anywhere in the tree but the root can address any node, nodes below the root cant address the root. I'm gonna guess you're addressing the wrong node but think you're addressing another one, that's why it only works when your script is attached to it but fails silently otherwise. Use % unique names in the tree for your node and address using that
 
Back