Hello Guest it is April 19, 2024, 05:56:40 AM

Author Topic: Help writing to a register?  (Read 532 times)

0 Members and 1 Guest are viewing this topic.

Help writing to a register?
« on: November 07, 2022, 03:46:35 PM »
I'm really stuck on this one, and I feel like it's something really simple... Here's the context:

I'm using Mach 4. I have some scripts I am working on writing. Two scripts need to access the same value, and a third changes that value.

I create a value in the register, and I wrote two scripts that can access that value no problem. It's the third part I am having trouble with:

I cannot get my third script to change that value no matter what I try. I've read the lua scripting guide and tried the examples several times and they just aren't working. It seems like the examples in that part of the guide are unnecessarily complex.?

To try and figure this out I boiled down the code to the simplest thing I could think of, here's what I'm working with:

Code: [Select]
local inst = mc.mcGetInstance()
hreg = mc.mcRegGetHandle(inst, "iRegs0/Spoilboard") --You can edit this number in the registry
spoilb = mc.mcRegGetValueString(hreg)
--spoilb = tonumber(spoilb)
wx.wxMessageBox(spoilb)

All this code does is prove I can pull a value. This works.

I've tried this code section:



I can't get that code to work at all. I even have had it so it debugs fine, but in watching the diagnostics for the register values it never actually changes the value.

The "WriteRegister" code seems to be exactly what I want, a dead simple way to change a register value but in this doc it's the only place it's mentioned. I can't find any mention of it in the CoreAPI doc either.

Can anyone shed some light on this?

All I want to do is take that registry value (spoilboard) and change it to another number. Very simple. :o
Re: Help writing to a register?
« Reply #1 on: November 07, 2022, 04:38:28 PM »
Hi,
the  "WriteRegister" is a function composed to make it easy to write to a register, but is in fact composed of several steps.

Like when you read a register you need to get the registers handle. That you can read a register suggests you are using the right syntax for that.
Instead of using mc.mcRegGetValueString() to read a value use mc.mcRegSetValueString() to write it.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Help writing to a register?
« Reply #2 on: November 07, 2022, 05:05:41 PM »
You make it sound so simple... I have yet to be able to get it to work.

This is what I tried:

Code: [Select]
local inst = mc.mcGetInstance()
hreg = mc.mcRegGetHandle(inst, "iRegs0/Spoilboard") --You can edit this number in the registry
spoilb = mc.mcRegGetValueString(hreg)
--spoilb = tonumber(spoilb)
wx.wxMessageBox(spoilb)
--things I tried below to write to register
mc.mcRegSetValueString("iRegs0/Spoilboard",2)
mc.mcRegSetValueString(hreg,2)
mc.mcRegSetValueString(spoilb,2)

What the heck am I missing here?

And if it's not already obvious I'm not a great programmer (not even a good one) so don't hesitate to dumb it way down for me...
Re: Help writing to a register?
« Reply #3 on: November 07, 2022, 05:33:45 PM »
Hi,
you need to single step through the code with the editor and at the line immediately after 'hreg = mc.mcRegGetHandle(inst, "iRegs0/Spoilboard")'
and hover over 'hreg' and confirm that you have a valid handle.

This is the correct syntax: 'mc.mcRegSetValueString(hreg,2)', but you are confusing a string and a numeric value.
The numeric value is 2 but 2 is not a string, "2" is a string as is tostring(2). Try 'mc.mcRegSetValueString(hreg,"2")'

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Help writing to a register?
« Reply #4 on: November 07, 2022, 05:49:14 PM »
Ahhhhhhhhhh That was it!

Putting quotations around it did the trick.

So now let me see if I understand what happened here:

The 2 was numeric value (numeric values can only be numbers I assume) but what the registry wanted was a string. Assumably a string could be "2" or "TWO" or "HEYTHERE123" right?

Is this why I had to use "tonumber" when I referenced that registry value in another script? To get that number switched from a string to a numeric value?

Also THANKS!
Re: Help writing to a register?
« Reply #5 on: November 07, 2022, 06:16:42 PM »
Hi,
you used mc.mcRegGetValueString() and therefore Lua was looking to read a string. If you want to convert that string into a numeric value
use the tonumber() function.

If you had used mc.mcRegGetValue(), note the absence of the String qualifier, then Lua would look to read a numeric value.

Lua is a type free language which is very convenient to use as opposed to C which is very strictly typed.

Strict typing is, from a purist point of view, a good thing but it is demanding. If for instance you write 4 bytes to a two byte memory location C will overwrite
the two bytes adjacent to the two defined bytes. This will almost certainly cause a crash. You absolutely must keep track of such details in C
but the result is very tight perfectly performing code.

Lua on the other hand you can code with little thought to the underlying typing. Its not that the data is not tightly typed, it simply has to be or crashes
are a certainty, but that Lua handles that for you. This is great if you want to write quick and dirty scripts, which is Lua's forte.....but you can come unstuck
if you do not realise that Lua has stored a value as a string and then you read it as a numeric.

You might say that Lua takes you two steps forward (not having to type every variable) but one step backwards (potential confusion as to what is written/read).
All in all Lua is fine for scripting purposes, and even reasonable size programs, say a few hundred lines but the formalism of C comes into its own in the large programs.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'