C
C#2y ago
Pdawg

Calling a function from a string.

Hey, everyone! I have a timer that will run every 100ms, but I have a list of functions named "f1", all the way up to "f255". I know it sounds impractical to even have all of those functions, but I need them. Anyway, is it possible to have an int++ to call the next function on the list? I want it to do this every time the timer runs: 1: add 1 to the framei int. 2: add the 'f' at the start of the function name, then attach framei after it 3: call the function Expected behavior: 100ms elapsed, f1(); another 100ms elapsed f2(); and so on. Here is an example of the code that doesn't work:
97 Replies
ero
ero2y ago
why would you possibly need all those functions?
Pdawg
Pdawg2y ago
I'm doing a challenge. Run bad apple by manually updating a pixel grid that was created in XAML Each frame is a function, setting pixels that need to be set I told you it's impractical, but I want to do it So is it possible?
ero
ero2y ago
i feel like this would be better of in one single method honestly
mrphil2105
mrphil21052y ago
What do these methods do?
ero
ero2y ago
print a bunch of full size blocks i guess
mrphil2105
mrphil21052y ago
If they just print the same thing but with a different number then use one method with a parameter
Pdawg
Pdawg2y ago
Manually updates each pixel on a pixel grid I haven't actually implemented that yet...since I need this to work first
mrphil2105
mrphil21052y ago
It is possible to call a function knowing its string, but I am not sure that's the solution here
ero
ero2y ago
"bad apple" is a song with a music video. people have started playing said music video on weird stuff just like "running DOOM on a calculator" or whatever https://youtu.be/2QiDb_khg44
Pdawg
Pdawg2y ago
Yup. Precisely. I have the frontend working, just not the backend How would I do that?
Pdawg
Pdawg2y ago
mrphil2105
mrphil21052y ago
Don't in this case
Pdawg
Pdawg2y ago
This is the app btw. That black square is actually made up of many small squares
mrphil2105
mrphil21052y ago
Calling a function by it's name is only necessary when you have no other option It's slow and kind of "bad code"
Pdawg
Pdawg2y ago
Would it still hit that 100ms mark? Like execute within 1ms
ero
ero2y ago
you probably want like a list or if you must, a dictionary
Pdawg
Pdawg2y ago
This is a 3532 frame video or something like that So fun
ero
ero2y ago
and just access the frame via the index/key
Pdawg
Pdawg2y ago
Oh, so like
framei++;
var dict = new Dictionary<int, Action>
{
{ 1, () => Console.WriteLine("Case 1") }
{ 2, () => Console.WriteLine("Case 2") }
{ 3, () => Console.WriteLine("Case 3") }
}
framei++;
var dict = new Dictionary<int, Action>
{
{ 1, () => Console.WriteLine("Case 1") }
{ 2, () => Console.WriteLine("Case 2") }
{ 3, () => Console.WriteLine("Case 3") }
}
Kinda
ero
ero2y ago
List<string> frames = new()
{
"",
...
}
List<string> frames = new()
{
"",
...
}
i would just do this? and then do
Console.WriteLine(frames[frame]);
Console.WriteLine(frames[frame]);
Pdawg
Pdawg2y ago
Wouldn't going through a list every time the timer hits be slower though?
ero
ero2y ago
what do you mean?
Pdawg
Pdawg2y ago
Than just directly generating the call every time
ero
ero2y ago
no? it will be so much faster you don't create the list anew every time
Pdawg
Pdawg2y ago
Ohh, okay. That makes sense I'll try this out Thank you
pip
pip2y ago
just for education's sake, the way that you're suggesting(the very bad way) might look something like classObject.GetType().GetMethod("namehere").Invoke(classObject, null); or something
ero
ero2y ago
private static int _frame;
private static readonly List<string> _frames = new List<string>()
{
// ...
};

private void Timer_Tick(object sender, EventArgs e)
{
Console.WriteLine(_frames[_frame++]);
}
private static int _frame;
private static readonly List<string> _frames = new List<string>()
{
// ...
};

