Insert an image to message

Post new mods and scenarios here.

Moderator: MOD_Command

Post Reply
LorenAgus
Posts: 12
Joined: Wed Nov 24, 2021 3:00 pm

Insert an image to message

Post by LorenAgus »

First of all greetings from Spain.
I have a question that I cannot find the answer in the forums.

When editing a scenario, I would like to include an image in a message that appears to the player when an X condition occurs. I see the option to include the image, but I can't figure out what to put in the "Href" box.

I would be very grateful if someone could help me. Thanks.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Insert an image to message

Post by KnightHawk75 »

TLDR:
If you are using an image from the internet (ie your scene requires internet connection) and this is just a basic Message action then you can just enter the url either as the href (when inserting image) or when editing the raw html's src= property.

(click thumbnail for full image)
Image


Extended: (provided not so much for your question but for others in future that may come up)

Some things to know for image paths relating to scene Description or Briefings only:
-If you want to use the local filesystem you can use relative path referencing in loaddoc'd html.
-If using [loaddoc] external file method then see: https://www.matrixgames.com/forums/tm.asp?m=4973025 for how to use them and for the various ways you can use relative paths (there are functional example scenes included).

Some things to know for image paths relating to Action Messages or Special Messages only:
- You can't use the [loaddoc] method. (that I know of as of build 1147.36)
- If trying to use a local file path you can't use relative path referencing like with [loaddoc], and must use a full path.
- Since it must be full path you will have to build it on the fly using VP_GetScenario().FileNamePath as your starting point.
- If trying to use a standard display message with a local file image that is built at runtime, the preview image will not show unless it's currently set to a valid local path and file on the system.
- You can insert placeholder text like src="__URL1__" where you replace __URL1__ on scene startup or dynamically during use, where you use the action-message really as just a template.
- You do need to be careful about how that is done, especially if actually doing a permanent replace via lua. One must first convert <, >,' ','&' etc codes to real characters before usage or setting back, otherwise it can get double escaped when setting the action text, or if just using the raw text it gets read literally by SpecialMessage who expects the html).

All: Keep your html to 4.x and css\styles and to what is supported in IE8-9ish.

So if you're wanting images in special message and trying to use internet source, then just use normal url.
If trying to use local file system say from loading from the folder the scene is contained in then use src= VP_GetScenario().FileNamePath .. '\\nameofimage', or alternatively src= 'file:///' .. VP_GetScenario().FileNamePath .. '\\nameofimage.png'. Just know that relative entries like 'file:./nameofimage.png' or './nameofimage.png' that work elsewhere will not function in display/special messages. Usage of said VP_ function requires cmo build 1147.2x+

For example a console test:

Code: Select all

