Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Gladox114

Error with string.format()

Question

When i am in the Lua section and want to use the string.format("%g",num) it works like it should but in my code drawtext not...

I want to use gpu.set(x,y,string) but my problem is that i want sometime numbers as strings but i don't know how to convert them properly

info who is wondering whats drawText()            

drawText(print, gpu proxy) is my function that prints something on my second screen with a second graphics card to multi-task

1f98ee56e8459549a2f7fa6ff8eabb70.png.a4b9145ae8cb9851691e6d9ce4c1e1f4.png

98cfaa8a9f57c1dc1a0f7e7d64ffa5cb.png.14980e55f2a73c0c7aac812365e7b116.png

13f15018142c0368502f5e26a0678b88.png.b10f69dd8e0ed0c4ffd0d6204f821bba.png

Link to post
Share on other sites

5 answers to this question

Recommended Posts

  • 0

I would expect that you're somehow overwriting the string lib in your context.. I would recommend using a different variable name for your string value other than `string` as this will probably cause your error later when calling string.format on it.. The result of type(string) by default would normally result in "table" because `string` is a global Lua library.

Link to post
Share on other sites
  • 0

It would be helpful to see the entire code of drawText, not only lines 39 - 50, but I may have found your problem.

It seem like you named your variable 'string', is that right? If so, you are either overriding the global string variable, if you declared 'string' as a public variable, or if you used local, your code is not actually looking for the global 'string' variable, but for your local one. So basically, either way you don't have access to the string functions.

In oder to solve this, you should not name your variables after globally ones unless you don't need to have access to them.

Ok, for your code, you want a program that does a gpu set using a string or a number as input? And if it is a string, you want to replace all \n with ""?

Then this might help:

local function set(str, gpuProxy)
  checkArg(1, str, "number", "string")
  checkArg(2, gpuProxy, "table")
  -- checkArg to ensure you don't have any false input
  -- but you don't really need it
  
  if type(str) == "number" then
    str = tostring(str)
  elseif type(str) == "string" then
    str = str:gsub("\n", "")
  end
  gpuProxy.set(x, y, str)
end

 

Link to post
Share on other sites
  • 0
Quote

Ok, for your code, you want a program that does a gpu set using a string or a number as input? And if it is a string, you want to replace all \n with ""?

Yeah for now i just wanted to go a line down if the string contains "\n" in it.    its cheap ik

27 minutes ago, CptMercury said:

if type(str) == "number" then str = tonumber(str)

I am not allowed to use numbers in gpu.set() but i want so i need to convert however the number to a string.tonumber() converts a string with a number like "10" in to a number like 10. The opposite what i need and i thought that string.format() does the opposite.

 

Link to post
Share on other sites

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
Answer this question...

×   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...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.