Author Topic: Dynamic Message Box Creation  (Read 9914 times)

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Dynamic Message Box Creation
« Reply #15 on: 2006-12-04, 02:37:51 PM »
Gah!  Bested by durnurd again ;)
Although I wonder if the internal script can affect local variables in the outer script.  I suspect there might be a problem with the msg variable.  You might have to make that a global variable instead of a local variable, and even then, I wouldn't be surprised if that didn't work.

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Re: Dynamic Message Box Creation
« Reply #16 on: 2006-12-04, 02:59:12 PM »
Yeah, that didn't work.

Something's gotta be wrong... First off, the original message box appears, with the code in it. After that, I get an error pointing me to the line of Execute(tmp). Any ideas on what's going on, guys?

And don't worry bluemonkmn, I don't see durnurd making any game development kits  :).

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Dynamic Message Box Creation
« Reply #17 on: 2006-12-04, 04:42:37 PM »
I tested the code I wrote and it worked perfectly.  Of course, my test wasn't inside GameDev, so we can't really be sure which line is actually problematic unless you copy the code you wrote in the textbox into the actual script just to test what's going on in place of the Execute line.  Then it will tell you exactly which line doesn't work.

But it does work with local and global variables just fine.  I read a thing about the Execute and Eval functions (written by the guy who was responsible for putting those very functions in the VBScript engine) to make sure beforehand.

As for game development kits... well, not any visual game development kits, anyway.
http://ta-engine.sf.net
Edward Dassmesser

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Re: Dynamic Message Box Creation
« Reply #18 on: 2006-12-04, 05:17:02 PM »
Ok, it seems like the problem is either at or very near to msg = "something". I get an "expected statement" error. Does that mean I'm leaving something out? Here's what I've got:

Code: [Select]
Sub Player_OnSpecialFunction(Func)
   If Func.FuncType = 1 AND left(Func.Value,7) = "#COND" + vbcrlf Then
      dim tmp, msg
      tmp = mid (Func.Value,8)
      dim PROGRESS
      PROGRESS = HostObj.GamePlayer.InvQuantityOwned(1)
      switch (PROGRESS)
         case 0 to 10
            msg = "Here's a message!"    'Problem apparently here
      end select
      ShowMessage(msg)
   End if
End Sub

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Dynamic Message Box Creation
« Reply #19 on: 2006-12-04, 06:34:52 PM »
I had noticed that you replied to my message as I was editing it.  I posted it, checked it, made a few changes to the code, then fixed the post, apparently after you already copied what I posted.  I'm used to writing C-style code, so I used Switch instead of Select Case.  Change

switch (PROGRESS)

to

Select Case PROGRESS

Also, you'll have to put something in, like BlueMonk said earlier, that changes the type of all of the special functions of type 1 that have #COND at the beginning to another type.  This prevents the code from being displayed as a message.  Something along the lines of

Code: [Select]
Sub Player_OnPlayInit()
   dim tmp
   for i = 0 to ProjectObj.MapCount
      for j = 0 to ProjectObj.Maps(i).SpecialCount
         set tmp = ProjectObj.Maps(i).Specials(j)
         if tmp.FuncType = 1 AND len(tmp.Value) >= 5 AND left(tmp.value,5) = "#COND" then tmp.FuncType = 7
      next
   next
End Sub
« Last Edit: 2006-12-04, 06:45:51 PM by durnurd »
Edward Dassmesser

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Re: Dynamic Message Box Creation
« Reply #20 on: 2006-12-04, 09:11:59 PM »
Okay, the home stretch: two problems left.

1) It doesn't want to accept "case 0 to 10" as valid. It'll take "case 0", but the whole "to" thing throws it for a loop.

2) Is there some assortment of characters that will give me a line break? If I just use enter, it's like a new line in script, which breaks the string.

Other than that, this is finally working.

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Dynamic Message Box Creation
« Reply #21 on: 2006-12-04, 09:25:21 PM »
I went ahead and made a test project and ironed out a few kinks.  Here's my final version:

Code: [Select]
dim PlayerName
PlayerName = "John"

Sub Player_OnSpecialFunction(SpecialObj)
   If SpecialObj.FuncType = 7 AND len(SpecialObj.Value) >= 7 AND left(SpecialObj.Value,7) = "#COND" + vbcrlf Then
      dim tmp, msg
      tmp = mid(SpecialObj.Value,8)
      Execute(tmp)
      ShowMessage(msg)
   End if
End Sub

Sub ShowMessage(msg)
   msg = Replace(msg,"%PLAYER%",PlayerName) 'Insert Players name where appropriate

   dim msgSpecial, old
   Set msgSpecial = NewSpecialFunction
   msgSpecial.funcType = 1
   msgSpecial.Value = msg
   old = HostObj.ScriptTimeoutSeconds
   HostObj.ScriptTimeoutSeconds = 0
   ProjectObj.GamePlayer.ActivateFunction(msgSpecial)
   HostObj.ScriptTimeoutSeconds = old
