Convert String to Variable Name Reference?
It's very simple. I have 1,000+ background sprites. Each one is named for the page it displays on. However, not every page gets a sprite. There must be a way to convert a string to a variable name. In Javascript, we used .style.display = 'block'. I also remember a $$ option where the second dollar sign was attached to a string.
// VARIABLES: PAGE TRACKER
public int page_current;
// VARIABLES: GUI
public string whichSprite;
public Sprite sprite_blank;
public Sprite sprite_bkg_00897;
public Sprite sprite_bkg_00971;
public Sprite sprite_bkg_00999;
public Image image_background;
// FUNCTION: RETURN [STRING] APPROPRIATE PAGE REFERENCE
public string returnPageString (page_current) =>
return "sprite_bkg_" + page_current.ToString("D5");
// METHOD: SWITCH BACKGROUND SPRITES BASED ON PAGE
public void swapBackground (string inputSpriteName) {
switch (page_current) {
case 896:
case 970:
case 998:
image_background.sprite = sprite_blank;
return;
}
// CONVERT FROM STRING TO SPRITE VARIABLE AND HAND TO BKG
image_background.sprite = (???variable???)inputSpriteName.???????????;
}
// ON PAGE 897, swapBackground SHOULD SWAP IN sprite_bkg_00897.
// ON PAGE 971, swapBackground SHOULD SWAP IN sprite_bkg_00971.
// ETC.
// THERE ARE NO PAGES BETWEEN 897 AND 970--899 JUMPS TO 970.
I'm not doing a 1000+ switch cases if I can just do it in one line.
I'm not doing a 1000+ dictionary.Add() (faster, but double the code).
Sprite[1000+] is out because of all the gaps.
Can I beat this with a reference?
Help!!!
Answer by canslp · Jul 27, 2020 at 12:26 PM
this is not the answer but it seems like what you're trying to do is the reason arrays and lists exist? like would it be easier to just identify them by an index instead?
Your answer

Follow this Question
Related Questions
Help with variables 1 Answer
Sorting by Variables Help 0 Answers
When Key is Pressed add one to variable 1 Answer