--console example setup
 local gKH = {}; gKH.Message={}; gKH.SceneGlobals={}; gKH.SceneGlobals.SceneMessages={};
 gKH.SceneGlobals.SceneMessages.InitialMsg={};
 
 --little function to convert whats in Message.Text (xml escaped) back to html.
 function gKH.Message.EscapedToHTML(s)
   s = s:gsub('<',"<"):gsub('>',">"):gsub(' '," "):gsub('&',"&"):gsub(''',"'");
   return s; 
 end
 
 --this returns the Message.Text field from a specified existing action name or guid.
 function gKH.Message:GetActionMessageText(actionName)
   local r, ea = pcall(ScenEdit_SetAction,{Description=actionName, Mode='list'});
   if ((((r==true) and ea ~=nil) and ea.actions ~= nil) and ea.actions[1].Message ~=nil) and ea.actions[1].Message.Text ~=nil then
     return gKH.Message.EscapedToHTML(ea.actions[1].Message.Text); --unescape it first.
   end
   return nil;
 end
 

Code: Select all

 --gets the existing action text, does a search and replace for everything in the table and returns the result.
 function gKH.Message:MessageFromTemplate(actionName,valuesTable,bChangeAction)
   if bChangeAction == nil then bChangeAction = false; end
   local txt = gKH.Message:GetActionMessageText(actionName)
   if ((txt ~= nil) and valuesTable ~=nil) and type(valuesTable) == 'table' then
     for k,v in pairs(valuesTable) do
       txt = txt:gsub(k,v); --replace k with v
     end
     if bChangeAction == true then ScenEdit_SetAction({Description=actionName, Mode='update',type="Message",Text=txt}) end
     return txt;
   else return nil; end
 end

Code: Select all

 --wraps the send adding default body start and default body end html if not otherwise provided.
 function gKH.Message:SendHTMLMessageBlackOnWhite2(theSide,msgOpen,theMsg,msgClose,lat1,lon1)
      if(theSide == nil) then return;end
      if (msgOpen == nil) or msgOpen =='' then
      msgOpen = '<HTML><style>body {background-color: #FFFFFF; font-family: """New Times Roman""",arial,verdana,san-serif; color:#000000;}</style><body>'; end
      if (msgClose ==nil) or msgClose =='' then msgClose ='</body></HTML>' ; end
      if(lat1 ==nil or lon1==nil) then
           ScenEdit_SpecialMessage(theSide,string.format('%s%s%s',msgOpen,theMsg,msgClose) );
      else
           ScenEdit_SpecialMessage(theSide,string.format('%s%s%s',msgOpen,theMsg,msgClose),{lat=lat1,lon=lon1});
      end
 end
 

Code: Select all

 --sample vars
 --sample header.
 gKH.SceneGlobals.SecureCommsHeader = 
 '<p style="color: #000000; background-color: #cccccc"><IMG src="%s" style="vertical-align:middle" height="45" width="45"></IMG>    Secure Communication</p>';
 gKH.SceneGlobals.SceneMessages.InitialMsg.uri = 
 "https://upload.wikimedia.org/wikipedia/commons/thumb/0/09/Seal_of_the_United_States_Department_of_the_Navy.svg/240px-Seal_of_the_United_States_Department_of_the_Navy.svg.png"
 -- you will manually have to add this file to the same folder as scene for this exact test
 gKH.SceneGlobals.SceneMessages.InitialMsg.filepath = 
 VP_GetScenario().FileNamePath .. '\\240px-Seal_of_the_United_States_Department_of_the_Navy.png' 
 -- sample msg.
 gKH.SceneGlobals.SceneMessages.InitialMsg.text = 
 '<p>Commander,</p><p>This is some couple line message. Here is the text for me to look at, hello world. Ok this should be enough to test with for now. Good Luck.</p>';
 
 --Example when building text yourself, swap values based on if user has internet.
 gKH.SceneGlobals.InternetEnabled = false; local tmpstr = gKH.SceneGlobals.SceneMessages.InitialMsg.uri
 if gKH.SceneGlobals.InternetEnabled ==false then tmpstr = gKH.SceneGlobals.SceneMessages.InitialMsg.filepath; end 
 gKH.Message:SendHTMLMessageBlackOnWhite2('playerside',nil,string.format(gKH.SceneGlobals.SecureCommsHeader,tmpstr) .. gKH.SceneGlobals.SceneMessages.InitialMsg.text)
 
 --Example when using an existing message in this case with images in subfolder images in a subfolder MyCoolScenarioNameAssets.
 --This presumes there is a action called test containing something like: 
 --<BODY scroll=auto><P>Testies 1 2 <IMG alt=testimage src="__URL1__"> </P><P>Side: __PLAYERSIDE__</P></BODY>
 ScenEdit_SpecialMessage('playerside',gKH.Message:MessageFromTemplate('test',{__URL1__=VP_GetScenario().FileNamePath .. 
 '\\MyCoolScenarioNameAssets\\images\\MyCool_ScenarioName_Yellow.png',__PLAYERSIDE__=ScenEdit_PlayerSide()},false))
 

As you might guess I often have use filepath's and Message Actions that have images or other _fillinblank_ values in templates and just display them via specialmessage using the template with placeholder and fill them in before display. Alternatively you can update the original message with the revised text in a condition before display (gKH.Message:MessageFromTemplate('thename',{stuffhere},true)), etc. When it comes to filepaths I don't recommend that approach as if a user moves their scenario files between plays the template values have already been replaced and saved they can end up out of date.

edit: changed some of the code line breaks where there are longer string to try to not scroll off the forum screen as much as original.
LorenAgus
Posts: 12
Joined: Wed Nov 24, 2021 3:00 pm

RE: Insert an image to message

Post by LorenAgus »

Thank you very much for the reply. I think I understood the dynamics.
Post Reply

Return to “Mods and Scenarios”