Author Topic: If this else that  (Read 6635 times)

gunthermic

  • Full Member
  • ***
  • Posts: 26
    • View Profile
If this else that
« on: January 24, 2019, 12:18:51 AM »
I am new to this, but can't seem to get this right. If [Players] is empty/blank don't display this png but a different one.

Code: [Select]
if ("[Players]" != ""){
local players_back = fe.add_image("images/players.png", pb_x, pb_y, pb_w, pb_h);
players_back.alpha = 235;
//local p = fe.add_text( "[Players]", flx*0.882, fly* 0.780, flw*0.105, flh*0.030);
local p = fe.add_text( "[Players]", pb_text_x, pb_text_y, pb_text_w, pb_text_h);
p.charsize = 28;
p.set_rgb( 0, 0, 0 );
p.font="Roboto";
p.align = Align.Centre;
p.style = Style.Bold;
}
else{
local players_back = fe.add_image("images/none.png", pb_x, pb_y, pb_w, pb_h);
}

rand0m

  • Sr. Member
  • ****
  • Posts: 343
    • View Profile
Re: If this else that
« Reply #1 on: January 24, 2019, 03:29:36 AM »
I have used the following function but its for images only.

Code: [Select]
function PlayerIcon() {
if (fe.game_info( Info.Players ) == "1") return "images/players/player01.png";
else if (fe.game_info( Info.Players ) == "2") return "images/players/player02.png";
else if (fe.game_info( Info.Players ) != "1" || "2") return "images/players/unknownplayers.png";
 }

local PlayerIcon= fe.add_image("[!PlayerIcon]", 1442, 238, 140, 130);

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: If this else that
« Reply #2 on: January 24, 2019, 04:06:22 AM »
What rand0m did is called a custom magic function. Check that area of layouts.md and it might make more sense.

gunthermic

  • Full Member
  • ***
  • Posts: 26
    • View Profile
Re: If this else that
« Reply #3 on: January 25, 2019, 12:46:25 AM »
I have used the following function but its for images only.

Code: [Select]
function PlayerIcon() {
if (fe.game_info( Info.Players ) == "1") return "images/players/player01.png";
else if (fe.game_info( Info.Players ) == "2") return "images/players/player02.png";
else if (fe.game_info( Info.Players ) != "1" || "2") return "images/players/unknownplayers.png";
 }

local PlayerIcon= fe.add_image("[!PlayerIcon]", 1442, 238, 140, 130);

rand0m!!! Thank you very kindly.

how could one do this by making it, if players isblank/empty do unknownplayers? i.e I have soem games players =3 so if it has 1,2,3,4,5,6,7,8 do an image and if not display unknown.

I did get about code working with mine. but liek I said I have some that have 3 players, or 4 or even 8..

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: If this else that
« Reply #4 on: January 25, 2019, 01:06:37 AM »
Code: [Select]
function PlayerIcon() {
fe.game_info( Info.Players)
   if (fe.game_info(Info.Players) >= 1 && fe.game_info(Info.Players) <=8) return "images/players/player0" + fe.game_info(Info.Players) +  ".png";
   return "images/players/unknownplayers.png";
 }

local PlayerIcon= fe.add_image("[!PlayerIcon]", 1442, 238, 140, 130);

rand0m

  • Sr. Member
  • ****
  • Posts: 343
    • View Profile
Re: If this else that
« Reply #5 on: January 25, 2019, 09:09:01 AM »
rand0m!!! Thank you very kindly.

how could one do this by making it, if players isblank/empty do unknownplayers? i.e I have soem games players =3 so if it has 1,2,3,4,5,6,7,8 do an image and if not display unknown.

I did get about code working with mine. but liek I said I have some that have 3 players, or 4 or even 8..

urw, Keil's code above is far more polished and to the point. The code I shared is a custom [Magic Token], when using such code you can give any number of instructions like
Code: [Select]
else if (fe.game_info( Info.Players ) == "3") return "images/players/player03.png";
else if (fe.game_info( Info.Players ) == "4") return "images/players/player04.png";
and so on...

Since we are working with AM and AM's default behavior is to leave the player field empty in romlist if no data is found. Following will work for empty player data.
Code: [Select]
else if (fe.game_info( Info.Players ) == "") return "images/players/unknownplayers.png";

