Need help making a Zscript Menu

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Need help making a Zscript Menu

Postby Mirvini » Wed Feb 17, 2021 4:05 am

I want to make a simple menu, that contains a list of all the "documents" a player has found and lets you select it to read them.

Problem is Menus are really difficult to understand.

The idea I was going with was to make a Menu class inheriting from GenericMenu then iterating through my list of documents to create a new list item for each one.

Heres where things get confusing.
All the exampels in zdoom.p3k seem to use a MenuItem with a MenuItemBase and they seem to kind of just run themselves off of MENUDEF. So I can't for the life of me figure out how to initialize one manually from the code.

For example I tried making a class called DocumentMenuItem extending MenuItemBase but if i just say do this

Code: Select allExpand view
DocumentMenuItem new_item;
new_item.init(stuff...);


I get an error when running: "Tried to read from address zero"
This error happens if i call ANY method on the new_item.

Rather than post all of my code, which is mostly copy and pasting what I think is correct, I was hoping to hear some guidence on how to do this properly.
Mirvini
 
Joined: 26 Jan 2021

Re: Need help making a Zscript Menu

Postby Enjay » Wed Feb 17, 2021 4:24 am

Would Nash's PDA starter kit be of any use to you?

viewtopic.php?f=105&t=65849
User avatar
Enjay
Everyone is a moon, and has a dark side which he never shows to anybody. Twain
 
 
 
Joined: 15 Jul 2003
Location: Scotland

Re: Need help making a Zscript Menu

Postby Player701 » Wed Feb 17, 2021 5:10 am

Mirvini wrote:For example I tried making a class called DocumentMenuItem extending MenuItemBase but if i just say do this

Code: Select allExpand view
DocumentMenuItem new_item;
new_item.init(stuff...);


I get an error when running: "Tried to read from address zero"
This error happens if i call ANY method on the new_item.

Let me explain what's going on here.

Mirvini wrote:
Code: Select allExpand view
DocumentMenuItem new_item;

Here you define a local variable called new_item, which is essentially a pointer to an instance of DocumentMenuItem. But you don't assign any value to your variable.

Mirvini wrote:
Code: Select allExpand view
new_item.init(stuff...);

And here you dereference new_item by calling a method on the instance of DocumentMenuItem it points to, but since you haven't initialized the variable, it doesn't actually point to an instance. That's why you get an error.

You have to assign a value to the variable first, either by making it point to an existing instance, or by creating a new instance of DocumentMenuItem (I assume you wanted the latter):

Code: Select allExpand view
DocumentMenuItem new_item = new('DocumentMenuItem');
new_item.init(stuff...);

Note that you can use an implicit type declaration here because the resulting type of the new expression on the right-hand side is known at compile time:

Code: Select allExpand view
let new_item = new('DocumentMenuItem');

And yes, you may want to check out Enjay's link as well as Marrub's documentation project for some useful general info on the ZScript language itself if you're having issues understanding it. Don't be afraid to ask questions too. :)
User avatar
Player701
 
 
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: Need help making a Zscript Menu

Postby Mirvini » Thu Feb 18, 2021 1:13 am

Ah , so that's how you call the constructor. After calling new DocumentMenuItem() didn't work I couldn't seem to find any examples of how to construct a new object.
Mirvini
 
Joined: 26 Jan 2021

Re: Need help making a Zscript Menu

Postby Player701 » Thu Feb 18, 2021 1:33 am

Yes, but do note that not everything can be constructed this way: actor classes should be instantiated via Spawn, and some built-in classes have static constructor methods (e.g. ThinkerIterator.Create for thinker iterators).
User avatar
Player701
 
 
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: Need help making a Zscript Menu

Postby Mirvini » Fri Feb 19, 2021 5:25 pm

Does Zscript cover garbage collection, or will I also need to Destroy the items in the array of menuItems afterward when I use the new("blah") constructor?
Mirvini
 
Joined: 26 Jan 2021

Re: Need help making a Zscript Menu

Postby Player701 » Fri Feb 19, 2021 11:16 pm

Yes, normal objects should be destroyed and GC'd automatically after they've gone out of scope. This does not apply to thinkers (including actors, which are also a type of thinker) and event handlers - their lifecycle is managed by the engine and you should neither create nor destroy them manually (unless you want to permanently disable your handler for some reason, but that is a very special case, see here for details).
User avatar
Player701
 
 
 
Joined: 13 May 2009
Location: Russia
Discord: Player701#8214
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support


Return to Scripting

Who is online

Users browsing this forum: No registered users and 3 guests