C
C#โ€ข3y 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
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
why would you possibly need all those functions?
Pdawg
Pdawgโ€ข3y 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?
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
i feel like this would be better of in one single method honestly
mrphil2105
mrphil2105โ€ข3y ago
What do these methods do?
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
print a bunch of full size blocks i guess
mrphil2105
mrphil2105โ€ข3y ago
If they just print the same thing but with a different number then use one method with a parameter
Pdawg
Pdawgโ€ข3y ago
Manually updates each pixel on a pixel grid I haven't actually implemented that yet...since I need this to work first
mrphil2105
mrphil2105โ€ข3y ago
It is possible to call a function knowing its string, but I am not sure that's the solution here
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y 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
Pdawgโ€ข3y ago
Yup. Precisely. I have the frontend working, just not the backend How would I do that?
Pdawg
Pdawgโ€ข3y ago
mrphil2105
mrphil2105โ€ข3y ago
Don't in this case
Pdawg
Pdawgโ€ข3y ago
This is the app btw. That black square is actually made up of many small squares
mrphil2105
mrphil2105โ€ข3y 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
Pdawgโ€ข3y ago
Would it still hit that 100ms mark? Like execute within 1ms
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
you probably want like a list or if you must, a dictionary
Pdawg
Pdawgโ€ข3y ago
This is a 3532 frame video or something like that So fun
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
and just access the frame via the index/key
Pdawg
Pdawgโ€ข3y 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
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y 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
Pdawgโ€ข3y ago
Wouldn't going through a list every time the timer hits be slower though?
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
what do you mean?
Pdawg
Pdawgโ€ข3y ago
Than just directly generating the call every time
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
no? it will be so much faster you don't create the list anew every time
Pdawg
Pdawgโ€ข3y ago
Ohh, okay. That makes sense I'll try this out Thank you
pip
pipโ€ข3y 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
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y 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
pipโ€ข3y ago
oh cool, that works too
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
but yeah, reflection is incredibly slow, and incredibly bad code
Pdawg
Pdawgโ€ข3y ago
This seems to want C# 9.0
Pdawg
Pdawgโ€ข3y ago
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
and why are you on such an old version?
Pdawg
Pdawgโ€ข3y ago
This is a UWP app That's what the blank app generates
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
hm just don't use target typed new then
pip
pipโ€ข3y ago
just put List<string> after it right?
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
it's really not a big deal yeah
Pdawg
Pdawgโ€ข3y ago
Ugh My visual studio install broke again Atleast I can still do this @Ero Would I just add each method as a string?
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
what do you mean?
Pdawg
Pdawgโ€ข3y ago
It doesn't like _frames.Add("f1);
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
i'm not sure what you're referring to you'd put whatever you want to print on that frame
Pdawg
Pdawgโ€ข3y 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
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
you don't even have any of the methods
Pdawg
Pdawgโ€ข3y ago
f1() and f2() for testing
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
this is literally your entire code
Pdawg
Pdawgโ€ข3y ago
Yeah, but the pixels are different for each frame, so it can't be in one single set of code
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y 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
Pdawgโ€ข3y 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?
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y ago
yes
Pdawg
Pdawgโ€ข3y ago
Does each comma start the next frame?
er๐ŸŽƒ
er๐ŸŽƒโ€ข3y 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
Pdawgโ€ข3y 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
Pdawgโ€ข3y ago
Pdawg
Pdawgโ€ข3y ago
If I put it into an event handler, it's there
Pdawg
Pdawgโ€ข3y ago
Pdawg
Pdawgโ€ข3y 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
Anchyโ€ข3y 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
Pdawgโ€ข3y ago
Yeah, but even trying to get the P(number)s to come up doesnโ€™t work either
Anchy
Anchyโ€ข3y ago
I don't follow?
Pdawg
Pdawgโ€ข3y 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
Anchyโ€ข3y ago
what exactly are you trying to do
Pdawg
Pdawgโ€ข3y 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
Anchyโ€ข3y ago
right, so im assuming you will be reading this pixel data in from somewhere
Pdawg
Pdawgโ€ข3y 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
Anchyโ€ข3y 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
Pdawgโ€ข3y 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
Anchyโ€ข3y ago
i don't really see the point in doing that
Pdawg
Pdawgโ€ข3y 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
Pdawgโ€ข3y ago
@Anchy
Pdawg
Pdawgโ€ข3y 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
Anchyโ€ข3y 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
Pdawgโ€ข3y ago
As long as it hits around the +-5ms mark, it'll be okay
Anchy
Anchyโ€ข3y ago
I have no idea what the performance would be like
pip
pipโ€ข3y ago
You can thread diff parts of the image youre recoloring but ye idk the performance either
Pdawg
Pdawgโ€ข3y 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
Anchyโ€ข3y ago
wat
Pdawg
Pdawgโ€ข3y 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
Pdawgโ€ข3y ago
Pdawg
Pdawgโ€ข3y ago
Look at this xaml
Anchy
Anchyโ€ข3y ago
oh you mean the Rectangles
Pdawg
Pdawgโ€ข3y ago
This is literally how each pixel out of the 256 are made
Anchy
Anchyโ€ข3y ago
well just replace X and Y with one Rectangle then
Pdawg
Pdawgโ€ข3y ago
Up to 256 of them will need to be changed at once Don't think that'll do
Anchy
Anchyโ€ข3y 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
Pdawgโ€ข3y ago
This gives the same issue as before
Pdawg
Pdawgโ€ข3y ago
Pdawg
Pdawgโ€ข3y ago
The original video is 320x320 The content area of xaml pixels is 16x16
Anchy
Anchyโ€ข3y ago
oh rename Frame to Pixel originally it was called Frame
Pdawg
Pdawgโ€ข3y ago
Pdawg
Pdawgโ€ข3y 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
Anchyโ€ข3y 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
Pdawgโ€ข3y ago
Actually, I could do that I have a 16x16 dump of all the frames
Pdawg
Pdawgโ€ข3y ago
Pdawg
Pdawgโ€ข3y ago
Why doesn't discord support bitmaps Anyway, these are pretty good. I could try and "decode" these for each frame
Anchy
Anchyโ€ข3y ago
I think the System.Drawing namespace supports opening bitmaps and you can even loop through all of the pixels
Pdawg
Pdawgโ€ข3y ago
Awesome! Will try it out tomorrow. Thanks!
MasterSubarashii
MasterSubarashiiโ€ข3y ago
(sorry this is not related to your question but is this WPF? Because it looking quite good)
Pdawg
Pdawgโ€ข3y 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