Since a game might have different player data then your instructions (more then 9) and be not empty, I'd suggest you make two instructions. One if there is no data (above example) and another if Data is different from your set (>9) and use two different images. 


keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: If this else that
« Reply #6 on: January 25, 2019, 02:23:59 PM »
How would there be more than 8 players? Wouldn’t hurt to make an 8+ logo I guess. No if statement is required for the unknown logo. It obviously didn’t meet a prior condition that yielded a return.

gunthermic

  • Full Member
  • ***
  • Posts: 26
    • View Profile
Re: If this else that
« Reply #7 on: January 26, 2019, 12:46:55 AM »
This is what I ended up with. Thank you both so very much. Unless you see something wrong with my else if. it does appear to work.

Code: [Select]
function PlayerIcon() {
if (fe.game_info( Info.Players ) >= "1") return "images/players.png";
else if (fe.game_info( Info.Players ) <= "" || " ") return "images/none.png";
 }
local PlayerIcon= fe.add_image("[!PlayerIcon]", pb_x, pb_y, pb_w, pb_h);
local p = fe.add_text( "[Players]", pb_text_x, pb_text_y, pb_text_w, pb_text_h);
« Last Edit: January 26, 2019, 01:41:56 AM by keilmillerjr »

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: If this else that
« Reply #8 on: January 26, 2019, 01:42:06 AM »
This is what I ended up with. Thank you both so very much. Unless you see something wrong with my else if. it does appear to work.

Code: [Select]
function PlayerIcon() {
if (fe.game_info( Info.Players ) >= "1") return "images/players.png";
else if (fe.game_info( Info.Players ) <= "" || " ") return "images/none.png";
 }
local PlayerIcon= fe.add_image("[!PlayerIcon]", pb_x, pb_y, pb_w, pb_h);
local p = fe.add_text( "[Players]", pb_text_x, pb_text_y, pb_text_w, pb_text_h);

That’s not going to work as intended.

Path to players.png will be used for any integer equal to or greater than 1. For example: 1, 2, 8, 12, 29 all have the same image!

You do not need an else if statement at the end. It’s not necessary, and will provide no image path if your condition of an empty string or a single space. Good practice is to either return the path regardless or do so but return an error if it’s not an empty string or space. Just in case something odd is passed, you have a way to alert and continue with an error.

Use the code I provided and expand on error catching if you want.

gunthermic

  • Full Member
  • ***
  • Posts: 26
    • View Profile
Re: If this else that
« Reply #9 on: January 26, 2019, 03:02:18 AM »
This is what I ended up with. Thank you both so very much. Unless you see something wrong with my else if. it does appear to work.

Code: [Select]
function PlayerIcon() {
if (fe.game_info( Info.Players ) >= "1") return "images/players.png";
else if (fe.game_info( Info.Players ) <= "" || " ") return "images/none.png";
 }
local PlayerIcon= fe.add_image("[!PlayerIcon]", pb_x, pb_y, pb_w, pb_h);
local p = fe.add_text( "[Players]", pb_text_x, pb_text_y, pb_text_w, pb_text_h);

That’s not going to work as intended.

Path to players.png will be used for any integer equal to or greater than 1. For example: 1, 2, 8, 12, 29 all have the same image!

You do not need an else if statement at the end. It’s not necessary, and will provide no image path if your condition of an empty string or a single space. Good practice is to either return the path regardless or do so but return an error if it’s not an empty string or space. Just in case something odd is passed, you have a way to alert and continue with an error.

Use the code I provided and expand on error catching if you want.

Sir,
yep i used the exact same Blank players Image, and the last line of my code puts the text number layered ontop of the players.png...

Attempting to try to understand what else you wrote. Don't need the none.png? because if not greater than "1" then it just won't display anything? is that what you said in a much better way? lol

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: If this else that
« Reply #10 on: January 26, 2019, 03:12:07 AM »
Ahh... I was going off the code posted from rand0m that had a separate picture for each number of players, not your original code that used a text number. Sorry.

You don’t need the else if statement. Just return the path to none.png file. It will work either way, it’s just unnecessary. Helps clean up your code.