private void Timer_Tick(object sender, EventArgs e)
{
Console.WriteLine(_frames[_frame++]);
}
it's called reflection, if you still care to look it up you probably don't need the GetType call
typeof(YourClass)
.GetMethod($"f{_frame++}", BindingFlags.Static | BindingFlags.NonPublic)
.Invoke(null, null);
typeof(YourClass)
.GetMethod($"f{_frame++}", BindingFlags.Static | BindingFlags.NonPublic)
.Invoke(null, null);
pip
pip2y ago
oh cool, that works too
ero
ero2y ago
but yeah, reflection is incredibly slow, and incredibly bad code
Pdawg
Pdawg2y ago
This seems to want C# 9.0
Pdawg
Pdawg2y ago
ero
ero2y ago
and why are you on such an old version?
Pdawg
Pdawg2y ago
This is a UWP app That's what the blank app generates
ero
ero2y ago
hm just don't use target typed new then
pip
pip2y ago
just put List<string> after it right?
ero
ero2y ago
it's really not a big deal yeah
Pdawg
Pdawg2y ago
Ugh My visual studio install broke again Atleast I can still do this @Ero Would I just add each method as a string?
ero
ero2y ago
what do you mean?
Pdawg
Pdawg2y ago
It doesn't like _frames.Add("f1);
ero
ero2y ago
i'm not sure what you're referring to you'd put whatever you want to print on that frame
Pdawg
Pdawg2y ago
Wait, so this doesn't call another method? This frame can require from 1-256 changes at the same time The methods need to be separate from the list because it would become a jumbled mess
ero
ero2y ago
you don't even have any of the methods
Pdawg
Pdawg2y ago
f1() and f2() for testing
ero
ero2y ago
this is literally your entire code
Pdawg
Pdawg2y ago
Yeah, but the pixels are different for each frame, so it can't be in one single set of code
ero
ero2y ago
what do you mean? you'd obviously have the pixels be different for each list entry
private static readonly List<string> _frames = new List<string>()
{
"pixels here",
"different pixels here",
"more different pixels"
};
private static readonly List<string> _frames = new List<string>()
{
"pixels here",
"different pixels here",
"more different pixels"
};
Pdawg
Pdawg2y ago
Ok. So what I mean then is it won't let me create an entry. I've never used a list before, only arrays and ObservableCollections You just...put that?
ero
ero2y ago
yes
Pdawg
Pdawg2y ago
Does each comma start the next frame?
ero
ero2y ago
it doesn't "start" anything it's just a different list entry accessible by the list's index you could even use an array, might even be better
Pdawg
Pdawg2y ago
By the way, each pixel is set by a color value So example: P12 = Colors.White; P24 = Colors.Black; That would change the color of each pixel Oh okay. Also, it won't let me access the pixel grid from the list
Pdawg
Pdawg2y ago
Pdawg
Pdawg2y ago
If I put it into an event handler, it's there
Pdawg
Pdawg2y ago
Pdawg
Pdawg2y ago
In the list, it has no idea what it is @Ero Is there a fix for this? I think it has to do with routing the event(?)
Anchy
Anchy2y ago
the issue lies with that the list is expecting a string inside of the initializer however you are trying to assign a brush to a control property inside of that initializer
Pdawg
Pdawg2y ago
Yeah, but even trying to get the P(number)s to come up doesn’t work either
Anchy
Anchy2y ago
I don't follow?
Pdawg
Pdawg2y ago
Basically, each pixel has a name: (example), P1, P2, P3, and so on Each "pixel" is created in xaml But I can't access the pixels from the list Okay, I understand what you're talking about now. Is there a way to fix this? Can I make it expect a control property?
Anchy
Anchy2y ago
what exactly are you trying to do
Pdawg
Pdawg2y ago
So...I have a grid of "pixels" (rectangles) and I need them to be updated every frame. Bad Apple is two colors, black and white So every frame, some pixels will have a P1.Fill = new SolidColorBrush(Colors.White); or a P1.Fill = new SolidColorBrush(Colors.Black); And I have P1-P256 Each rectangle is one pixel, in a 16x16 grid
Anchy
Anchy2y ago
right, so im assuming you will be reading this pixel data in from somewhere
Pdawg
Pdawg2y ago
Nope. This is a challenge, I will be manually creating each frame in a function So P1-P256 can be changed each time the timer is called, to whatever value they need to change to I tried earlier, the List thing didn't want to work
Anchy
Anchy2y ago
just create a class which defines a frame, then create an ordered list of frames then you can loop through your list of frames and render them based on your frame definition creating 256 functions for each frame is really stupid
Pdawg
Pdawg2y ago
Oh I was gonna create 3288 functions to update each pixel at once Lol Though that would be like 800k lines of code (not even joking, I did the math)
Anchy
Anchy2y ago
i don't really see the point in doing that
Pdawg
Pdawg2y ago
So a loop that counts up to 3288 That makes sense, but the issue lies in the list itself The List does not want to cooperate with me...at all
Pdawg
Pdawg2y ago
@Anchy
Pdawg
Pdawg2y ago
Here is the issue It doesn't want to let me create a list of pixels for each frame This code is what the person who previously tried to solve this gave me
Anchy
Anchy2y ago
public class Pixel
{
public Color Color { get; set; }
public int X { get; set; }
public int Y { get; set; }

public Pixel(Color color, int x, int y) { this.Color = color; this.X = x; this.Y = y; }
}