End Sub

Sub Player_OnPlayInit()
   dim tmp
   for i = 0 to ProjectObj.MapCount - 1
      for j = 0 to ProjectObj.Maps(i).SpecialCount - 1
         set tmp = ProjectObj.Maps(i).Specials(j)
         if tmp.FuncType = 1 AND len(tmp.Value) >= 5 AND left(tmp.value,5) = "#COND" Then
            tmp.FuncType = 7
            tmp.Flags = tmp.Flags OR 8 'INTFL_RAISEEVENT
         end if
      next
   next
End Sub

HostObj.SinkObjectEvents ProjectObj.GamePlayer, "Player"
HostObj.ConnectEventsNow()
ProjectObj.GamePlayer.Play 16

And the code inside the special function:

Code: [Select]
#COND
dim FOUNDRING
FOUNDRING = ProjectObj.GamePlayer.InvQuantityOwned(0)
select case FOUNDRING
   case 0
      msg = "Oh woe is me, I have lost my wedding ring!  Who are you?  %PLAYER%?  Can you help me find my ring, %PLAYER%?"
   case 1
      msg = "You found it!  Thank you so much."
      ProjectObj.GamePlayer.InvQuantityOwned(0) = 2
   case else
      msg = "Thanks again for finding my ring, %PLAYER%!"
   end select

In the script, I changed the script timeout to 0 while showing the message so the script doesn't timeout if the player looks at it for more than two seconds.  I also changed an IF to check for the right function type in OnSpecialFunction since it changes in OnPlayInit.  I also took the liberty of adding a Raise Event flag to all of the messages so even if you forget to put them in during design time it will put them in during runtime.  Finally, I added another IF in the OnSpecialFunction to make sure that the value is at least 7 characters long before trying to take the first 7 of them.

In the GUI code, I didn't do much.  I just renamed the variable to FOUNDRING since that's all that variable can account for.  I also automatically added 1 to it after displaying the initial "You found it" message, so that it will display the "Thanks again" message any subsequent times.

As for your line break, if you want a line break in the message, do something like:

Code: [Select]
msg = "First line of the message" + vbcrlf + "Second line of the message
Or

Code: [Select]
msg = "First line of the message" + vbcrlf + _
      "Second line of the message

Which does the same thing but is easier to read.  The vbcrlf stands for VB Carriage Return Line Feed, which is basically the fancy Windows way of saying New Line.
Edward Dassmesser

durnurd

  • Lead Lemming
  • Expert
  • Fanatic
  • *****
  • Posts: 1234
  • Games completed so far: 0
    • MSN Messenger - durnurd@hotmail.com
    • View Profile
    • Find My Ed
Re: Dynamic Message Box Creation
« Reply #22 on: 2006-12-04, 09:30:34 PM »
As for the case thing... looks like VBScript isn't as fancy as VB itself.  The only thing you can do in VBScript is check for individual values or comma-separated lists of values:

Code: [Select]
select case x
   case 1
   case 2, 3, 4
   case else
end select

If you want to check for something with more than a few values, I'd use an If-Else statement instead:

Code: [Select]
If x >= 0 AND x <= 10 Then
   msg = "Message 1"
else if x > 15
   msg = "Message b"
else
   msg = "Message c"
end if
Edward Dassmesser

bluemonkmn

  • SGDK Author
  • Administrator
  • Fanatic
  • *****
  • Posts: 2761
    • ICQ Messenger - 2678251
    • MSN Messenger - BlueMonkMN@gmail.com
    • View Profile
    • http://sgdk2.sf.net/
    • Email
Re: Dynamic Message Box Creation
« Reply #23 on: 2006-12-05, 06:55:03 AM »
Are you sure you want this level of flexibility at the cost of writing script for every conditional message?  I'm all for flexibility (which you'll see in SGDK2), but sometimes the complexity it introduces is just unwieldy :).  So you gotta find a good balance.  Of course, you can also have the best of both worlds.  Use #CODE for scripted messages and #COND for simpler conditions.

eric22222

  • Fanatic
  • ***
  • Posts: 177
    • View Profile
    • Eric Online
    • Email
Re: Dynamic Message Box Creation
« Reply #24 on: 2006-12-05, 09:08:38 AM »
That got it! It's finally doing everything we need. Thanks alot, durnurd!

And bluemonkmn, I realize this is going to make things a bit more complex, but now that it's done, the hard part's over. All it is now is copying a few lines and then adding all the crazy stuff we need to happen. I'm sure it'll be worth the effort in the end.