【问题标题】:Is it possible to make an unconditional redirect without return in Visual Basic?是否可以在 Visual Basic 中进行无条件重定向而不返回?
【发布时间】:2015-09-14 14:03:28
【问题描述】:

我正在使用 paxCompiler 在 Delphi 中制作游戏书创作程序。 paxCompiler 支持 Basic,这就是我使用 paxBasic 的原因。
在游戏手册中,您有时需要重定向到不同的程序而不返回此处并继续执行。
例如:

procedure p_woods;
begin
 if Random(2)=1 then go('fight');//the function go is my own, it calls the procedure "p_fight"
 print('You stroll peacefully in the woods');
end;

您会看到,在调用该过程后,它应该停留在那里而不是返回这里。
在 Pascal 中,有一个解决方案,但很麻烦:

begin
 go('proc');
 exit;
end;

但我在 Basic 中找不到与此 begin..end 块等效的任何内容。
我的问题:

  1. 什么 Basic 代码可以模拟上面最好的代码?
  2. 是否可以缩短 Pascal 代码?

【问题讨论】:

  • 在 pascal 或 delphi 中没有 go(); 方法这样的东西,afaik。你在想GoTo吗?听起来这就是你想要的。但是,理想情况下,这可能不是您应该采取的路线。分解你的大方法听起来是一个更好的方法。将存在于分支决策之下的任何内容放入单独的方法中。
  • 如果你不希望 go() 下面的代码被执行,那就不要把它放在那里......也许我错过了什么
  • else 关键字有什么问题? if Random(2)=1 then go('fight') else print('You stroll peacefully in the woods'); 工作完成。
  • @J...,Shinkarom 本质上是要求使用 JMP 指令而不是 CALL。这个go 函数是假设的。然而,无论是 gamebook 还是普通的线性导航书,子程序,无论是否返回,似乎都不是阅读书籍行为的好模型。 Shinkarom,您最好为您的任务寻找不同的解决方案。
  • 这种应用程序应该使用状态和转换

标签: delphi basic


【解决方案1】:

不清楚您使用的是什么语言。当您包含 vb.net 标记时,这是一个 VB.Net 解决方案,用于调用名为 Proc 的 Sub 并跳过执行调用的 Sub 的其余部分。

Sub SomeMethod()
    'some code that will be executed

    'The following statement is an example of how you are deciding whether to redirect
    If shouldRedirect = True Then
        Proc 'call the Sub named Proc
        Exit Sub 'Exit the SomeMethod Sub
    End If

    'some code that will not be executed after Proc is called
End Sub

【讨论】:

  • 我添加vb.net是因为paxCompiler的作者说“Basic语言的语法类似于VB.NET。”
  • @shinkarom 如果足够相似,那么我的答案中的代码应该可以工作。
  • 有没有办法使用这样的块,没有条件或循环,就像在 Pascal 中你可以在任何地方插入 begin..end?
  • @shinkarom 是的,您可以在 Sub 中的任何您喜欢的位置编码 Proc,后跟 Exit Sub。但是正如 the_lotus 在评论中指出的那样,如果你总是跳过其余的代码,那么一开始就没有任何意义。
  • 我知道该怎么做了。我将 go("string") 替换为 if true then go('string') exit sub end if。只需要找到一个用于替换引号之外的正则表达式,我就找到了。
猜你喜欢
  • 1970-01-01
  • 2015-12-21
  • 2021-12-25
  • 1970-01-01
  • 2018-04-21
  • 2013-07-10
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
相关资源
最近更新 更多