[VB.net]Random events menu

Shout-Out

User Tag List

Results 1 to 4 of 4
  1. #1
    Cush's Avatar Elite User
    Reputation
    504
    Join Date
    May 2006
    Posts
    528
    Thanks G/R
    3/19
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [VB.net]Random events

    Hey.

    Im working on a small turn-based game with VB.net, but im having a bit of trouble. Basically I want to make it so there's a chance that one of quite a few random events happen when the player starts their next turn (And some events have a higher chance than others)

    I'm wondering if there's a way to set this up without making a very large mess of arrays and variables. Kind of struggling to learn much new in VB, seems to be the same thing that happened with C++...I learned the basics, and how to do quite a bit of simple stuff, then everything past that is just confusing and I cant seem to find anywhere that explains it in a way I understand.

    *EDIT*

    Ack, sorry

    [VB.net]Random events
  2. #2
    Apoc's Avatar Angry Penguin
    Reputation
    1388
    Join Date
    Jan 2008
    Posts
    2,750
    Thanks G/R
    0/13
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The easiest way to do it, would be via reflection, and a good usage of OOP.

    Create a base 'GameEvent' class that has all the important info you need.

    Eg;
    ChanceToExecute (double/float/w.e)
    Priority (whatever datatype you want here)
    CanExecute (bool, to see if the event is allowed [good for checking for certain conditions])
    Execute()

    Pretty much; a randomized FSM implementation.

    Excuse the C# code (just run it through a converter or something)

    After loading all your 'GameEvent's, you can use something similar to this:

    Code:
        public abstract class GameEvent
        {
            public abstract float ChanceToExecute { get; }
            public abstract int Priority { get; }
            public abstract bool CanExecute { get; }
            public abstract void Execute();
        }
    
        public class GameEventEngine
        {
            List<GameEvent> _events = new List<GameEvent>();
            Random _rand = new Random();
            public GameEvent GetNextEvent()
            {
                double randPct = _rand.NextDouble();
                int randPriority = _rand.Next(0, 101);
                foreach (GameEvent e in _events)
                {
                    
                    if (e.Priority > randPriority && e.ChanceToExecute > randPct && e.CanExecute)
                    {
                        return e;
                    }
                }
    
                // If percent based event grabbing fails, just randomize selection until we get something we can execute.
                GameEvent ev = _events[_rand.Next(0, _events.Count)];
                while (!ev.CanExecute)
                    ev = _events[_rand.Next(0, _events.Count)];
                return ev;
            }
    Obviously, none of the code to load/prioritize things properly is there. (I have a thread about it in the C# section [it's for an FSM engine])

    Just derive new events from the GameEvent class, and there ya go.

  3. #3
    ReidE96's Avatar Archer Authenticator enabled
    Reputation
    470
    Join Date
    Dec 2006
    Posts
    1,625
    Thanks G/R
    1/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    Public MustInherit Class GameEvent
        Public MustOverride ReadOnly Property ChanceToExecute() As Single
        Public MustOverride ReadOnly Property Priority() As Integer
        Public MustOverride ReadOnly Property CanExecute() As Boolean
        Public MustOverride Sub Execute()
    End Class
    
    Public Class GameEventEngine
        Private _events As New List(Of GameEvent)()
        Private _rand As New Random()
        Public Function GetNextEvent() As GameEvent
            Dim randPct As Double = _rand.NextDouble()
            Dim randPriority As Integer = _rand.[Next](0, 101)
            For Each e As GameEvent In _events
                
                If e.Priority > randPriority AndAlso e.ChanceToExecute > randPct AndAlso e.CanExecute Then
                    Return e
                End If
            Next
            
            ' If percent based event grabbing fails, just randomize selection until we get something we can execute.
            Dim ev As GameEvent = _events(_rand.[Next](0, _events.Count))
            While Not ev.CanExecute
                ev = _events(_rand.[Next](0, _events.Count))
            End While
            Return ev
        End Function
    End Class
    Automagic translation.

  4. #4
    Cush's Avatar Elite User
    Reputation
    504
    Join Date
    May 2006
    Posts
    528
    Thanks G/R
    3/19
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, that just shows me how little I have actually progressed in VB.net :P I dont really understand most of it. Im stuck as to how to learn and understand how to create code like that, too.

    Ill have a mess about with it and see what I can come up with anyway, thanks.

Similar Threads

  1. Replies: 7
    Last Post: 07-08-2012, 02:26 AM
  2. Fast Yellow mob + random event Farming, Act 2-7
    By TehVoyager in forum Diablo 3 Guides
    Replies: 1
    Last Post: 05-23-2012, 05:00 AM
  3. [Request] Event: Random Word/Letter Game
    By interlanden in forum WoW EMU Questions & Requests
    Replies: 4
    Last Post: 03-07-2011, 02:47 AM
  4. Random ban from ascentemulator.net?
    By Bartons in forum WoW EMU Questions & Requests
    Replies: 1
    Last Post: 11-09-2010, 03:03 AM
  5. [question/idea] Random values and events
    By kamil234 in forum WoW Memory Editing
    Replies: 6
    Last Post: 02-27-2010, 06:15 PM
All times are GMT -5. The time now is 01:40 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search