Conditionally register global step.

Post Reply
User avatar
Devy
Member
Posts: 133
Joined: Sat Jan 21, 2017 02:31
GitHub: DevyHeavy
In-game: devy

Conditionally register global step.

by Devy » Post

How would I go about using an if statement to call minetest.register_globalstep()? I've tried this:

Code: Select all

if something == true then
  minetest.register_globalstep(function(dtime)
      --Do stuff
  end)
end
This is in the highest local space of a file called by dofile. It doesn't work though, the global step does not register. If you need more info just ask. This is probably simple so please forgive my naive question.

Thank you!

User avatar
karamel
Member
Posts: 56
Joined: Wed Jul 19, 2017 21:51
Contact:

Re: Conditionally register global step.

by karamel » Post

This is how it should work…

Are you sure your file is parsed (first step) and sure that something is really true? Then are you sure --Do stuff actually does something?

An ugly debugging way to check could be to add some minetest.log('error', 'Hello') at some points to see if your code is really executed (warning: this will probably fill up your console, be ready to quit game quickly). You can also put if something == true then minetest.log('error', 'something') else minetest.log('error', 'not something') end.

Don't do this at home, this is too ulgly. Don't forget to remove all those logs just after. You can use print instead if you run minetest from terminal or check debug.txt. This would be a little cleaner. Then you can look for all print before releasing because there is no point to put a print in file.

It would be a little more easy to debug with the actual code.

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: Conditionally register global step.

by Byakuren » Post

If you don't want it to be logged you can just call print and run minetest from the command line.
Every time a mod API is left undocumented, a koala dies.

User avatar
Devy
Member
Posts: 133
Joined: Sat Jan 21, 2017 02:31
GitHub: DevyHeavy
In-game: devy

Re: Conditionally register global step.

by Devy » Post

Thanks, guys. Turns out I'm an idiot and I was trying to check if the variable was true (bool) when the variable was a string. Duh!

User avatar
lisacvuk
Member
Posts: 274
Joined: Sat Jul 19, 2014 11:52
GitHub: lisacvuk
IRC: lisac
In-game: lisacvuk
Location: Serbia, Užice

Re: Conditionally register global step.

by lisacvuk » Post

Why not just check if the variable is true inside the global step, and return if it is not?
It's lisac, not lisa.
400 character limit? Am I writing a book?
Administrator on Craig's server. Minetest player.
"The enemy pales when they see the face of Dazzle!" ~ Dazzle obviously.
I live in Serbia.
Steam | OpenDOTA
My mods:
Tool ranks
I appreciate donations in TF2 items. :)

User avatar
karamel
Member
Posts: 56
Joined: Wed Jul 19, 2017 21:51
Contact:

Re: Conditionally register global step.

by karamel » Post

If you do so it is checked at least every time the function could be effectively run.

If you check before registering you check it only once for all. This prevent reenabling it automatically once the server is run (by changing a variable in-game, but you can reenable it an other way), but is cleaner. So you doesn't register an useless globalstep.

It's a question about code styling and "beauty", because a function call and a check every 0.1 or 0.05s (or even 0.01) won't make any difference, but hey, why would you do useless things?

User avatar
azekill_DIABLO
Member
Posts: 7507
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO
Location: OMICRON
Contact:

Re: Conditionally register global step.

by azekill_DIABLO » Post

Devy wrote:How would I go about using an if statement to call minetest.register_globalstep()? I've tried this:

Code: Select all

if something == true then
  minetest.register_globalstep(function(dtime)
      --Do stuff
  end)
end
This is in the highest local space of a file called by dofile. It doesn't work though, the global step does not register. If you need more info just ask. This is probably simple so please forgive my naive question.

Thank you!
why not register a global step and then put an if? example:

Code: Select all

minetest.register_globalstep(function(dtime)
    if something == true then
        --Do stuff
  end
end)
it should do what you want
Gone, but not dead. Contact me on discord: azekill_DIABLO#6565
DMs are always open if you want to get in touch!

User avatar
lisacvuk
Member
Posts: 274
Joined: Sat Jul 19, 2014 11:52
GitHub: lisacvuk
IRC: lisac
In-game: lisacvuk
Location: Serbia, Užice

Re: Conditionally register global step.

by lisacvuk » Post

azekill_DIABLO wrote:
Devy wrote:How would I go about using an if statement to call minetest.register_globalstep()? I've tried this:

Code: Select all

if something == true then
  minetest.register_globalstep(function(dtime)
      --Do stuff
  end)
end
This is in the highest local space of a file called by dofile. It doesn't work though, the global step does not register. If you need more info just ask. This is probably simple so please forgive my naive question.

Thank you!
why not register a global step and then put an if? example:

Code: Select all

minetest.register_globalstep(function(dtime)
    if something == true then
        --Do stuff
  end
end)
it should do what you want
Yeah, it is what I suggested. It IS the cleaner way. Bool checks are lightning fast, anyways, and ability to alter settings at runtime is probably worth it
It's lisac, not lisa.
400 character limit? Am I writing a book?
Administrator on Craig's server. Minetest player.
"The enemy pales when they see the face of Dazzle!" ~ Dazzle obviously.
I live in Serbia.
Steam | OpenDOTA
My mods:
Tool ranks
I appreciate donations in TF2 items. :)

User avatar
azekill_DIABLO
Member
Posts: 7507
Joined: Wed Oct 29, 2014 20:05
GitHub: azekillDIABLO
In-game: azekill_DIABLO
Location: OMICRON
Contact:

Re: Conditionally register global step.

by azekill_DIABLO » Post

i won't change anything, except if you need a 0.001 precision, which shouldn't happen at all.
Gone, but not dead. Contact me on discord: azekill_DIABLO#6565
DMs are always open if you want to get in touch!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests