Hallo there,
I was wondering how to create the following in LUA:
There is this helm in conflux that gives random messages at random intervals. How would one create such an item with LUA ?
question about randomness in LUA
Moderator: Sophia
Forum rules
Please read the Forum rules and policies before posting.
Please read the Forum rules and policies before posting.
-
- Novice
- Posts: 15
- Joined: Mon Dec 19, 2011 3:40 pm
- Sophia
- Concise and Honest
- Posts: 4239
- Joined: Thu Sep 12, 2002 9:50 pm
- Location: Nowhere in particular
- Contact:
Re: question about randomness in LUA
The usual method for randomness in DSB is dsb_rand(min, max).
So, if you want to generate a random number between 1 and 5 inclusive, the function call dsb_rand(1, 5) will be what you want.
By printing out a random index of an array, you can use this to produce random messages.
The minimum is 1, because Lua arrays start at 1. The maximum is the number of elements in the array, expressed in Lua with the # operator.
So, if you want to generate a random number between 1 and 5 inclusive, the function call dsb_rand(1, 5) will be what you want.
By printing out a random index of an array, you can use this to produce random messages.
The minimum is 1, because Lua arrays start at 1. The maximum is the number of elements in the array, expressed in Lua with the # operator.
Code: Select all
random_messages = {
"HELLO THERE.",
"THIS IS A RANDOM MESSAGE.",
"THIS IS A DIFFERENT RANDOM MESSAGE."
}
function random_message()
dsb_write(system_color, random_messages[dsb_rand(1, #random_messages)])
end
-
- Novice
- Posts: 15
- Joined: Mon Dec 19, 2011 3:40 pm
Re: question about randomness in LUA
Thanks sophia I'll try it out.
This will randomly create one message? ... or will it continu to create them (but how do I control the time in which it creates them / the intervals?)
This will randomly create one message? ... or will it continu to create them (but how do I control the time in which it creates them / the intervals?)
- Sophia
- Concise and Honest
- Posts: 4239
- Joined: Thu Sep 12, 2002 9:50 pm
- Location: Nowhere in particular
- Contact:
Re: question about randomness in LUA
That will create one message.
In order to create further messages, some sort of custom message handler and a dsb_msg with a random delay will be the kind of thing you want. However, there are multiple ways to do it so it's difficult for me to suggest one absolutely "correct" approach. In base/msg_handlers.lua there are numerous examples of writing message handlers, so that might be a useful file to look into.
Here is simple code for a message loop. Note that it is missing important features like any way to deactivate it or, if it's to be used as an item, detect if the item is equipped/worn/whatever, but it should provide a good basis for your experimentation.
Now, all you have to do is set the msg_handler of whatever archetype you want to use as your looping message generator to simple_looping_msg_handler in your custom dungeon's objects.lua, and send an instance of that archetype a "Next Tick" (i.e., M_NEXTTICK) message. This will start the whole loop going.
If this seems a little complex, well, that's because it kind of is, if you're just getting into DSB editing. However, it will make sense as you learn more about how messages work.
In order to create further messages, some sort of custom message handler and a dsb_msg with a random delay will be the kind of thing you want. However, there are multiple ways to do it so it's difficult for me to suggest one absolutely "correct" approach. In base/msg_handlers.lua there are numerous examples of writing message handlers, so that might be a useful file to look into.
Here is simple code for a message loop. Note that it is missing important features like any way to deactivate it or, if it's to be used as an item, detect if the item is equipped/worn/whatever, but it should provide a good basis for your experimentation.
Code: Select all
function message_loop(id, data)
dsb_write(system_color, "MESSAGE RECEIVED.")
dsb_msg(dsb_rand(5, 60), id, M_NEXTTICK, 0)
end
simple_looping_msg_handler = {
[M_NEXTTICK] = message_loop
}
If this seems a little complex, well, that's because it kind of is, if you're just getting into DSB editing. However, it will make sense as you learn more about how messages work.
-
- Novice
- Posts: 15
- Joined: Mon Dec 19, 2011 3:40 pm
Re: question about randomness in LUA
Super. I will look into it. At the moment I am doing to much things at the same time. So I'll park this little project awhile. First need to get to grips with the graphics for walls. (see other post)