Jump to content

Subroutines


USCcouple

Recommended Posts

For some things I need a large set of code to be run, and I need that group of code to be used in multiple places throughout the cartridge. Is there any way to just make a subroutine and call it instead of have the code rewritten over and over? So far I have just been copying and pasting the text in the lua file and opening it back up in the builder, but I'm hoping that there is a better way since it is time consuming and increases the file size and complexity.

 

Thanks,

Kevan

Link to comment

You should be able to include it in your author functions section. Create a function in that section and call it whenever you need it in the rest of the code.

 

I did a quick test in one of the cartridges I am creating. I could call an author function and the builder did not modify the line when I saved the file. However, since it didn't recognize a function call, editing the functions from where I call my function is now text-based instead of using Groundspeak's GUI. I think you're comfortable with manipulating the code itself, so this should not pose a concern for you.

Link to comment

You should be able to include it in your author functions section. Create a function in that section and call it whenever you need it in the rest of the code.

 

I did a quick test in one of the cartridges I am creating. I could call an author function and the builder did not modify the line when I saved the file. However, since it didn't recognize a function call, editing the functions from where I call my function is now text-based instead of using Groundspeak's GUI. I think you're comfortable with manipulating the code itself, so this should not pose a concern for you.

 

Thanks for the info! Would you mind posting a sample code just so I know where everything should go and how it should be named and called?

 

Thanks,

Kevan

Link to comment

However, since it didn't recognize a function call, editing the functions from where I call my function is now text-based instead of using Groundspeak's GUI. I think you're comfortable with manipulating the code itself, so this should not pose a concern for you.

Okay, that brings up a question. (Threadjack!) For me, a programmer by nature, I'd almost prefer to be able to edit the code directly in any of the event handlers. Is there a way to tell the Builder that I'd like to bypass the automated steps and just use a text editor? If not, and it's considered for a future enhancement, it'd be cool to just have a toggle button that allows you to switch between Guided Mode and Advanced Mode. Of course, you can get yourself into lots more trouble that way, so it might be something that has to be turned on in Preferences before becoming available.

 

Just a thought; I'm working on my first cartridge now, and have several items that need very similar functionality implemented. Copypasta would be so much easier than clickclickclicking through the same options each time. (And yeah, I'd consider making it an author function, but even then I'd need to be able to manually call the function, so voluntary text mode would still be a Good Thing.) Is there a simple way to do this already, and if not, think the Builder team might consider it?

Edited by FoxFireX
Link to comment

setting things invisible or inactive, I save the cartridge, edit it in notepad, and reopen it in the builder.

 

Thanks for the many tips, Ranger. You mention using notepad as an editor, and I wanted to mention that Notepad++ is a free (GPL) editor that does syntax highlighting and code folding, and it has lua included in it's syntax file. But you probably already have your own favorite editor by now ;-}

 

I loaded your function_demo in the builder and although it complains about the event script, it opens an edit window for the user to fix the parsing errors... so once the function call is put in the event script by hand, one could most likely use the edit window provided to update "parse error" script and still get a compile out of the builder.

 

It's too bad that the compiler and emulator are linked libraries and not stand alones.....

Link to comment

I have attached a really simple demo of what I mentioned.

 

Hi Ranger.

 

The example you gave in your demo code acts as a procedure rather than a function. As you well know, a function should return a value.

 

Can LUA do this?

 

I want to be able to have a shared function but check the return value from the calling code.

 

Your help is appreciated.

Link to comment

The example you gave in your demo code acts as a procedure rather than a function. As you well know, a function should return a value.

 

Can LUA do this?

Yes. In fact, Lua functions can return more than one value -- without the usual crutch of using reference parameters. Many of the Wherigo library functions take advantage of this feature. For example, VectorToPoint returns both a distance a bearing.

 

Although it's thick with academic jargon, you may find the Lua reference manual useful.

Link to comment

That's right. Since twolpert answered the question already, I'll provide two examples.

 

First, an example of a function returning a value. Since there is no math.round, I have this bit of code:

function round(num, idp)
 local mult = 10^(idp or 0)
 return math.floor(num * mult + 0.5) / mult
end

As you can see, the "return" statement returns the value.

 

An example of handling a function returning more than one value, the one twolpert mentioned:

local distance,bearing = Wherigo.VectorToPoint(ReferencePoint, zonePoint)

 

Just you wait to see what coding insanity I'm about to release (Monday or Tuesday night) and have for demo at GW6. The funny thing is, when good code is done correctly, it seems simple to others.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...