public List<Pixel[]> Frames = new List<Pixel[]>
{
new Pixel[]
{
new Frame(Color.White, 0, 1),
new Frame(Color.Black, 1, 1)
}
};
public class Pixel
{
public Color Color { get; set; }
public int X { get; set; }
public int Y { get; set; }

public Pixel(Color color, int x, int y) { this.Color = color; this.X = x; this.Y = y; }
}

public List<Pixel[]> Frames = new List<Pixel[]>
{
new Pixel[]
{
new Frame(Color.White, 0, 1),
new Frame(Color.Black, 1, 1)
}
};
you could do something like this I suppose, however even this would be really slow
Pdawg
Pdawg2y ago
As long as it hits around the +-5ms mark, it'll be okay
Anchy
Anchy2y ago
I have no idea what the performance would be like
pip
pip2y ago
You can thread diff parts of the image youre recoloring but ye idk the performance either
Pdawg
Pdawg2y ago
This code seems to be for an actual pixel grid. The "pixels" don't have x and y values. Instead, they are all assigned names
Anchy
Anchy2y ago
wat
Pdawg
Pdawg2y ago
I know, it's confusing, but the challenge was to create a grid of "pixels" in XAML, and then change their color each frame
Pdawg
Pdawg2y ago
Pdawg
Pdawg2y ago
Look at this xaml
Anchy
Anchy2y ago
oh you mean the Rectangles
Pdawg
Pdawg2y ago
This is literally how each pixel out of the 256 are made
Anchy
Anchy2y ago
well just replace X and Y with one Rectangle then
Pdawg
Pdawg2y ago
Up to 256 of them will need to be changed at once Don't think that'll do
Anchy
Anchy2y ago
public class Pixel
{
public Color Color { get; set; }
public Rectangle Rect { get; set; }

public Pixel(Color color, Rectangle rect) { this.Color = color; this.Rect = rect; }
}

public List<Pixel[]> Frames = new List<Pixel[]>
{
new Pixel[]
{
new Frame(Color.White, P1),
new Frame(Color.Black, P2)
}
};
public class Pixel
{
public Color Color { get; set; }
public Rectangle Rect { get; set; }

public Pixel(Color color, Rectangle rect) { this.Color = color; this.Rect = rect; }
}

public List<Pixel[]> Frames = new List<Pixel[]>
{
new Pixel[]
{
new Frame(Color.White, P1),
new Frame(Color.Black, P2)
}
};
again, trying to do this by hand would just be a lot of work for no reason i'd sooner try and read the video file directly and fetch the pixel data before trying to hardcode each frame
Pdawg
Pdawg2y ago
This gives the same issue as before
Pdawg
Pdawg2y ago
Pdawg
Pdawg2y ago
The original video is 320x320 The content area of xaml pixels is 16x16
Anchy
Anchy2y ago
oh rename Frame to Pixel originally it was called Frame
Pdawg
Pdawg2y ago
Pdawg
Pdawg2y ago
I'm talking about these It doesn't like the fact that it's pointing to a rectangle So unless I put 102,400 small rectangles on a page, that's not gonna work
Anchy
Anchy2y ago
no it just doesn't like using that reference inside the initializer so instead I would just create the list inside the constructor
Pdawg
Pdawg2y ago
Actually, I could do that I have a 16x16 dump of all the frames
Pdawg
Pdawg2y ago
Pdawg
Pdawg2y ago
Why doesn't discord support bitmaps Anyway, these are pretty good. I could try and "decode" these for each frame
Anchy
Anchy2y ago
I think the System.Drawing namespace supports opening bitmaps and you can even loop through all of the pixels
Pdawg
Pdawg2y ago
Awesome! Will try it out tomorrow. Thanks!
MasterSubarashii
(sorry this is not related to your question but is this WPF? Because it looking quite good)
Pdawg
Pdawg2y ago
No, it’s UWP with winui 2.8 lol It would be a lot of work to make a WPF app look this good
Want results from more Discord servers?
Add your server
More Posts